Scratch Project 1: Traffic Light
Description: build a working traffic light out of LEDs
Programming Concepts: controlling LEDs, using the wait command to time events
Materials: Hummingbird Controller, Red LED, Yellow LED, Green LED
Approx. Time: 10 Minutes
1. Connect the green, yellow, and red LEDs to ports 1, 2, and 3, respectively.
2. Connect the Hummingbird to your computer to begin programming.
1. Open the BirdBrain Robot Server.
2. Wait for the screen to say "Hummingbird Duo Connected".
3. Click "Open Scratch".
4. First we need a "trigger" to indicate when our program should run. This is called an event. Select the "Events" category.
5. Drag a "When space key pressed" block into the middle of the screen to make the program begin when the space key is pressed.
6. Now we are ready to add the blocks that control the Hummingbird. Select the "More Blocks" category to reveal the Hummingbird-specific blocks.
We want our traffic light to go from green to yellow to red and then back to green. First we need to turn on the green light (port 1).
7. Attach an "HB LED" block to the "When space key pressed" block.
8. We want the LED to be at its maximum power, which is 100. Click the 50 and type 100.
Your screen should look like this:
9. You can test a block by clicking on it. Click on the LED block and the green light should turn on.
That turns the green light on. Now we need to turn it back off.
10. Right-click (or ctrl-click) the LED block and click "duplicate".
11. Attach the new LED block to the end of the original one and change the intensity to 0.
But we don't want the light to turn off immediately. We want to leave the green light on for some time.
12. Select the "Control" section and drag a "wait 1 secs" block and snap it in-between the two LED blocks.
13. Set the delay to 3 seconds.
14. Duplicate the stack of three blocks by right-clicking the first LED block and selecting "duplicate".
15. Attach the new blocks to the end of the stack.
Your code should now look like this:
16. Now let's change the second two LED blocks to affect the yellow LED. Click the 1 after "LED" and change the port number to port 2 on the bottom two LED blocks.
17. The yellow light should be on for a shorter time than the green light. Change the delay to 1 second.
18. Now duplicate the blocks again and set them to the red LED (port 3).
19. Set the delay to 2 seconds.
The program is now almost complete:
20. We want the program to repeat forever. To do this, drag a forever loop (from the "Control" section) over the current blocks so that it expands to wrap around all of them.
21. Now run the program by pressing your space bar. The light should go from green to yellow to red. To stop the program, click the red stop sign.
22. To save your program, click "File" and then "Save As" and select a location to save the file. Subsequent modifications can be saved using "File" and "Save".