This is the final tutorial for CR101. This lesson will go over subroutines and will introduce the final project for this course.
A subroutine, also known as a function or a method, is a set of instructions designed to perform a frequent operation. It is also used to organize a program. You can find the subroutine block in the yellow Control bin, and unlike most other blocks they do not go in the loop. Like variables, subroutines must have unique names.
An example of how we can use a subroutine is for movement commands.
By creating a subroutine named “forward” and putting the code for a forward movement in it, we can just call on the subroutine every time we want the rover to go forward. Creating subroutines for every type of movement command saves a lot of time and effort in the long run.
Your Final Task
In teams or individually, your task is to build a robot that uses two bumpers to navigate out of an enclosure. Use a button for each bumper.
Make sure to think about the logic, like how long movement commands should last for and exactly what processes should occur when one (or both) bumpers are activated. A flow chart like the following can help a lot.
If you want an extra challenge, add headlights to your rover! Make the headlights turn on or off depending on which bumpers have been activated. For example, make the right light turn on when the right bumper is triggered.