Animation
Purpose
Today you will learn the code behind LED light shows with multiple colors and movement.
Sandbox
Let’s refresh what we did last time. Build this code:
on start
└─ set strip to NeoPixel at pin P0 with 10 leds
└─ strip show color Red
└─ strip set pixel color at 4 to White
└─ strip show
Update the code with more Set Pixel Color At… blocks to make every pixel light up a different color
Write in your sprint notes…
What you notice about the number in Set Pixel Color At ___.
Walkthrough
- Create a new project called
Red and Blue - Add the NeoPixel extension
- Build this code that makes the LEDs change color every second:
on start
└─ set strip to NeoPixel at pin P0 with 10 leds as RGB
forever
└─ strip show color Red
└─ pause (ms) 1000
└─ strip show color Blue
└─ pause (ms) 1000
- Download and test it on your LED strip.
Take a picture…
of your code.
Note: Hang on to this picture. You’ll use this again soon in the assessment.
Exercises
Color Timer
Approaching
Create a new project called Color Timer that:
- Shows green for 3 seconds
- Changes to yellow for 2 seconds
- Finally changes to red for 1 second
- Repeats forever
Write in your sprint notes…
How many
pauseblocks did you use?
Moving Light
Proficient
Create a new project called Moving Light that makes a single white light that move down your strip:
- Start at position 0
- Move one position every half second
- When it reaches the end, start over at position 0
Hint: Use the “set pixel color” and “clear” blocks
Click here for a solution
Write in your sprint notes…
What happens if you don’t clear the strip each time?
Rainbow Wave
Distinguished
Create a new project called Rainbow Wave that makes a moving rainbow pattern:
- Use the “show rainbow” block
- Make it move using the “rotate pixels” block
- Experiment with different rotation speeds and pause times
Hint: Use the “show rainbow” and “rotate pixels by [variable]” and “change [variable] by” blocks
Click here for a solution
Write in your sprint notes…
What rotation and pause values gave you the smoothest animation?