I have an Arduino Esplora REV04 board. I an trying to make it a controller for one of my games and use the Linear Potentiometer as the item selector. I have written a series of for loops with the intent of checking the state and if it has changed print the new result if it has changed
However when I run it I end up with a constant stream of numbers based on where the slider is. If anyone can point out where I went wrong or show me a better way of doing this I would be much obliged.
Since it appears that lp1 and lp2 are local variables, neither is initialized. You assign a value to lp1, and then test whether that is different from the junk in lp2. I'd not be surprised that lp1 doesn't equal lp2.
AWOL "lp" is the output from the potentiometer "lp1" and "lp2" are check numbers for current and previous states. The first block of "if" statements simplifies the output the second prints an output based on it.
PaulS All variables are local. I intended to initialize both "lp1" and "lp2" in that first section though not assign a value to them. The first block assigns a value to "lp1" based on the potentiometers position and the second checks it against "lp2" since the first time "lp2" has no value lp1 != lp2 would evaluate to true and asign a value to "lp2" which would be checked the next time around. Assuming that the potentiometer has not moved it should evaluate to false and since I used &&(AND) not ||(OR) it should not execute the code.
Yes that is what I am using. This is all under the "void loop()" not in a separate function. However we digress from the main question. Is there a way to do what I am trying to do?
C allows variables to initiated without assigning a value.
I think you mean declared.
And, yes it does. But the variable simply is assigned a memory address that possibly contains some junk.
You need to make lp2 static or global. If you make it static, you need to give it some initial value. If you make it global, the compiler will do that for you.
PauS Thank you. I made them global and my problem went away. now I just need to tinker with the sensitivity. However I would appreciate it if you had a better way of doing this.
There are a lot of values for lp (for example, 917 and 918) that are not even handled. lp1 will just keep its old value. If that old value is junk, it will stay junk.