Lab 3

purpose | preparation | procedure

This assigment will give you the chance to practice using conditional flow control (if statements) and comparison expressions.

In-lab presentation

The slides that Ms Hogan, the lab instructor, delivered in the lab are visible in the frame to the right. If they are difficult to see, however, you can also download the slides.

Purpose and Outcomes

In the lab, you will explore the use of conditional flow control via if statements. While doing this, you will:

  • continue practicing the use of variables and
  • continue practicing the declaration, definition and calling of functions.


Answer the following questions together with your partner before coming to the lab.

  1. In this lab, you will make your robot wait for a button to be pressed (button identified by a number corresponding to one of the constants BUTTON_A, BUTTON_B or BUTTON_C). It will then turn left if button A was pressed, drive straight if button B was pressed or turn right if button C was pressed. Draw a flow chart for the logic required in this program.

  2. Design a simple experiment for measuring how fast your robot turns (in degrees/s) when power (controlled by the same duty cycle values as last week) is applied to its wheels in opposite directions.


1. Initial setup

  1. Import the lab 3 template file ( just as you did in previous labs.

  2. Add stub (incomplete) implementations of the turn and driveForwards functions.

  3. Ensure that the code compiles and can be downloaded to your 3$\pi$ robot, even though it doesn’t yet make the robot move.

2. Speed experiment

As with last week’s driving speed experiment, you must determine how fast your robot turns if you are to complete accurate left and right turns. Use the experiment that you designed in the prelab.

Varying the power applied to the 3$\pi$ robot’s wheels (via duty cycle), measure the turning angle and record it in a table of values together with the duty cycle and time and then plot the angular speed versus duty cycle. Use any convenient units.

3. Working code

Using the data gathered in the experiment above (and last week), write a complete implementation for the turn and driveForwards functions.

In the main function, use conditional flow control (an if statement) to make the robot:

  • turn 90 degrees left if Button A is pressed,
  • drive 1ft straight ahead if button B is pressed or
  • turn 90 degrees right if Button C is pressed.

4. Conclusion

Describe what you observed in this lab. Are there any interesting aspects of the robot’s behaviour that do not fit the code as you wrote it?

Submit your work:

Once you have finished writing up your lab activity, submit your log book to the TAs in the lab. If you need extra time to finish the lab, you can submit your log book in the Engineering One Help Centre.