I built a small robotic car with an ultrasonic sensor on it. (I followed this tutorial : http://educ8s.tv/arduino-robot-easy-diy-project/ ) and the full source code is underneath it (you will have to download)
I modified the code as I want the robot to stop when there is a object placed in front of it, but if the object quickly moves away, for the robot to keep on going forward.
Here is my attempt inside of the void loop :
void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);
distance = readPing();
if (distance >= 15){
while(distance>= 15){
moveForward();
distance = readPing();
}
}
if(distance<=15)
{
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distance = readPing();
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
distance = readPing();
if (distance >= 15){
while(distance >= 15){
moveForward();
distance = readPing();
}
}
if(distanceR>=distanceL)
{
turnRight();
}else
{
turnLeft();
}
}else
{
moveForward();
}
distance = readPing();``
}
This code works initially to stop the robot and then it keeps going, but the second time you put your hand in front of it and keeping it there, it turns but then suddenly stops. (I am trying to test that the first time an object quickly moves away and the second time it stays there.)
To me it seems that the code stops looping after I have the robot stop for the first time and runs the rest of the void loop but then stops because in the beginning I have the robot test the distance of any objects in front of it but it still doesn't move.
Any help will be appreciated.