I am making a Line follower using Arduino Uno, L293D motor shield and three IR sensors on the front. It was not turning for very sharp turns more than 90 degrees. I am testing it on black line in white background. So, I put a while loop to make it turn till its mid sensor senses black and the two side sensors sense white, if two sensors( middle one and one beside it) sense black which means it is a sharp turn. But it is turning slightly for an short instant and getting out of track. I am pasting my code here. Please help.
#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int irl =A0;
int irm =A1;
int irr= A2;
int rl;
int rm,rr;
int wait=600;
void setup()
{
motor1.setSpeed(20);
motor1.run(RELEASE);
motor2.setSpeed(20);
motor2.run(RELEASE);
Serial.begin(115200);
}
void loop()
{
rl=digitalRead(irl); // reading of left sensor
rm=digitalRead(irm);// reading of middle
rr=digitalRead(irr); // reading of right
motor1.setSpeed(70);
motor2.setSpeed(70);
if(rl==0&&rm==1&&rr==0)
{
motor1.run(FORWARD);
motor2.run(FORWARD);
}
else if(rl==1&&rm==0&& rr==0)
{
motor1.run(RELEASE);
motor2.run(FORWARD);
}
else if(rl==1 && rm==1 && rr==0)// FOR SHARP LEFT TURN
{
while(rl!=0&& rm!=1 && rr!=0)
{
motor1.run(RELEASE);
motor2.run(FORWARD);
}
}
else if(rl==0&&rm==0&& rr==1)
{
motor1.run(FORWARD);
motor2.run(RELEASE);
}
else if(rl==0&& rm==1 && rr==1)//FOR SHARP RIGHT TURN
{
while(rl!=0&& rm!=1 && rr!=0)
{
motor1.run(FORWARD);
motor2.run(RELEASE);
}
}
else if(rl==0&&rm==0&&rr==0)
{
motor1.run(FORWARD);
motor2.run(FORWARD);
}
}
because there is nothing within the WHILE loop to update the variables that it is testing.
My strong advice is NOT to use WHILE to solve this problem. It will block the Arduino and make the code a lot more messy and hard to maintain.
I suspect the overshoot is because the thing is moving too fast. I suggest when you detect the sharp corner that you slow down the motors - just like you would do with a real car.
Separately, rather than this style of complex IF statements
if(rl == 0 && rm == 1 && rr == 0)
I suggest you convert the three values into a single number - like this
rl = digitalRead(irl); // reading of left sensor
rm = digitalRead(irm);// reading of middle
rr = digitalRead(irr); // reading of right
combinedSensorValue = rl;
if (rm == 1) {
combinedSensorValue += 2;
}
if (rr == 1) {
combinedSensorValue += 4;
}
This will mean that each of the options for the sensors will produce a unique value in the range 0 to 7 and you can do the same test more simply like this
if (combinedValue == 2) { // the settings of 010 for L, M and R is the binary equivalent of 2)
Actually, the problem is if I don't use a while loop, after turning slightly, all three sensers come on the black track and it stops that's why I used while loop so that it continues rotating until it senses the track.
I tried reading the IR sensors again in the loop , as you said the loop is not taking any new input and I also slowed it for a sharp turn but still it is turning slightly and getting out of the track.
Please suggest what should I do to make it do a turn more than 90 degrees.