#define IR1 2 // the sensor at the front of the robot
#define IR2 3 // the sensor at the back of the robot
#define IR3 4 // the sensor at the right side of the robot
#define IR4 5 // the sensor at the left side of the robot
#define right_motor1 6
#define right_motor2 7
#define left_motor1 8
#define left_motor2 9
#define right_motor_EN 10
#define left_motor_EN 11
int signal1 =0; // the signal recieved from the first IR (front) sensor
int signal2 =0; // the signal recieved from the secend IR (back) sensor
int signal3 =0; // the signal recieved from the third IR (right) sensor
int signal4 =0; // the signal recieved from the fourth IR (left) sensor
void setup() {
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
pinMode(IR3, INPUT);
pinMode(IR4, INPUT);
pinMode(right_motor1, OUTPUT);
pinMode(right_motor2, OUTPUT);
pinMode(left_motor1, OUTPUT);
pinMode(left_motor2, OUTPUT);
pinMode(right_motor_EN, OUTPUT);
pinMode(left_motor_EN, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(right_motor_EN, HIGH); // set the value of the enable of the right motor to high (like we turn the motor on)
digitalWrite(left_motor_EN, HIGH); // set the value of the enable of the left motor to high (like we turn the motor on)
signal1 = digitalRead(IR1); // read the signal of the first IR (front) sensor
signal2 = digitalRead(IR2); // read the signal of the second IR (back) sensor
signal3 = digitalRead(IR3); // read the signal of the third IR (right) sensor
signal4 = digitalRead(IR4); // read the signal of the fourth IR (left) sensor
Serial.println(signal1); // print the data from the first sensor on the serial monitor
Serial.println(signal2); // print the data from the secend sensor on the serial monitor
Serial.println(signal3); // print the data from the third sensor on the serial monitor
Serial.println(signal4); // print the data from the fourth sensor on the serial monitor
// we will give the priority to the right turns (right hand algorithm)
switch (signal1){
case HIGH :
switch (signal2){
case HIGH :
switch (signal3){
case HIGH :
digitalWrite(right_motor1, HIGH);
digitalWrite(right_motor2, LOW);
digitalWrite(left_motor1, LOW);
digitalWrite(right_motor1, HIGH); // this block of code let the robot turn right
break ;
case LOW :
switch (signal4){
case HIGH :
digitalWrite(right_motor1, LOW);
digitalWrite(right_motor2, HIGH);
digitalWrite(left_motor1, HIGH);
digitalWrite(right_motor1, LOW); // this block of code let the robot turn left
break ;
case LOW :
digitalWrite(right_motor1, HIGH);
digitalWrite(right_motor2, LOW);
digitalWrite(left_motor1, HIGH);
digitalWrite(right_motor1, LOW); // this block of code let the robot move forward
break ;
}
}
}
case LOW :
switch (signal2){
case HIGH :
switch (signal3){
case HIGH :
digitalWrite(right_motor1, HIGH);
digitalWrite(right_motor2, LOW);
digitalWrite(left_motor1, LOW);
digitalWrite(right_motor1, HIGH); // this block of code let the robot turn right
break ;
case LOW :
switch (signal4){
case HIGH :
digitalWrite(right_motor1, LOW);
digitalWrite(right_motor2, HIGH);
digitalWrite(left_motor1, HIGH);
digitalWrite(right_motor1, LOW); // this block of code let the robot turn left
break;
case LOW :
while(signal2==HIGH^signal1==LOW) {
digitalWrite(right_motor1, HIGH);
digitalWrite(right_motor2, LOW);
digitalWrite(left_motor1, HIGH);
digitalWrite(right_motor1, LOW); // this block of code let the robot move forward in case of the robot comes into a cut
}
do {
digitalWrite(right_motor1, HIGH);
digitalWrite(right_motor2, LOW);
digitalWrite(left_motor1, LOW);
digitalWrite(right_motor1, HIGH); // this block of code let the robot turn right
}
while(signal1==LOW);
break ; // this piece of code let the robot turn around but Im not sure
}
}
}
default :
digitalWrite(right_motor1, LOW);
digitalWrite(right_motor2, LOW);
digitalWrite(left_motor1, LOW);
digitalWrite(right_motor1, LOW); // this block of code let the robot stop
break ;
}
}