Hi, I'm making a rover to navigate an open space and I'm using HRC404 sensors to detect walls and obstacles. Something is going wrong when I'm calling my motor function as it seems to mess up the sensor readings, which work up to that point. After the motor function is called, the sensor readings will constantly stay 0 and not update at all.
void left_motor_forward()
{
digitalWrite(LEFT_MOTORB,HIGH);
digitalWrite(LEFT_MOTORA,LOW);
}
void right_motor_forward()
{
digitalWrite(RIGHT_MOTORB,LOW);
digitalWrite(RIGHT_MOTORA,HIGH);
}
void both_motors_forward()
{
left_motor_forward();
right_motor_forward();
}
void navigation()
{
DistanceL=sensor1();
DistanceR=sensor2();
DistanceF=sensor3();
// Everything is Clear
if (DistanceF >= 4 && DistanceL >= 4 && DistanceR >= 4)
{
x=4;
Serial.println(x);
//k=1024;
both_motors_forward();
delay(20);
Serial.println(x);
Serial.print("k:");
Serial.println(k);
}
}
void loop()
{
if(digitalRead(11)==LOW)
{
delay(500);
i=1;
while (i==1)
{
x=1;
Serial.print("x:");
Serial.println(x);
navigation();
x=3;
Serial.print("x:");
Serial.println(x);
if(digitalRead(11)==LOW)
{
x=0;
i=0;
}
Serial.print("i:");
Serial.println(i);
}
Serial.print("k:");
Serial.println(k);
delay(10000);
}
return;
}
I tried to select the relevant parts and attached the entire sketch. I'm using an IR sensor for the change on pin 11. The excessive print statements are to confirm exactly where the code is going wrong but I'm still not sure what the problem is and how to correct it. Any help would be appreciated.
rovertotal.ino (7.19 KB)