Hello guys, I need help with this project. I have been trying to build an obstacle avoidance robot. it works well, but my problem is that when moving forward, the robot collides with the wall in full speed before it stops and check for obstruction, heres the code below
for the project I'm using Uno R3, L293D, Hc-SR04, 2 IR sensor infront for extra accuracy and slight adjustment of movement
#include <Servo.h>
#include <AFMotor.h>
#define RIGHT A2 // Right IR sensor connected to analog pin A2 of Arduino Uno:
#define LEFT A3 // Left IR sensor connected to analog pin A3 of Arduino Uno:
#define Echo A0
#define Trig A1
#define motor 10
#define Speed 170
#define Speed2 80
#define spoint 90
#define MAX_SPEED 190
bool block;
char value;
int distance;
int Left;
int Right;
int L = 0;
int R = 0;
int L1 = 0;
int R1 = 0;
int speedSet =0;
const int distanceLimit = 20;
unsigned int frontcm; // distance measurements
unsigned int leftcm;
unsigned int rightcm;
unsigned int Right_Value = 0; //Variable to store Right IR sensor value:
unsigned int Left_Value = 0; //Variable to store Left IR sensor value:
Servo servo;
AF_DCMotor M1(1);
AF_DCMotor M2(2);
AF_DCMotor M3(3);
AF_DCMotor M4(4);
void setup() {
Serial.begin(9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
servo.attach(motor);
M1.setSpeed(Speed);
M2.setSpeed(Speed);
M3.setSpeed(Speed);
M4.setSpeed(Speed);
pinMode(RIGHT, INPUT); //set analog pin RIGHT as an input:
pinMode(LEFT, INPUT); //set analog pin RIGHT as an input:
}
void loop() {
// Obstacle Avoidance Code
Right_Value = digitalRead(RIGHT); // read the value from Right IR sensor:
Left_Value = digitalRead(LEFT); // read the value from Left IR sensor:
distance = ultrasonic();
block = (Right_Value == 0 && Left_Value == 0);
if (distance <= 20 || block == true) {
Stop();
delay(200);
backward();
delay(100);
Stop();
L = leftsee();
servo.write(spoint);
delay(800);
R = rightsee();
servo.write(spoint);
delay(800);
if (L < R || R == 0) {
right();
delay(500);
Stop();
delay(200);
} else if (L > R || L ==0) {
left();
delay(500);
Stop();
delay(200);
}
}else if((Right_Value==0) && (Left_Value==1)) { //If the condition is 'true' then the statement below will execute:
Stop();
delay(500);
left();
delay(150);
Stop();
delay(200);
bool lefT = ((Right_Value==0) && (Left_Value==1));
while(lefT != false){
Stop();
delay(500);
left();
delay(150);
Stop();
delay(200);
Right_Value = digitalRead(RIGHT); // read the value from Right IR sensor:
Left_Value = digitalRead(LEFT); // read the value from Left IR sensor:
lefT = ((Right_Value==0) && (Left_Value==1));
}
}else if((Right_Value==1)&&(Left_Value==0)) { //If the condition is 'true' then the statement below will execute:
Stop();
delay(500);
right();
delay(150);
Stop();
delay(200);
bool righT = ((Right_Value==1)&&(Left_Value==0));
while(righT != false) { //If the condition is 'true' then the statement below will execute:
Stop();
delay(500);
right();
delay(150);
Stop();
delay(200);
Right_Value = digitalRead(RIGHT); // read the value from Right IR sensor:
Left_Value = digitalRead(LEFT); // read the value from Left IR sensor:
righT = ((Right_Value==1)&&(Left_Value==0));
}
}else {
forward();
}
}