If logic not working?

Ive set myself a project intented to get me more comftorable with the development process. The game is to control 3 LEDs to run in a loop in as many ways as possible. eg, loop on off, button, potentiometer etc.

In this example im trying to get LED0 to turn on as the potentiometer value drops, LED1 when its not moved, and LED2 as the value rises.

Ive copied my code and the serial monitor readout where you can see that at points 'PotRead' is > 'PotRead2' and vice versa yet the variable 'LED' doesn't update.

'LED' seems to only change when the potentiometer is turned to the lowest value, where it will flash between 2 and 3.

Im entirely self taught so Im sorry if my code is a mess, tell me how to improve. Also I understand that due to noise I dont have a stable idle value but I can address that later.

int LED;
const int LED0 = 2; //pin lables
const int LED1 = 3;
const int LED2 = 4;


void setup() {
  Serial.begin(9600);
  delay(10);
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(A0, INPUT);

}

void loop() {

  //find if the Potentiometer value is rising, dropping or staying the same
  int PotRead = analogRead(A0);  
  delay(100);
  int PotRead2 = analogRead(A0);

      Serial.print(PotRead); 
      Serial.print(" , ");
      Serial.print(PotRead2);
      Serial.print(" , ");

  //create a delay if the LED changes, for development purpose
  int LEDState = LED; 

   //using imput decide which LED to activate
  if (PotRead > PotRead2) LED = 2;  
  if (PotRead = PotRead2) LED = 1;
  if (PotRead < PotRead2) LED = 0;
  
  if (LEDState != LED) delay(200); //delay on LED state change

      Serial.print(LED);
      Serial.println("");

  switch (LED) //change LEDs state
    {
    case 0:
    digitalWrite(LED0, HIGH); digitalWrite(LED1, LOW); digitalWrite(LED2, LOW);
    break;

    case 1:
    digitalWrite(LED0, LOW); digitalWrite(LED1, HIGH); digitalWrite(LED2, LOW);  
    break;

    case 2:
    digitalWrite(LED0, LOW); digitalWrite(LED1, LOW); digitalWrite(LED2, HIGH);
    break; 
    }
  }

SM readout when knob is wiggled.
PotRead , PotRead2 , LED

475 , 474 , 2
438 , 354 , 2
304 , 220 , 2
189 , 79 , 2
0 , 5 , 2
83 , 289 , 2
470 , 613 , 2
744 , 750 , 2
630 , 504 , 2

For when turned all the way down

5 , 3 , 2
3 , 0 , 3
7 , 2 , 2
0 , 6 , 2
1 , 0 , 3

Im a beginner at this so if my method here is unconvetional, I'd be happy to hear of other ways.

Use '==' for comparison

please note the difference between "=" and "=="

= - assignment
== comparision

Even if you fix the "=" this may not work the way you expect because the ADC values have noise, it is seldom that you would read two identical values back to back.

You need to detect when it is within a small range instead.

Method left as an exercise for the student. :slight_smile:

BTW, I don't understand why this code outputs 2 (as OP wrote):

As I think it should always produce 1, because the code is equivalent to:

 if (PotRead > PotRead2) LED = 2;  
 PotRead = PotRead2;
 if (PotRead) LED = 1;
 if (PotRead < PotRead2) LED = 0;

may be OP make a typo when upload the code to the forum?

Unless 'PotRead2' is zero. Then you get:

 if (PotRead > 0) LED = 2;  
 PotRead = 0;
 if (0 != 0) LED = 1;  // never true
 if (0 < 0) LED = 0;  // never true

If PotRead > 0, LED should be set to 2.
If PotRead == 0, LED is not changed.

why the code


if (PotRead) LED = 1;

you interpreted as

The 'if' statement takes a boolean (true or false) value. Integer values are converted to boolean values by comparing to zero. Non-zero values are 'true' and zero values are 'false'.
`if (intValue)'
is equivalent to:
'if (intValue != 0)'

1 Like

thanks, I understood

So i was right, in most cases (unless PotRead2 == 0),
the code from post#1 should returns LED =1

But it is not the case, as I see in the OP output:

So I think that OP showed not the same code as run on the arduino

Both pots on A0 , how is the pots wired?

As i understand, OP used only one pot. He just read it twice with 100msec delay between

1 Like

Ok the == thing made it work, Thank you all, I really appricate everyone getting involved.

This is true

There is a chance its not the exact same but the only changes will be delays and at one point I swapped the order of PotRead and PotRead2, I was trying random stuff but thought it was the same. I'm sorry if you've now got a headache cause the code and SM were from different runs. I'll be very careful to post the exact same ones next time.

99.9% of if not working threads is this ^^^

... and if compiler warnings are enabled, it will be flagged as a warning.

Now you are being unreasonable

1 Like

That can't be true!

I think about 25% are:

  if (PotRead == PotRead2); 
    LED = 1;

:slight_smile:

touché

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.