Lab 2:Bike Blinky

After lots of research, I chose this rear light for my bike. It is USB rechargeable, really bright, with a sort of random flashing mode that I think gets drivers' attention when I'm biking on roads. What's involved in programming some light like this to get it to flash the way you want? That's what we'll explore today.

We'll use our microbits and makecode.microbit.org to create a bike blinky in stages: 1. Animate the lights, 2. Toggle them on/off, and for extra credit 3. Change the animation speed. In this activity we bring together three foundational building blocks in programming: variables, loops, and if-statements.

Before you start work, watch the 1.5 minute video below so you can see what you're trying to create.

First write your name down in a group in our shared google slides for the day. Your Zoom breakout room number will correspond to your google slide group. Follow the steps shown below. Colors of words in the text correspond to which blocks drawer to look in.

Step 1 (1 point)

Goal: Use a forever loop along with icon patterns to animate the lights.

You can use the Basic drawer show icon or show ls blocks to create an original sequence that would be effective as a bike light. You should have at least 3 different patterns in your animation. Put a screenshot on your page.

Step 2 (1 point)

Goal: Toggle the animation on/off when button A and button B are pressed together, playing sounds when you toggle on/off.

To do this we will use a
variable which can be set to true/false, using that to control whether the led patterns are displayed.

  1. Make a variable called display (or some similar name). Use a blue on start block and in it initialize display to be true, pulling true out from the turquoise Logic drawer.

  2. Put an if block inside the forever loop, encasing all the led display blocks. Put the display variable inside the if condition. This way when display is true, the led sequence is shown, but if display is false, the display sequence isn't shown.

  3. Now we want to be able to toggle the display value (between true/false) when the A+B buttons are simultaneously pressed.

    1. Add the on button A+B pressed block from the Input drawer.

    2. Inside it add an if-else block.

    3. Add display as the value to be checked in the if condition.

    4. If the condition is true, set display to false, else set display to true.

  4. From the Music / Tone drawer for each on/off condition add a couple of tones such as play tone Middle C for 1/8 beat.

Remember to put a screenshot on your page of this second version, in addition to the first one.

Extra Credit Time permitting (1 point)

Goal: Change the animation speed to slow down using button A and to speed up using button B, using the Python view.

  1. Add a new variable (g. speed) and initialize it to 40. When button A is pressed, change speed by -40, and when button B is pressed change speed by 40.

  2. Go into the Python view to the basic.show_leds("""... """) and the basic.show_icon(...) sections. For each of them, just before the closing parenthesis, add: , speed

E.g. you would change

basic.show_icon(IconNames.SQUARE)

to

basic.show_icon(IconNames.SQUARE, speed)

When going back and forth between blocks and Python view sometimes variables get replicated or changed, so you went to look over the code whenever you switch between views.

Remember to put a screenshot on your page of this extra credit version, in addition to the first two.

Gifs created using the free program GIPHY an a Mac, Licecap on Windows.