You are on page 1of 3

int enable=2; //Enable pins of L239D are connected to Arduino digital pin2

int motA1=4; //pin-2 of L239D is connected to Arduino digital pin4


int motA2=5; //pin-7 of L239D is connected to Arduino digital pin5
int motB1=6; //pin-10 of L239D is connected to Arduino digital pin6
int motB2=7; //pin-15 of L239D is connected to Arduino digital pin7
int senA1=8; //sensorA first IR LED wire is connected to Arduino digital
pin8
int senA2=9; //sensorA second IR LED wire is connected to Arduino digital
pin9
int senA3=10; //sensorA Photodiode wire is connected to Arduino digital
pin10
int senB1=11; //sensorB first IR LED wire is connected to Arduino digital
pin11
int senB2=12; //sensorB second IR LED wire is connected to Arduino digital
pin12
int senB3=13; //sensorB Photodiode wire is connected to Arduino digital
pin13
int senAread=0; //wire connected between photodiode and 3 K resistor of
sensorA is connected to Arduino analog pinA0
int senBread=1; //wire connected between photodiode and 3 K resistor of
sensorB is connected to Arduino analog pinA1
void setup()
{
pinMode(enable,OUTPUT);
digitalWrite(enable,HIGH);
pinMode(motA1,OUTPUT);
pinMode(motA2,OUTPUT);
pinMode(motB1,OUTPUT);
pinMode(motB2,OUTPUT);
pinMode(senA1,OUTPUT);
digitalWrite(senA1,HIGH);
pinMode(senA2,OUTPUT);
digitalWrite(senA2,HIGH);
pinMode(senA3,OUTPUT);
digitalWrite(senA3,HIGH);
pinMode(senB1,OUTPUT);
digitalWrite(senB1,HIGH);
pinMode(senB2,OUTPUT);
digitalWrite(senB2,HIGH);
pinMode(senB3,OUTPUT);
digitalWrite(senB3,HIGH);
digitalWrite(motA1,HIGH); //
digitalWrite(motA2,LOW); //In default mode both the motors will rotate
forward
digitalWrite(motB1,HIGH); //
digitalWrite(motB2,LOW); //
}
void loop()
{
int val1=analogRead(senAread); // val1 variable stores the readings from
sensorA
int val2=analogRead(senBread); // val2 variable stores the readings from
sensorB
int diff=val1-val2; //calculating the difference of val1 and val2
if(val1<800 || val2<800) //if any obstacle is detected by any one of the
sensor
{
if(diff>=0) //if obstacle is present at right side of the robot
{
digitalWrite(motA2,HIGH); //
digitalWrite(motA1,LOW); //make the bot move backwards
digitalWrite(motB2,HIGH); //
digitalWrite(motB1,LOW); //
delay(1000); //wait for 1 second
digitalWrite(motB2,LOW); //
digitalWrite(motB1,HIGH); //turn left
delay(700); //wait for 700 milli seconds
digitalWrite(motA1,HIGH); //
digitalWrite(motA2,LOW); //move forward
}
else if(diff<0) //if obstacle is present at left side of the robot
{
digitalWrite(motA2,HIGH); //
digitalWrite(motA1,LOW); //make the bot move backwards
digitalWrite(motB2,HIGH); //
digitalWrite(motB1,LOW); //
delay(1000); //wait for one second
digitalWrite(motA2,LOW); //
digitalWrite(motA1,HIGH); //turn right
delay(700); //wait for 700 milli seconds
digitalWrite(motB1,HIGH); //
digitalWrite(motB2,LOW); //move forward
}
}
else //if no obstacle is present
{
digitalWrite(motA1,HIGH); //
digitalWrite(motA2,LOW); //
digitalWrite(motB1,HIGH); // move forward
digitalWrite(motB2,LOW); //
}
}

You might also like