Help with program, can't figure it out!

Code is meant to turn on an led (will be a liquid pump for soda when built IRL) for
a certain period of time depending on where the potentiometer
is set. led2 always turns on after either led1 or led3 are activated
even if the value from the potentiometer doesn't fall into led2's
range. I've tried multiple fixes, nothing has worked. The code is pasted below.

int sensorValue = 0;
int pot = A0;
int led1 = 13;
int led2 = 8;
int led3 = 3;
int buttonState = 0;
int button = 6;

void setup()
{

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pot, INPUT);
pinMode(button, INPUT);

Serial.begin(9600);

}

void loop()
{

sensorValue = analogRead(pot);
buttonState = digitalRead(button);
Serial.println(sensorValue);

if(sensorValue <= 500){
if (buttonState == HIGH) {
Serial.println("Small Soda");
digitalWrite(led1, HIGH);
delay(2000);
digitalWrite(led1, LOW);
}
}

if (500 < sensorValue < 900); {
if(buttonState == HIGH) {
Serial.println("Medium Soda");
digitalWrite(led2, HIGH);
delay(3000);
digitalWrite(led2, LOW);
}
}

if(sensorValue >= 900){
if (buttonState == HIGH){
Serial.println("Large Soda");
digitalWrite(led3, HIGH);
delay(4000);
digitalWrite(led3, LOW);
}
}

}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

lose the semicolon on this line:

  if (500 < sensorValue < 900);
  if (500 < sensorValue < 900);

The semicolon on the end of the line is the only code dependent on the test. Delete it

Deleted it, the problem still persists.

what do you expect this condition to do?

if (500 < sensorValue < 900) {...}

(hint: it's not what you expect)

Well, I expected it to make it so the if statement only happens if the sensor value falls between 800 and 900. I'm guessing that's wrong? Do you have a fix for that?

Edit: I was able to fix the problem by using an && statement, I should have thought of that sooner.

All OK now ?

:clap:

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