CR201 Tutorial – Class 4

This is the final tutorial for the CR201 course. This tutorial will go over conditional statements, relational operators, and flowcharts.

Conditional Statements

Conditional statements are a tool used to compare values. When you need to compare the relationship of two values in an if/else statement, you’ll use these.


Math Relational OperatorC Programming Syntax

When used in a conditional statement, the output will be TRUE when the relationship check is correct or FALSE when the relationship check is incorrect. However, relational operators have no effect outside a conditional statement.

An example of conditional statements would be something like this:

A = 1
B = 2

if (A == B) {

/* this is false, code here will NOT be executed */

} else if (A != B) {

/* this is true, code here WILL be executed */

} else {

/* code here will NOT be executed because the second condition above is already true and exit the if/else statement*/


if (A > B) {

/* this is false, code here will NOT be executed */

} else if (A >= B) {

/* this is false, code here will NOT be executed */

} else if (A < B) {

/* this is true, code here WILL be executed */

} else if (A <= B) {

/* although this is true, code here will NOT be executed because the third condition above is already true and exit the if/else statement*/


With the Ultrasonic Sensor

Comparative statements are extremely useful with the ultrasonic sensor.  Using them, we can have the Arduino do things when the distance sensed is greater/smaller/equal to a specific value. For example:

#include <CAROBOT_SwissCHEESE.h>

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

void setup() {

pinMode(trig, OUTPUT);
pinMode(echo, INPUT);


void loop() {

digitalWrite(trig, LOW);
digitalWrite(trig, HIGH);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
duration = duration / 59;
if(duration < 30){

    Serial.println(“Too Close!”);


else {





This code will check if the rover is within 30 cm of an object. If the distance is less than 30 cm, it will print a warning that it’s too close.

The Servo Motor

Servo motors are motors that can move to fixed positions, letting you precisely control angular precision. They’re useful for making things like robotic arms.

In order to use a servo motor with Arduino, it has to be plugged into an output port. From there, you will need a few things in your code:

  • #include <Servo.h> at the top of the code to include the servo library. It should go with the code you added to include the SwissCHEESE library.
  • Declare the servo at the start of the code (eg. Servo servo;). This goes before the setup function, and with things like variable declarations.
  • Declare the servo pin before the setup code (eg. int servo_pin = O1;).
  • Type servo.attach(servo_pin); in the setup function.
  • Whenever you want the servo to turn to an angle, use servo.write(angle);. For example, if you want it to turn to 90 degrees, you would write servo.write(90);. Make sure you don’t forget a delay, otherwise the servo won’t know how long to maintain that position for!

Flowcharts and the Right Hand Algorithm

The previous tutorials have already gone over flowcharts, but it’s worth it to revisit them. Essentially, creating flowcharts for programs is a very good way to visually represent them, and therefore understand them better while preventing logic errors.

The Right Hand Algorithm is a specific algorithm useful for navigating through a maze. The flowcharts for said algorithm are below. The first flowchart outlines the basic concepts, while the second is a little more specific on what the Arduino should be doing during this time.

CR201 Tutorial - Class 4

CR201 Tutorial - Class 4

One Last Challenge

Build a robot that can navigate through a maze using the ultrasonic sensor and/or the bumper sensor. You’ll need to attach a servo motor to your ultrasonic sensor to let it check the distances from a wall for each direction, and therefore decide which way to turn whenever the rover gets close to a wall.

That’s the end of CR201. Hope you learned a lot from the tutorials, and that you continue learning more about programming and robotics!