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
Encoder tinning
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 |
Jack Purdum
Also, you do not want to do any Serial.print() or Serial.println() calls in an interrupt service routine. They use their own ISR and you may end up in a blocking situation. Jack, W8TEE From: John P <j.m.price@...> To: [email protected] Sent: Monday, August 14, 2017 8:53 PM Subject: Re: [BITX20] Encoder tinning On Mon, Aug 14, 2017 at 05:21 pm, Norberto Modanesi wrote: Would it work?One way to find out! ? -- John - WA2FZW |
?
toggle quoted message
Show quoted text
Yes. I put Serial.print in ISR only to see how
counter goes up and down.-
?
Norberto Modanesi
San Nicol¨¢s
|
Vince Vielhaber
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.
toggle quoted message
Show quoted text
Vince. On 08/14/2017 08:21 PM, Norberto Modanesi wrote:
Hi folks: --
Michigan VHF Corp. |
Jack Purdum
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@...> To: [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.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
Vince Vielhaber
Look again:
r is defined more than once.#include <Rotary.h> 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/-- Michigan VHF Corp. |
Jack Purdum
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@...> > *To:* [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.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
Vince Vielhaber
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.
toggle quoted message
Show quoted text
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 --
Michigan VHF Corp. |
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.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
John P
On Mon, Aug 14, 2017 at 08:03 pm, Jack Purdum wrote:
Also, you do not want to do any Serial.print() or Serial.println() calls in an interrupt service routine. They use their own ISR and you may end up in a blocking situation.Thanks for that Jack! A week late and a dollar short! Had that issue in the mag loop controller I've been working on last week. I pretty figured out what was going on right away. Figuring out how to get around it was a?different matter! ? -- John - WA2FZW |
Vince Vielhaber
Actually I said "error/warning". Arduino seems to let you get away with that, gcc and g++ typically do not 'cuze I see that regularly when compiling someone else's code or when I forget and reuse a global's name - which isn't hard when you're dealing with a program spread across a couple dozen files with 100s of thousands of lines of code.
toggle quoted message
Show quoted text
The wasted space I was referring to is the compiled code, not the running code. Vince. On 08/15/2017 03:05 PM, Jack Purdum via Groups.Io wrote:
I agree that the duplicate definition is unnecessary. What I was --
Michigan VHF Corp. |
Jack Purdum
Part of the reason for this exchange we're having is that I don't want anyone to be discouraged from trying to experiment with programming an Arduino. To you and me, error/warning is no big deal, but to a newcomer to programming, I've actually seen it literally paralize a student. They think they "broke" something. An error means the compile stops and the upload does not happen. While the duplicate definition of the object you pointed out shouldn't be done, it is not an error. Indeed, setting the proper flags to the compiler and it won't even issue a warning. Warnings are often semantic (not syntax) errors that are not sufficiently bad to cease the compile. Unused variables are an examlpe. The mistake you first pointed out is not an error, but will generate a warning. I don't want warnings to discourage new programmers from slogging through those warnings. Starting with (I think) IDE version 1.8.0, the gcc parser was tightened up, which caused it to increase the number of warning significantly. The thing that irritates me most is that they did not take the time to "fix" the standard library files to the point where no warnings are issues. Newcomers see these warnings and may wonder if it is "safe" to proceed. Generally, if the IDE completes the upload, you're good to go. If any warnings from the IDE pertain to your own code, you should take the time to fix them. Otherwise, I leave them alone. I'm pretty sure that I could fix the standard library warnings, but then I would be out of sync with everyone else, so I don't. I need to see the same problems anyone else may see. Namespace collisions are pretty rare. Names in other files are private to those files so the namespace doesn't flow between files unless you want it to (e.g., using the extern keyword). Include files can be nettlesome because some do define global variables. Still, those are pretty obvious since your INO file had a #include in it. You're right about the compile-time statistics; they cannot reflect runtime memory use other than functions like freeRam(). As a rule, if SRAM starts to approach 70% usage, I start to get nervous since parameter use via function calls isn't directly available. If your code starts to act "funny", quite often it's because you've run out of runtime SRAM even though the compiletime stats say you have a third left. To any beginner who wants to work at removing warning or error messages from their own code, ususally a Google search using the format: "Arduino " + plus the error/warning message will give you enough information to isolate the error. Also, the IDE often points you to the proper line number. Sometimes the error is many lines earlier before the compiler throws up its hand and gives up, so if the line of the error message looks ok, look at earlier lines for the error. Jack, W8TEE From: Vince Vielhaber <vev@...> To: [email protected] Sent: Tuesday, August 15, 2017 10:58 PM Subject: Re: [BITX20] Encoder tinning Actually I said "error/warning".? Arduino seems to let you get away with that, gcc and g++ typically do not 'cuze I see that regularly when compiling someone else's code or when I forget and reuse a global's name - which isn't hard when you're dealing with a program spread across a couple dozen files with 100s of thousands of lines of code. The wasted space I was referring to is the compiled code, not the running code. Vince. On 08/15/2017 03:05 PM, Jack Purdum via Groups.Io wrote: > 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@... <mailto:vev@...>> >> *To:* [email protected] <mailto:[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@...> > <mailto:vev@... <mailto:vev@...>>> >>> *To:* [email protected] <mailto:[email protected]> > <mailto:[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.? <> > <> >? ? ? ? ? ? ? ? ? ? ? ? ? > <> > > > > > > -- ? Michigan VHF Corp.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
¿ªÔÆÌåÓýThanks Jack! ? All that needed to be made explicit. You've precisely expressed the trepidation that I have as an aspiring home builder and tyro with these amazing microprocessors.? -John? On Aug 16, 2017, at 09:00, Jack Purdum via Groups.Io <econjack@...> wrote:
|
to navigate to use esc to dismiss