Lights & Colors 2 & 3
Lesson (25 mins):
Classroom Code / Project: Light LED Pattern
Equipment: (1 for each Trainee)
Micro:bit
computer
USB cord
Skills
Light Individual pixels,
Make patterns
Flash LED strip
Keywords
sequences
loops
variables
In Game Application
Entering a portal, Communicating
Part 1:
Flashing LED Strip
One of the neat things about addressable LEDs is we can “animate” them. In other words, by changing the colors of the LEDs or turning them off and on really fast, we give the impression that the lights are moving.
Some examples:
https://learn.adafruit.com/cheerlights-led-animations
https://learn.adafruit.com/circuitpython-feather-ble-neopixel-hat
We’re going to start by making the LED strip flash for our first animation.
Facilitator guides the students through “Flash LED Strip” Project:
Explanation of the code:
Before this, we only used “On start” with our LED Strip. If you recall, “On start” tells the LED strip what to do one time, when the micro:bit starts up.
This time, we also want to use the “Forever” block. The “Forever” block is a loop. It tells the micro:bit to keep doing whatever is inside that block over and over and over.
In this case, it’s telling the LED strip to show white, pause for 200 milliseconds (that’s 1/1000th of a second) before going to the next instruction, show black, then pause again. When it finishes doing that, it starts it all over again.
You can put whatever you want in the “Forever” block, and it will keep repeating that. Try changing the colors and the length of the pauses to get different flashing patterns. You can even make it change more than just on and off!
Student Activity
Students should try modifying the code to show their own flashing patterns. Note that they can change the timing, the color, and always add more blocks to cycle between more colors, as in the example below.
Part 2:
Controlling individual LEDs as Ranges
One of the great things about addressable LEDs is that every LED in the strip can be controlled independently. That means we can light up the LED strip in different designs by telling it what color each LED should be, and whether it should be on or off.
Neopixels let you define a range of LEDs and set those however you want. In the example below, I’ve set the first 5 LEDs to red. (Note that when you count the LEDs on the strip, it starts from 0, which is why it says to start the range from 0. Then it’ll count 5 LEDs from zero and define that as the range).
“Set range” doesn’t actually turn the LEDs on yet -- it just gives the instructions for what color that range of LEDs will need to be. To actually show those colors, you have to include a “show” block at the end.
What happens if you want to define two ranges? Say you want the first 5 pixels to be red, but the second 5 to be yellow?
We can create two ranges -- “Range1” and “Range2”. That way, we can tell the micro:bit what colors we want each range to show.
Student Activity
Students should try modifying the code to make their own patterns. They should try to add as many ranges and colors as they want, and they can even combine it with Part 1 to make their patterns flash.
Lesson (25 mins):
Demonstration Code: (For Part 2) : Flash LED Strip 2
Classroom Code/Project: Flash LED Strip
Equipment: (1 for each Trainee)
Micro:bit
computer
USB cord
Skills
Animate LED Strip
Keywords
sequences
loops
variables
animate
In Game Application
Entering a portal, Communicating, Scaring monsters
Flashing LED Strip
We’re going to start by making the LED strip flash for our first animation.
Facilitator guides the students through “Flash LED Strip” Project: Flash LED Strip
Explanation of the code:
Before this, we only use “On start” with our LED Strip. If you recall, “On start” tells the LED strip what to do one time, when the micro:bit starts up.
This time, we also want to use the “Forever” block. The “Forever” block is a loop. It tells the micro:bit to keep doing whatever is inside that block over and over and over. In this case, it’s telling the LED strip to show white, pause for 200 milliseconds (that’s 1/1000th of a second) before going to the next instruction, show black, then pause again. When it finishes doing that, it starts it all over again.
Other animations:
You can put whatever you want in the “Forever” block, and it will keep repeating that. Try changing the colors and the length of the pauses to get different flashing patterns. You can even make it change more than just on and off. For instance, what happens if I try the code below?
You can also combine it with what we learned previously about lighting up individual LEDs and ranges, like in the example below: https://makecode.microbit.org/_UhfJMk14wAKJ