Reading & comparing multiple digital input signals

Hello to all,I am not a hardcore programer. I have just started basic programming with the help of Google. i would like some programming support to make my line follower operational . I am using arduino UNO with 5 sensors(digital HIGH/LOW output) for the line follower. The code i have written is very simple, with the below code the motors run:

int S1 = 2;
int S2 = 3;
int S3 = 4;
int S4 = 5;
int IN1 = 6;
int IN2 = 7;
int IN3 = 8;
int IN4 = 9;
int EN1 = 10;
int EN2 = 11;
int S5 = 12;

void setup()
{
// declare the LED pins as outputs
pinMode(S1, INPUT) ;// SENSOR1
pinMode(S2, INPUT) ;// SENSOR2
pinMode(S3, INPUT) ;// SENSOR3
pinMode(S4, INPUT) ;// SENSOR4
pinMode(S5, INPUT) ;// SENSOR5
pinMode(IN1,OUTPUT); //input 1
pinMode(IN2,OUTPUT); //input 2
pinMode(IN3,OUTPUT); //input 3
pinMode(IN4,OUTPUT); //input 4
pinMode(EN1,OUTPUT); //pwm en 3,4
pinMode(EN2,OUTPUT); //pwm en 1,2
pinMode(13,OUTPUT);
}

void loop()
{
if (digitalRead(S1) == LOW )
{
analogWrite(EN1, 255);
analogWrite(EN2, 255);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay(500);
}
if (digitalRead(S1) == HIGH )
{
analogWrite(EN1, 255);
analogWrite(EN2, 255);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(500);
}
}

but when i change the code it doesn't do any thing

void loop()
{
if (digitalRead(S1) == LOW && digitalRead(S2) == LOW && digitalRead(S3) == HIGH && digitalRead(S4) == LOW && digitalRead(S5) == LOW)
{
analogWrite(EN1, 255);
analogWrite(EN2, 255);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(500);
}
else if (digitalRead(S1) == HIGH )
{
analogWrite(EN1, 255);
analogWrite(EN2, 255);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(500);
}

the code verifies without any errors but nothing happens, Any advices.

Thanks :slight_smile:

Well my guess would be that S1 is LOW but one of the other conditions in that if statement isn't. When they're combined with && then they all have to be true. If you want for any one of those conditions by itself to trigger the code then use OR || instead of AND &&.

In my case

if (digitalRead(S1) == LOW && digitalRead(S2) == LOW && digitalRead(S3) == HIGH && digitalRead(S4) == LOW && digitalRead(S5) == LOW)

all the conditions should be true that is when the robot will decide whether to go straight or take left right . Is there any simple way to use (digital HIGH/LOW output) sensors to make line a follower, I tried Google for solution but id didn't help me.

Is there any simple way to use (digital HIGH/LOW output) sensors to make line a follower

Yes.

Space 2 digital sensors wider apart than the width of the line. Arrange the circuit and the program so that the sensors return HIGH when not over the line and LOW when over the line.

Start both motors in the same direction and speed.

If the left sensor reads LOW the robot has swung to the right so slow down (or stop) the left motor until the left sensor reads HIGH then resume running at equal speeds.

If the right sensor reads LOW the robot has swung to the left so slow down (or stop) the right motor until the right sensor reads HIGH then resume running at equal speeds.

This will give you very crude line following. It can be vastly improved but you did say that you wanted a simple way.

ksheer:
In my case

if (digitalRead(S1) == LOW && digitalRead(S2) == LOW && digitalRead(S3) == HIGH && digitalRead(S4) == LOW && digitalRead(S5) == LOW)

all the conditions should be true that is when the robot will decide whether to go straight or take left right . Is there any simple way to use (digital HIGH/LOW output) sensors to make line a follower, I tried Google for solution but id didn't help me.

Should be true and actually are true are maybe two different things. Try some Serial.print lines in there to see what those pins are actually reading.