LEDs are really fun to play with and easy to use. Let’s start by configuring the CodeBug emulator to help us out.
Click on the cog at the top right of CodeBug.
Tick the Show leg controls box.
Click Apply button.
The emulator will show the status of CodeBug’s legs with blue arrows. An arrow pointing towards the leg is an input, away from the leg is an output.
To be able to control and LED you need to tell CodeBug to use leg 0 as an output.
Click on the cog on your start block.
In the window that appears, tick the checkbox next to leg 0 input/output.
Click the cog again to close the window.
We want to set leg 0 to an output.
Select the drop down menu next to “leg 0 as” on your start block, and select digital output.
We want to set leg 0 to ground, so the electricity to flows from CodeBug’s power leg, through the LED, and to leg 0.
Click Inputs/Outputs menu and drag in a set leg block.
Now set leg 0 to 0 (i.e. ground).
Click the Play button to run your code. Notice that leg 0’s blue arrow is pointing away from the leg (showing it is an output) and that the leg background is black (showing it has been set to ground).
To flash the LED we need to turn it on and off, with a pause in between.
From the Basics menu drag in a pause block and snap it beneath your set leg block.
From the Inputs/Outputs menu drag in another set leg block and snap it beneath your pause block. Set this new set leg block to 1.
Click the Play button to run your code. Notice that leg 0’s background turns black for 1 second and then goes red.
To flash your LED more than once, you need to replay or ‘loop’ your code. From the Loops menu, drag in a repeat while true block and place it on a blank area on your workspace. Click on your first set leg block and drag your blocks into your new while loop. Snap your while loop onto the start block. From the Basics menu, drag in a pause block and snap it beneath your second set leg block (making sure it is inside the repeat while block.
Click the Play button to run your code. Leg 0’s background goes black for 1 second and then red for 1 second, continuously.
Click the Download button to download your program. Make sure to follow the download to CodeBug tutorial to load your program onto your bug.