CR201 Tutorial – Class 3

Welcome to the third CR201 tutorial. This tutorial will introduce functions, the ultrasonic sensor, the serial monitor, and long variables.

Functions

A function is a block of code that carries out a specific task placed within it. If you’ve seen setup() and loop() in your code (which you should have), those are examples of functions. The former executes any code used for the initial settings once, while the latter executes the code within over and over again.

Functions are similar to variables in the sense that they have to be declared and called. If you want to call a function, use the following code:

void function_name() {

/* function code goes in here */

}

You can name your function anything you want, with the exception of things already reserved by the system (like setup or loop). Just like variables, every function must have a unique name. If you need to call on a function, you can just type in function_name(). Once a function is executed, the code will just pick up from where it left off. 

Functions make it so that you don’t have to retype code that might be repetitive. A good example of this would be creating functions for the rover, telling it to move forward, turn right, etc.

Tasks:

  1. Rewrite the rover code from before, but using functions to make it turn left and go forward.
  2. Declare two more functions for move backward and turn right. ie. moveBackward() and turnRight() and program its code
  3. Program the robot that moves: forward for 3s, right for 1s, stop for 2s, back for 2s, left for 0.5s, forward for 2s, stop

The Ultrasonic Sensor

A CAROBOT SwissCHEESE ultrasonic sensor.

An ultrasonic sensor is a sensor that sends and receives very high frequency sound waves – ones inaudible by the human ear. By measuring the amount of time between sending out the sound wave and the time when the sound wave arrives back to the sensor, we can calculate the distance between the sensor and the object.

In air, the speed of sound is approximately 343 m/s. If you send out a ping, and it takes 1s for the ping to return, the amount of time it took the sound wave to arrive at the object will be 0.5 seconds. With this information, we can rearrange and use the equation velocity = distance/time to calculate the distance.

When using the Ultrasonic sensor with an Arduino, you’ll need to tell the sensor to emit a signal, and then wait for a return signal. This is done by sending a signal to the TRIG pin, which tells the sensor to send out a ping signal, and then reading the duration of the ECHO pin’s pulse, which tells you the time measured by the ultrasonic sensor unit.

You might notice that the ultrasonic sensor has two pins instead of one – one for the trigger, and one for the echo. To use the ultrasonic sensor, plug the TRIG pin into an output port, and plug the ECHO pin into an input port. Once your sensor is plugged in, write this code into the IDE:

#include <CAROBOT_SwissCHEESE.h>

int trig = O0;
int echo = I0;
long duration = 0;

void setup() {

pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);

}

void loop() {

digitalWrite(trig, LOW);
delayMicroseconds(5);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
duration = duration / 59;
Serial.println(duration);
delay(250);

}

This is just the code required to get the sensor working – it doesn’t do a whole lot else. But to read the results that the sensor gives you, you’ll need to understand how the serial monitor works. So, without further ado:

The Serial Monitor

The Arduino serial monitor allows you to both send messages from your computer to the Arduino and receive messages from the Arduino. To use the serial monitor, the baud rate setting in your program and the one in the serial monitor must match. In most cases, a baud rate of 9600 is sufficient.

The serial monitor is a very powerful tool because it allows you to talk to the Arduino and also for the Arduino to talk back. We won’t be exploring sending message to the Arduino in this course but we will be receiving serial data from the Arduino.

Now, upload your code and open the serial monitor, ensuring that the baud rate setting is at 9600. Once you are getting readings from the ultrasonic sensor, move it around or put an object in front of it and see what happens. If your sensor is working, the values returned in the serial monitor should change as you move your sensor around – this is your sensor tracking distance!

Long Variables

You may have noticed that we have a new variable type in the above code!

Up until now, we’ve been using the data type “int” (integer) to save whole numbers. However, sometimes int variables might not be big enough to save your number, because int variables can only store 2 bytes of data. Therefore, the numbers it can save can only be from -32,768 to 32,767.

If we want to save a number that is bigger, we’ll need to use the “long” variable type, which is a 4-byte data type. This variable type allows us to save a range from (-232)to (232-1) which is -2,147,483,648 to 2,147,483,647.

You may be wondering: why can’t we always use “long” instead of using “int”, if long variables store larger numbers? Yes, you can always use long variables instead of int variables. But as a programmer, you should always think about saving memory. The “long” variable uses up much more memory and processing power.

More Tasks

Replace the delay(250) code from the program you just made with the following:

if (duration < 10) {

robotStop();

}

else {

moveForward();

}

Then, add a button and a LED to your robot and program it so your robot will only move after the press of the “start” button and stop and flash the LED when the robot is 20cm away from an object.

Challenge

Build and program an obstacle avoidance or object following robot using the ultrasonic sensor.

Think about the logic of the robot and apply all the concepts you have learned so far.

Once you finish this, you’re done with this tutorial! Look forward to the final one!