If statement not working!

if statement not working can someone explain?
this is my code:
int Relay1 = 22;

int Relay2 = 23;

int Relay3 = 24;

int Relay4 = 25;

int PotPin1 = 2;

int Val1 = 0;

int Delay1 = 100;

void setup() {

Serial.begin(9600);

pinMode(Relay1, OUTPUT);

pinMode(Relay2, OUTPUT);

pinMode(Relay3, OUTPUT);

pinMode(Relay4, OUTPUT);

}

void loop() {

Val1 = analogRead(PotPin1);

Serial.println(Val1);

if (Val1<= 170);{

digitalWrite(Relay1, HIGH);

delay(Delay1);

digitalWrite(Relay2, HIGH);

delay(Delay1);

digitalWrite(Relay3, HIGH);

delay(Delay1);

digitalWrite(Relay4, HIGH);

delay(Delay1);

}

if ((Val1 >=170) && (Val1<= 340));{

digitalWrite(Relay1, LOW);

delay(Delay1);

digitalWrite(Relay2, HIGH);

delay(Delay1);

digitalWrite(Relay3, HIGH);

delay(Delay1);

digitalWrite(Relay4, HIGH);

delay(Delay1);

}

if ((Val1 >=340) && (Val1<= 510));{

digitalWrite(Relay1, HIGH);

delay(Delay1);

digitalWrite(Relay2, LOW);

delay(Delay1);

digitalWrite(Relay3, HIGH);

delay(Delay1);

digitalWrite(Relay4, HIGH);

delay(Delay1);

}

if ((Val1 >=510) && (Val1<= 680));{

digitalWrite(Relay1, HIGH);

delay(Delay1);

digitalWrite(Relay2, HIGH);

delay(Delay1);

digitalWrite(Relay3, LOW);

delay(Delay1);

digitalWrite(Relay4, HIGH);

delay(Delay1);

}

if ((Val1 >=680) && (Val1<= 840));{

digitalWrite(Relay1, HIGH);

delay(Delay1);

digitalWrite(Relay2, HIGH);

delay(Delay1);

digitalWrite(Relay3, HIGH);

delay(Delay1);

digitalWrite(Relay4, LOW);

delay(Delay1);

}

if (Val1 >=840);{

digitalWrite(Relay1, LOW);

delay(Delay1);

digitalWrite(Relay2, LOW);

delay(Delay1);

digitalWrite(Relay3, LOW);

delay(Delay1);

digitalWrite(Relay4, LOW);

delay(Delay1);

}

}

It is because you have ended the if statements with a semicolon and that is the only code dependant on the result of the test and code after it will be executed unconditionally

1 Like

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
https://forum.arduino.cc/index.php?topic=712198.0
Then look down to "code problems" about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Note ;
All your if statements are like this

if ((Val1 >=170) && (Val1<= 340));{

They should be like this.

if ((Val1 >=170) && (Val1<= 340)) {

You need to remove the semicolon.

Tom... :grinning: :+1: :coffee: :australia:

There are reference documents for a lot of Arduino stuff.

A Google search on 'Arduino if' points to this page;

If Reference

Which shows the correct format for the if.

This is not unique to Arduino. This is C syntax, and often catches students. It is more obvious if the opening curly brace starts on the next line, a style sometimes seen.

The semicolon is a "scope terminator". The IF statement has ended. The curly braces might lift the stack and foster variable definitions but may as well not be there.

You will see code with one line IF statements. Sadly Arduino lends itself to this because we are not stepping through the code to debug so reducing the lines can be a focus.

After a decade or two we should have had a simulator.

Please share where you learned this.

a7

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