Instead of moving the robot with hardcoded steps, we want to use feedback from the ultrasonic sensor to decide how to move the robot. This can be done with the loop function. At every loop we get the distance of the top wall using the ultrasonic sensor. And then we make a decision based on that distance on how to move the servos of the robot. For example, a starting strategy would be: if the distance is greater than 110 then move left. If less than 110 move right.

Exercise

The coins are around 110 cm away from the top wall. Use the data from the ultrasonic sensor to navigate the robot to collect both coins.

HINT

If you just rotate the robot left and right, you will not be getting closer or farther away from the wall. You will just be changing the direction of where the robot is pointing to. If you want to get closer to the wall, a good strategy to start with is:

Rotate the robot to the left
Move the robot forward for few milliseconds
Rotate the robot back to the right

After this, the robot will be moved closer to the wall while staying parallel with the wall.