Flashing LED


Step 1 /10

Introduction

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.


Step 2 /10

Configure leg

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.


Step 3 /10

Set to output

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.


Step 4 /10

Set leg

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).


Step 5 /10

Run your code

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).


Step 6 /10

Flash the LED

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.


Step 7 /10

Run your code

Click the Play button to run your code. Notice that leg 0’s background turns black for 1 second and then goes red.


Step 8 /10

Continuous flashing

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.


Step 9 /10

Run your code

Click the Play button to run your code. Leg 0’s background goes black for 1 second and then red for 1 second, continuously.


Step 10 /10

Download your code

Click the Download button to download your program. Make sure to follow the download to CodeBug tutorial to load your program onto your bug.