Stuck in loop
Hello everyone , I am doing a project and it need to work like this :
first have to follow a black line and catch a product then bring it back to first position, everything work well but during the process i need to add a barrier to the black line so when the robot see the barrier it need to avoid it by passing by the side. I did this with ultrasonic sensor and i write a code for the distance , if the distance is closer than 26cm the robot should turn right , move forward and turn left to follow the line again.I did this with delays sequences but it was not everytime accurate because sometimes was moving much more that it needs and sometimes was working perfect , so to solve this i added an encoder to dc motor to monitor the position and I did a condition like this: when ultrasonic sees the barrier , robot stops for 2 sec then turns rigt for 90 degrees , i did that with while loop and also tried with do-while but did not worked because it was stuck all time in the loop and sometimes never enters the loop. the code was like this:
if(distance<26){
motor stop
delay(2000)
do {
motor move right
}
while(encoder<90deg);
}
else{
follow the line
}
*I wrote other sequences on the main code*
If you need more information i can provide
Thank you