loading
This is for beginners
prequisite-
knowledge of sr04
l293d

Step 1: Connections

First do the wiring as shown in figure

Step 2: Writing Code

int trigPin=8;
int echoPin=9;
int a=3;
int b=2;
int c=5;
int d=4;
float distance;
float pingTime;
float speedOfSound=347.22;
float lDistance;
float rDistance;

void setup(){
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(echoPin,INPUT);
}

float meaSure()
{

digitalWrite(trigPin,LOW);
delayMicroseconds(2000);

digitalWrite(trigPin,HIGH);
delayMicroseconds(10);

digitalWrite(trigPin,LOW);
pingTime=pulseIn(echoPin,HIGH);
distance=(speedOfSound*pingTime)/20000;//in cm
return distance;
}



void forward(){

digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
delay(500);
stopp();

}
void backward(){
digitalWrite(b,HIGH);
digitalWrite(a,LOW);
digitalWrite(d,HIGH);
digitalWrite(c,LOW);
delay(1000);
stopp();

}
void left(){
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(d,HIGH);
digitalWrite(c,LOW);
delay(570);
stopp();

}
void right(){
digitalWrite(b,HIGH);
digitalWrite(a,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
delay(1000);
stopp();

}
void stopp(){
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
Serial.println("stopp");
}





void loop(){
//measure distance
digitalWrite(trigPin,LOW);
delayMicroseconds(2000);

digitalWrite(trigPin,HIGH);
delayMicroseconds(10);

digitalWrite(trigPin,LOW);
pingTime=pulseIn(echoPin,HIGH);
distance=(speedOfSound*pingTime)/20000;//in cm
if(distance>20){
forward();
Serial.println("No obstackle");
}
if(distance<=20){
stopp();
Serial.println("Checking Left");
left();
delay(250);

lDistance= meaSure() ;
Serial.println("Checking Right");
right();
delay(250);
rDistance=meaSure();
if(rDistance>lDistance && rDistance>20){
forward();
Serial.println("Moving forward in right path");
}
if(rDistance<=lDistance && lDistance>20){
right();
delay(250);
forward();
Serial.println("Moving forward in left path");
}
else{
Serial.println("In cave");
Serial.println("Going Back and checking again");
backward();
stopp();
left();
delay(250);
lDistance=meaSure();
right();
delay(250);
rDistance=meaSure();
if(rDistance>lDistance && rDistance>20){
forward();
}
if(rDistance<=lDistance && lDistance>20){
right();
delay(250);
forward();
}
}
}
}
<p>Nice robot, thanks for sharing!</p>

About This Instructable

179views

0favorites

License:

More by ArunS35:OBSTACLE AVOIDING BOT 
Add instructable to: