Conditional Statement Help Request

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

  int lp = Esplora.readSlider();
  int lp1;
  int lp2;

  if (lp > 918) lp1 = 1;
  if (lp < 917 && lp > 816 ) lp1 = 2;
  if (lp < 815 && lp > 714) lp1 = 3;
  if (lp < 713 && lp > 612) lp1 = 4;
  if (lp < 611 && lp > 510) lp1 = 5;
  if (lp < 509 && lp > 408) lp1 = 6;
  if (lp < 407 && lp > 306) lp1 = 7;
  if (lp < 305 && lp > 204) lp1 = 8;
  if (lp < 203 && lp > 102) lp1 = 9;
  if (lp < 101) lp1 = 10;

  if (lp1 != lp2 , lp1 == 1) { Keyboard.write('1'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 2) { Keyboard.write('2'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 3) { Keyboard.write('3'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 4) { Keyboard.write('4'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 5) { Keyboard.write('5'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 6) { Keyboard.write('6'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 7) { Keyboard.write('7'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 8) { Keyboard.write('8'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 9) { Keyboard.write('9'); lp2 = lp1; };
  if (lp1 != lp2 , lp1 == 10) { Keyboard.write('0'); lp2 = lp1; };

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.

  if (lp1 != lp2 , lp1 == 1) { Keyboard.write('1'); lp2 = lp1; };

The comma operator is most certainly not the one you want to use. Which one you do want is not at all clear.

I am trying to make it so I can check both values and if both are true execute the code. I have tried && and& with no change.

&& is the correct operator. You need to post the code that uses &&.

Here it is.

  int lp = Esplora.readSlider();
  int lp1;
  int lp2;

  if (lp > 918) lp1 = 1;
  if (lp < 917 && lp > 816 ) lp1 = 2;
  if (lp < 815 && lp > 714) lp1 = 3;
  if (lp < 713 && lp > 612) lp1 = 4;
  if (lp < 611 && lp > 510) lp1 = 5;
  if (lp < 509 && lp > 408) lp1 = 6;
  if (lp < 407 && lp > 306) lp1 = 7;
  if (lp < 305 && lp > 204) lp1 = 8;
  if (lp < 203 && lp > 102) lp1 = 9;
  if (lp < 101) lp1 = 10;

  if (lp1 != lp2 && lp1 == 1) { Keyboard.write('1'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 2) { Keyboard.write('2'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 3) { Keyboard.write('3'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 4) { Keyboard.write('4'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 5) { Keyboard.write('5'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 6) { Keyboard.write('6'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 7) { Keyboard.write('7'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 8) { Keyboard.write('8'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 9) { Keyboard.write('9'); lp2 = lp1; };
  if (lp1 != lp2 && lp1 == 10) { Keyboard.write('0'); lp2 = lp1; };

tested it and here was no change.

lp2?

You mean "here is some of it".

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.

Now, if lp2 was global or static...

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.

since the first time "lp2" has no value

How does that work?

C allows variables to initiated without assigning a value.

Really?
Automatics too?

What do you mean “automatics?”

Local, non-static variables.

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.

However I would appreciate it if you had a better way of doing this.

  if (lp > 918) lp1 = 1;
  if (lp < 917 && lp > 816 ) lp1 = 2;
  if (lp < 815 && lp > 714) lp1 = 3;
  if (lp < 713 && lp > 612) lp1 = 4;
  if (lp < 611 && lp > 510) lp1 = 5;
  if (lp < 509 && lp > 408) lp1 = 6;
  if (lp < 407 && lp > 306) lp1 = 7;
  if (lp < 305 && lp > 204) lp1 = 8;
  if (lp < 203 && lp > 102) lp1 = 9;
  if (lp < 101) lp1 = 10;

How many of those if tests can evaluate to true, for any value of lp? Using else if statements would make the code a lot simpler.

If the ranges are relatively constant sizes, the map() function will do that in one step.

Maybe later. If you could post some ideas in a reply it would appreciated.

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.