Hi iam making a line following robot using a arduino uno,l298N motor driver and three IR sensors that that outputs HIGH or LOW.
this is the first state in wich i start the robot irL==1 && irM==0 && irR==1
but when the robot moves and the State changes to irL==0 && irM==0 && irR==1 the robot takes about 5 seconds to change its direction so it has already missed the line.
int mr1=2; //motor right 1
int mr2=3; //motor right 2
int mL1=4; //motor left 1
int mL2=5; //motor left 2
void setup() {
pinMode (mr1,OUTPUT);
pinMode (mr2,OUTPUT);
pinMode (mL1,OUTPUT);
pinMode (mL2,OUTPUT);
pinMode (A2,INPUT);
pinMode (A1,INPUT);
pinMode (A0,INPUT);
pinMode (A3,INPUT);
Serial.begin(115200);
}
void loop() {
int irL=digitalRead (A2); // ir sensor left
int irM=digitalRead(A1); //ir sensor middle
int irR=digitalRead(A0); // ir sensor right
if(irL==1 && irM==0 && irR==1){
digitalWrite(mr1,HIGH);//Right motor in1 high
digitalWrite(mr2,LOW);//RIGht motor in2 Low
digitalWrite(mL1,HIGH);//Left Motor in1 High
digitalWrite(mL2,LOW);//Left Motor in2 low
Serial.println("Forward");
}
else if (irL==0 && irM==0 && irR==1){
digitalWrite(mr1,HIGH);// right motor in1 high
digitalWrite (mr2,LOW); // right motor in2 low
digitalWrite (mL1,LOW); // left motor in1 low
digitalWrite (mL2,HIGH); // left motor in2 high
Serial.println("90 left");
}
else if (irL==1 && irM==0 && irR==0){
digitalWrite(mr1,LOW);///right motor in1 Low
digitalWrite(mr2,HIGH);//right motor in2 HIGH
digitalWrite(mL1,HIGH);// Left motor in1 High
digitalWrite(mL2,LOW);//Left motor in2 LOw
Serial.println("90 right");
}
else if (irL==1 && irM==1 && irR==0){
digitalWrite (mr1,LOW); //right motor in1 high
digitalWrite (mr2,HIGH); // right motor in2 low
digitalWrite (mL1,HIGH); // left motor in1 low
digitalWrite (mL2,LOW); // left motor in2 high
Serial.println("right");
}
else if (irL==0 && irM==1 && irR==1){
digitalWrite (mr1,HIGH); //right motor in1 high
digitalWrite (mr2,LOW); // right motor in2 low
digitalWrite (mL1,LOW); // left motor in1 low
digitalWrite (mL2,HIGH); // left motor in2 high
Serial.println("left");
}
else if(irL==1 && irM==1 &&irR==1){
digitalWrite(mr1,HIGH);//Right motor in1 high
digitalWrite(mr2,HIGH);//RIGht motor in2 Low
digitalWrite(mL1,HIGH);//Left Motor in1 High
digitalWrite(mL2,HIGH);//Left Motor in2 low
Serial.println("Stop");
}
}