Program the Rover to move around the room using the ultrasonic sensor to avoid running into obstacles. If the Rover detects an object, turn away from it and keep moving forward.
Setup
A small obstacle course with flat surfaces like boxes and books works well for the Rover to detect. The ultrasonic sensor works best against flat, hard surfaces, angled or soft surfaces are harder to detect because the sensor relies on reflected sound waves.
Our Approach
Stage 1: Detect an Obstacle
Use an IF statement with a logic comparison to check if the ultrasonic sensor detects an obstacle in front of the Rover.
Stage 2: Turn or Drive Forward
Use the IF / ELSE block to turn when an obstacle is detected and drive forward when it isn’t, both with motor blocks.
Stage 3: Loop Forever
Wrap everything in a While True loop so the behaviour repeats forever.

Example code, obstacle-avoiding Rover
Continue Learning
Related resourceActivity: Puppy BotBranching and looping with the ultrasonic sensor.


