Keyboard Shortcuts
ctrl + shift + ? :
Show all keyboard shortcuts
ctrl + g :
Navigate to a group
ctrl + shift + f :
Find
ctrl + / :
Quick actions
esc to dismiss
Likes
Search
Re: Encoder tinning
Jack Purdum
I agree that the duplicate definition is unnecessary. What I was disagreeing with was your statement that the definition would cause a compiler error; it won't. The most you might get is a warning if r goes unused at that scope level. Also, the definition of r in setup() really doesn't waste any SRAM because, as soon as execution leaves setup(), the stack space allocated to r is reclaimed. It's variables defined in the heap space that don't get reclaimed and permanently chew up SRAM space. Jack, W8TEE From: Vince Vielhaber <vev@...> To: [email protected] Sent: Tuesday, August 15, 2017 2:21 PM Subject: Re: [BITX20] Encoder tinning The one in the local scope is still redefining r.? It's also unneeded. You're setting a global variable (counter) based on r.process()'s value. ? You're not changing anything to do with r, so while it's shadowing the global r, it's also wasting precious program space. Because the compiler (wrongly, IMO) doesn't complain, it's no reason to do it. Vince. On 08/15/2017 02:05 PM, Jack Purdum via Groups.Io wrote: > OK, now you look again. The two /r /objects are defined at different > scope levels so there is no conflict. The first definition of /r /has > global scope while the second has function (local) scope. Since they are > at different scope levels, the compiler will not throw an error. Prove > it to yourself with this expansion of your post: > > #include <Rotary.h> > Rotary r = Rotary(3, 2);? ? ? // Global scope > > int counter; > > void setup() > { >? Rotary r = Rotary(3, 2);? ? // Local scope >? unsigned char result = r.process(); >? if (result == 0) >? ? counter = 0; >? Serial.begin(9600); > } > > ISR(PCINT2_vect) { >? unsigned char result = r.process(); >? if (result == DIR_CW) >? ? counter = counter + 1; >? if (counter > 1020) { >? ? counter = 1020; >? } >? else if (result == DIR_CCW) >? ? counter = counter - 1; >? if (counter < 5) { >? ? counter = 5; >? } >? Serial.println (counter); > > } > void loop() { > } > > The compiler compiles the code without error, as it should. > > Jack, W8TEE > ------------------------------------------------------------------------ > *From:* Vince Vielhaber <vev@...> > *To:* [email protected] > *Sent:* Tuesday, August 15, 2017 12:38 PM > *Subject:* Re: [BITX20] Encoder tinning > > > Look again: > >>> #include <Rotary.h> >>> Rotary r=Rotary(3, 2);? ? <---------------------- >>> >>> comment out? analogtinnig? A7 >>> >>> in setup() >>> Rotary r=Rotary(3, 2);? ? <---------------------- > > r is defined more than once. > > > Vince. > > > > On 08/15/2017 11:40 AM, Jack Purdum via Groups.Io wrote: >> He's not redefining the object/r/. He is defining an /unsigned char/ >> variable named /result/ into which he is assigning the return value from >> the /process() /method of the? /r/ object. There should be no compiler >> error from that. >> >> Jack, W8TEE >> >> ------------------------------------------------------------------------ >> *From:* Vince Vielhaber <vev@... <mailto:vev@...>> >> *To:* [email protected] <mailto:[email protected]> >> *Sent:* Tuesday, August 15, 2017 10:58 AM >> *Subject:* Re: [BITX20] Encoder tinning >> >> >> You're going to get an error or warning for redefining r.? You've >> definied it globally right under your include statement, then again in >> setup.? In setup it's only local but you'll still get an error/warning. >> >> Vince. >> >> >> On 08/14/2017 08:21 PM, Norberto Modanesi wrote: >>> Hi folks: >>> What if I add these lines to the Raduino code >>> #include <Rotary.h> >>> Rotary r=Rotary(3, 2); >>> >>> comment out? analogtinnig? A7 >>> >>> in setup() >>> Rotary r=Rotary(3, 2); >>> >>> Then the interrupt routine >>> ISR(PCINT2_vect) { >>>? unsigned char result = r.process(); >>>? if (result == DIR_CW) >>>? ? counter=counter+1; >>>? ? if(counter>1020){counter=1020;} >>>? else if (result == DIR_CCW) >>>? ? counter = counter-1; >>>? ? if(counter<5){counter=5;} >>>? ? Serial.println (counter); >>> >>>? ? } >>> Finaly, replace every analoRead(analogtunning) with counter.- >>> >>> Would it work? >>> >>> 73 de LU5DNM >>> >>> Norberto Modanesi >>> San Nicol¨¢s >>> >>> >>> >> >> -- >>? Michigan VHF Corp.? > <><> >> <><> >>? ? ? ? ? ? ? ? ? ? ? ? ? > <> >> <> >> >> >> >> >> >> >> > <> >>? ? Virus-free. www.avast.com >> > <> >> >> >> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> >> > > -- >? Michigan VHF Corp.? <> > <> >? ? ? ? ? ? ? ? ? ? ? ? ? > <> > > > > > > -- ? Michigan VHF Corp.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
to navigate to use esc to dismiss