if the sensor sence an object between 2-23 cm it first turns right for 0.5 sec, takes a short brake
then again looks if their is an object in the intervall, if their is no object (distance>23) it starts moving forward, but if their is still an object their it turns left for a longer time 0.85 sec and now again looks if it is an object their or not and then start the program over.
But when I run the robot and it sense an object it turns right then it turns left, AND THEN it looks if their is no object in front and starts to drive forward again. So it misses that if sats after Turnright.
You have any ideas?
(maybe solved it with an return function, should try it tomorrow)
If the value in distance is greater than 2 and less than 23, see if it is greater than 23. What are the odds of THAT happening?
do you mean that the if sats inside the if sats is dependet of that value? so that the " if(distance>23) "
cant happening because it lay in the "if(distance>2&&distance<23)" ?
do you mean that the if sats inside the if sats is dependet of that value? so that the " if(distance>23) "
cant happening because it lay in the "if(distance>2&&distance<23)" ?
I think you understand what I want to do with the code?
I'm not sure that I do. It appears that you want to do something if the distance is small, and something else (there's a clue) if the distance is not small.
if(distance>2&&distance<23)
{
// Do whatever should happen when there is an obstacle close
}
else
{
// Distance is greater than or equal to 23, so lots of room out front
}
PaulS:
I'm not sure that I do. It appears that you want to do something if the distance is small, and something else (there's a clue) if the distance is not small.
if(distance>2&&distance<23)
{
// Do whatever should happen when there is an obstacle close
}
else
{
// Distance is greater than or equal to 23, so lots of room out front
}
Well, so lets say we have an easy labyrint that first goes straight, then a right turn, straight, then left turn and so on or the other way round.
So it goes straight as long as there is no object in 2-23 cm , then when the wall comes , it shoulds turn right for 0,5 sec and then stop for a short time, then it looks again if the object is still in range ( 2-23 cm, witch it will not be as it was a right turn), if it is not the robot start goin straight again.
Then comes the left turn, and it notice that an object is within 2-23 cm, it starts to turn right for just 0.5 sec and stops for a short while , it looks if there is an object within the interval, but this time the wall is still there as it is a left turn, so now it makes a longer left turn for 0.85 sec, (then it maybe turns a couple of time) but as the left turn is longer it will in the end have no object in front of it start to go straight again and then it have managed to take both right and left turn.
So it goes straight as long as there is no object in 2-23 cm
See, that is where you don't seem to understand how loop() works. The loop() function could (and should) iterate many thousands of times while there is nothing close in front of the bot. What it should do each time it iterates is check the distance. If it is still good, do nothing.
Only if it is not should you do something. That something, whatever it is you decide to do, should block loop() from ending until the operation is complete.
At that point, loop() will be called again, and the distance in front of the bot checked, and used to decide what to do - stop, go forward, turn right, turn left, whatever.
There should NOT be a while loop in loop(). The loop() function already runs in a while loop.
PaulS:
See, that is where you don't seem to understand how loop() works. The loop() function could (and should) iterate many thousands of times while there is nothing close in front of the bot. What it should do each time it iterates is check the distance. If it is still good, do nothing.
Only if it is not should you do something. That something, whatever it is you decide to do, should block loop() from ending until the operation is complete.
At that point, loop() will be called again, and the distance in front of the bot checked, and used to decide what to do - stop, go forward, turn right, turn left, whatever.
There should NOT be a while loop in loop(). The loop() function already runs in a while loop.