← MicroPython on Pi Pico and Projects

Project 2.4 Companion

Project 2.4 Worksheet: Animating with Lists of Lists

Name: Date: Period:

Use this with Project 2.4: Animating the NeoPixel Bar. Your answers can be short: you do not need full sentences.

nested list: a list whose elements are lists; here, a list of frames
frame: one moment of the animation: a list of 8 colors
index: a position in a list, counting from 0; frames[2][5] uses two
iteration: one pass through a loop; a loop inside a loop iterates once per frame, per pixel

A. Plan your animation

Rows are moments in time and columns are pixels. Plan each frame by writing a letter in each square (R, G, B, or your own letters), and OFF as a dash. Leave a row empty if you do not need it. Make a key for your letters below the grid.

FramePixel 0Pixel 1Pixel 2Pixel 3Pixel 4Pixel 5Pixel 6Pixel 7
0
1
2
3
4
5
6
7

Key:

B. Reading frames on paper

Do these before you touch the code. Here is an animation of two pixels moving toward each other:

R = (20, 0, 0) G = (0, 20, 0) OFF = (0, 0, 0) frames = [ [R, OFF, OFF, OFF, OFF, OFF, OFF, G ], # frame 0 [OFF, R, OFF, OFF, OFF, OFF, G, OFF], # frame 1 [OFF, OFF, R, OFF, OFF, G, OFF, OFF], # frame 2 [OFF, OFF, OFF, R, G, OFF, OFF, OFF], # frame 3 ]
How many frames are there? len(frames)
How many colors in a frame? len(frames[0])
What color is frames[1][1]?
What color is frames[2][5]?
In frame 3, which two pixels are lit, and what color is each?
Predict what this does, and why: frames[4][0]
Predict what this does, and why: frames[0][8]

C. The loop inside a loop

for f in range(len(frames)): for i in range(len(np)): np[i] = frames[f][i] np.write()
How many times does the line np[i] = frames[f][i] run during one trip through all 4 frames?
How many times does np.write() run?
Why is np.write() after the inner loop, not inside it?

D. Scrubbing with the knob

The knob picks a frame number. For the 4 frames above, which frame numbers are legal?
With the knob turned all the way up, a mapping gives 4. What happens, and how could you stop it?

E. Your own animation

Pick any square from your grid in A. Write its position as frames[ ][ ] and its color.
Why is an animation a list of lists? Use the words frame and index in your answer.