Hey,
I am wrking on a grid solving line follower bot using Arduino(ATmega 328). For line following i am using a 7 sensors sensor array (am using 4 out of 7 sensors). The problem is.... it isnt following what i am specifying in the code.. its taking turns on intersections on its own. I hav tested the sensors, they are wrking fine and sending proper signals to microcontroller. So I guess the prob is with my code.
I am pasting my code here.. kindly check and tell me where i went wrong..
int rtin = 3; //inner right sensor //pin
int rtout = 2; //outer right sensor
int ltin = 6; //inner left sensor
int ltout = 7; //outer left sensor //pin
void loop()
{
if(digitalRead(ltin) == HIGH && digitalRead(rtin) == LOW && digitalRead(rtout) == LOW && digitalRead(ltout) == LOW)
{
do
{
right();
}while(digitalRead(ltin) == HIGH);
forward();
}
else if(digitalRead(rtin) == HIGH && digitalRead(ltin) == LOW && digitalRead(rtout) == LOW && digitalRead(ltout) == LOW)
{
do
{
left();
}while(digitalRead(rtin) == HIGH);
forward();
}
else if(digitalRead(rtin) == HIGH && digitalRead(rtout) == HIGH && digitalRead(ltin) == HIGH && digitalRead(ltout) == HIGH)
{
left();
delay(2000);
// forward();
}
else if(digitalRead(rtin) == HIGH && digitalRead(rtout) == LOW && digitalRead(ltin) == HIGH && digitalRead(ltout) == LOW)
{
forward();
}
}
This code was written to test its line following... I wanted it to adjust itself on the line (first if and else if), turn left on the next intersection (2nd else if) and move forward otherwise.
I tested the forward, left, right commands individually and they are wrking fine.
The prob is instead of following the above commands, the bot is turning left and right alternatively at each junction (moving diagonally across the grid).
Please tell me if there is some problem with my code... or any other thing i should debug.. Tell me if u want some more info.
But i need the answer really soon.. I gotta complete this project by next week..
Thank you.