I’m having trouble with two if statements in my code. My project is basically a mega-line crawler which drives around the track and is made from an RC car and Arduino sensors. The RC car follows the lines on the track through 3 IR sensors. It seems to be working pretty well, but the code doesn’t fully work. I’ve programmed it to turn left when the right-most IR sensor turns goes from LOW to HIGH (that is stops receiving IR light bounced off the white track line) and turn even farther left if the two sensors on the right turn to HIGH. This is also set up for the opposite (turning right when the left sensor is HIGH) But, for some reason the second statement, that should turn the wheels farther if the car is farther off the line (if two IR sensors turn to HIGH), doesn’t work for either turning right or left. That is, the if statement doesn’t trip. The 1st and 2nd if statements are very similar, so I can’t figure out why it doesn’t work.
#include <Servo.h>
// DON’T TOUCH THIS:
Servo sspeed;
Servo ssteer;
int GND = 5;
int PWR = 6;
int SIG = 7;
int GND2 = 11;
int PWR2 = 12;
int SIG2 = 13;
int IR1 = 2;
int IR2 = 3;
int IR3 = 4;
//TOUCH THIS
int carspeed = 80; //SPEED of the car
int angle = 96; //INITIAL ANGLE of steering, 96 appears to be straight
//DON’T TOUCH SETUP
void setup() {
pinMode(GND, OUTPUT);
pinMode(PWR, OUTPUT);
pinMode(GND2, OUTPUT);
pinMode(PWR2, OUTPUT);
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
pinMode(IR3, INPUT);
sspeed.attach(SIG2); //Attach the servo to pin SIG
ssteer.attach(SIG);
sspeed.write(carspeed); //write starting throttle position
ssteer.write(angle);
Serial.begin(9600);
}
void loop() {
//DON’t TOUCH THIS
digitalWrite(PWR, HIGH); // V+ for the header
digitalWrite(GND, LOW); //GND for the header
digitalWrite(PWR2, HIGH); // V+ for the header
digitalWrite(GND2, LOW); //GND for the header
sspeed.write(carspeed);
ssteer.write(angle);
// TOUCH THIS - ONLY ADJUST the numbers after angle -/+=
if (0 == digitalRead(IR1) == digitalRead(IR2) == digitalRead(IR3)) // ON TRACK? GO STRAIGHT
{
angle = angle;
ssteer.write(angle);
}
else if (1 == digitalRead(IR1) and 0 == digitalRead(IR2) and 0 == digitalRead(IR3)) // TURN LEFT
{
angle -=4;
ssteer.write(angle);
}
else if (1 == digitalRead(IR1) and 1 == digitalRead(IR2) and 0 == digitalRead(IR3)) // DOES NOT WORK - NEVER trips (Steering never adjusts by 7)
{
angle -=7;
ssteer.write(angle);
}
else if (1 == digitalRead(IR3) and 1 == digitalRead(IR1) and 1 == digitalRead(IR2)) //IF OFF LINE,STOP
{
angle = angle;
sspeed.write(angle);
}
else if (0 == digitalRead(IR1) and 0 == digitalRead(IR2) and 1 == digitalRead(IR3)) //TURN RIGHT
{
angle +=4;
ssteer.write(angle);
}
else if (0 == digitalRead(IR1) and 1 == digitalRead(IR2) and 1 == digitalRead(IR3)) // DOES NOT WORK - NEVER trips (Steering never adjusts by 7)
{
angle +=7;
ssteer.write(angle);
}
else // CONFUSED? GO STRAIGHT and STOP
{
ssteer.write(angle);
}
delay(100);
}