## CS 1020 - Lab 5 - Advanced programming and concurrency

Start by creating a new program file "lab5.txt" in your LEGO folder. Please do not simply tack on new code to an existing project - it's time to be neat, and you'll lose points if you're not! :) At the top of your new program, include your group number, your names, and "lab 5" in a comment. After we've had a chance to observe the progress on your 8-ball robot, temporarily disconnect the Handy Board from your robot (sorry!), and attach two switches (bump sensors) to digital inputs 7 and 8. Do not attach any motors, but simply observe the red and green motor LEDs for the exercises below.
• Write six procedures "p1" through "p6" that solve each of the 6 tasks below. Do not define any other procedures, but use the primitive commands ("a," "on", "off", "rd", "toggle", "switch 7", ...). Download your procedures as menu options 1-6 and test them. We will provide the instructors' Handy Board with correct versions of these procedures as example so you can test to make sure you understand what they're supposed to be doing.

1. a is on when switch 7 is pressed and off otherwise; b is on when switch 8 is pressed and off otherwise.
2. switch 7 turns a on and b off, switch 8 turns a off and b on (in any order).
3. Only one of a and b is on. Which one is on changes every time switch 7 is pressed.

Note: The following cannot be accomplished without mutable variables and/or concurrency:

1. switch 7 toggles motor a, switch 8 toggles motor b (in any order).
2. switch 7 turns on a for a second, switch 8 turns on b for a second. Switches active even when motors on.
3. motors a and b go forward for 1 second, then backwards for 1 second. They repeat this behavior until switch 7 is pressed.

• Challenge 7 (extra credit) Write a procedure "p7" to implement the following behavior: Initially, motor a continually reverses its direction every 0.2 seconds (the green and red LEDs blink back and forth). Pressing switch 7 causes motor b to blink instead. Pressing it again switches back to motor a, and so forth. Again, you can observe this behavior on our sample Handy Board. When you are done, print out your code (and hand it in at the beginning of class on Friday). Then, you can go back to work on your 8-ball robot. Hopefully you can put your new programming skills to use!