Hey people,
I have written a code for an autonomous obstacle avoiding robot. It uses two TSOP general purpose proximity sensors with digital output. Each sensor module consists of a IR emitter and TSOP receiver pair. One for obstacles on left, and the other for right.
The TSOP receiver detects a signal of fixed frequency and therefore it works very efficiently in ambient lighting also.
It also uses a H-Bridge L293D Motor Controller to control the left and right motors which use 6v each.
I still haven't bought the sensor modules for I am not sure whether it will work or not.
Could you guys please verify the code for me once and tell me will it work or not? Is there anything I would have to change?
// Left Motor Controls
# define L1 4 // Left Positive
# define L2 5 // Left Negetive
// Right Motor Controls
# define R1 6 // Right Positive
# define R2 7 // Right Negetive
// TSOP Sensors
# define Ls A0
# define Rs A1
// Variables to store sensor data
int ls_val;
int rs_val;
// Declaring INPUT and OUTPUT pins
void setup() {
Serial.begin(9600);
pinMode (L1, OUTPUT);
pinMode (L2, OUTPUT);
pinMode (R1, OUTPUT);
pinMode (R2, OUTPUT);
pinMode (Ls, INPUT);
pinMode (Rs, INPUT);
}
void loop() {
ls_val = digitalRead(Ls); // Read data from Left Sensor
rs_val = digitalRead(Rs); // Read data from Right Sensor
if (ls_val != rs_val && ls_val == HIGH) { // If the Left Sensor detects an obstacle go Right
digitalWrite (L1, LOW);
digitalWrite (L2, HIGH);
digitalWrite (R1, HIGH);
digitalWrite (R2, LOW);
Serial.println ("RIGHT");
}
if (ls_val != rs_val && rs_val == HIGH) { // If the Right Sensor detects an obstacle go Left
digitalWrite (L1, LOW);
digitalWrite (L2, HIGH);
digitalWrite (R1, HIGH);
digitalWrite (R2, LOW);
Serial.println ("LEFT");
}
else if (ls_val == LOW && rs_val == LOW) { // If both the sensors dont detect any obstacle go Forward
digitalWrite (L1, HIGH);
digitalWrite (L2, LOW);
digitalWrite (R1, HIGH);
digitalWrite (R2, LOW);
Serial.println ("FORWARD");
}
else { // If both the sensors detect obstacles turn around
digitalWrite (L1, LOW);
digitalWrite (L2, HIGH);
digitalWrite (R1, HIGH);
digitalWrite (R2, LOW);
delay(3000);
Serial.println ("DEAD END");
}
}
Also can I use this (ls_val == HIGH && rs_val == LOW)
instead of (ls_val != rs_val && ls_val == HIGH)
Thanks In Advance,
SolidSnake31295