# Exercise 3: Navigate

Now we would like to put everything together and use sensor feedback to perform a navigation task. The idea is to begin formulating strategies for solving programming tasks. Here we will also learn to use functions that incorporate feedback from the environment to accomplish a task.

# Task: Stop moving

In this exercise you will program your robot to detect an obstacle at some distance, say 50cm. Now that you know how to convert sonic pulses into distances, you can do this.

Goal: Modify a program to detect an object at a certain distance. If detected stop moving.

Let's look at how functions work first to give us some insight into how to accomplish tasks more efficiently and logically.

# Prep: Functions

So far we have seen and used functions from an Arduino library. A function is a block of code that runs only when we make a call to that function. We can pass data (parameters) to the function. Using functions is a good practice to be able to effectively reuse code.

# Declaring a Function

We will start with a simple function:

void functionName()
    //code inside the function

The above code declares a function named functionName. A function declaration starts with a type. This is called the return type of the function. However, specifying the return type to be void means this function will not return anything. Whenever we call this function, it will only run the code inside of it.

After the return type, we specify the name of the function. We need to give the function a name so that we can use it afterwards.

To specify that this is a function, we need to use ( ) after the function name and then start the scope of the function {.

Inside this scope we will define what we want this function to do each time we call it.

# Calling a Function

We use the name of the function followed by () to call the function and execute the code block inside of it.

Warmup problem

The code below declares a function sayHello that prints Hello to the Serial. Inside the loop function we are making a call to our function and then waiting for 1 second. Print something else and change the delay to 2 seconds.

Task: Stop!

Below, we have brought together the servo and ultrasonic sensor code basics. There are errors in the default sketch. Modify it so that it works. Then use it on your robot to make it stop.

  1. Get rid of the errors in the sketch below. They will be output to the serial monitor. The robot should travel in a straight line and collect all the coins.
  2. Download the working sketch and upload to your Arduino.
  3. Use an object like a notebook or piece of paper to see if you can make your robot stop.

Feel free to invent new functions with delay() and other nameservo.write() statements, and play around in the virtual environment getting your robot to move. For example, can you make it move in a circle?

# Optional: Follow The Wall

Ideally when we program a problem solving task with variables in the environment that can't be perfectly controlled or ignored we want to avoid "hard-coding" the solution. For example, 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 can get the distance of the top wall using the ultrasonic sensor. 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.


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.


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.