---
title: "Activity: Roaming Rover"
date: "2021-06-03"
categories: ["Activities", "All"]
tags: ["Iteration", "Ultrasonic", "Motors", "Beginner"]
excerpt: "Learn to avoid obstacles with the ultrasonic sensor."
featuredImage: "/images/resources/roaming-rover.png"
---


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 the object and keep moving forward.

####
  Relevant Coding Skills

    Branching

	--&gt;

    Iteration

    Functions

    Variables

	--&gt;

    Algorithm Design

  --&gt;

    Maths

####
   Relevant Rover Concepts

    Ultrasonic

    IR

	 --&gt;

    Colour

    Gyroscope

    Accelerometer

    Motors

    LEDs

	--&gt;

    Buzzer

 	--&gt;

Activity Demonstration

### Setup

When running the exercise, it can often work well to have a small obstacle course setup with flat surfaces like boxes and books for the rover to detect. The ultrasonic sensor works best against flat hard surfaces as it is detecting reflected sound waves. Angled or soft surfaces are harder to detect.

### Here’s Our Approach

#### **Stage 1**

Use an IF statement block with a logic comparison block to check if the ultrasonic sensor detects an obstacle in front of the rover.

**Stage 2**

Use the IF ELSE block to turn when an obstacle is detected and drive forward when it is not detected using motor blocks.

#### **Stage 3**

Repeat this behaviour by wrapping everything in a while loop. While true will repeat forever.

![](/images/content/6bfaf0-screenshot-2023-10-31-083156.png)

Example Code
