# Program 6: speeding up the animation
When real fireworks explode, the outer edge of the explosion is bright and the centre gets dimmer. Let's change the animation to do that.
-The animation's a bit slow. We can speed things up by only updating the pixels that change, rather than all of them.
+The animation's a bit slow. We can speed things up by only updating the pixels that change, rather than all of them. But when we do, the animation's too fast!
+
+We need to add an explicit delay in the "fading" loop so we can see the animation. Experiment with adjusting the value of `time.sleep`, and the number of fading steps, to have an animation that looks good. (But don't spend so long that you can't do the next steps.)
> [Program 6](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/pi-pico-version/main6.py)
NEOPIXEL_DATA_PIN = 0
NEOPIXEL_LENGTH = 60
-BURST_SIZE = 10
-DISPLAY_ANIMATION_SPEED = 500 # Add this line
-
np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW")
-
button_y = Button(15)
+BURST_SIZE = 10
+
BLUE = (0, 0, 64, 0)
RED = (64, 0, 0, 0)
OFF = (0, 0, 0, 0)
NEOPIXEL_DATA_PIN = 0
NEOPIXEL_LENGTH = 60
-BURST_SIZE = 10
np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW")
-
button_y = Button(15)
+BURST_SIZE = 10
+
BLUE = (0, 0, 64, 0)
RED = (64, 0, 0, 0)
OFF = (0, 0, 0, 0)
NEOPIXEL_DATA_PIN = 0
NEOPIXEL_LENGTH = 60
-BURST_SIZE = 10
-
np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW")
-
button_y = Button(15)
+BURST_SIZE = 10
+
BLUE = (0, 0, 64, 0)
RED = (64, 0, 0, 0)
OFF = (0, 0, 0, 0)
reset()
-delay_timer = 100 # Add this line
+delay_timer = 100
while True:
if button_y.raw() or delay_timer <= 0:
NEOPIXEL_DATA_PIN = 0
NEOPIXEL_LENGTH = 60
-BURST_SIZE = 10
-
np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW")
-
button_y = Button(15)
+BURST_SIZE = 10
+
BLUE = (0, 0, 64, 0)
RED = (64, 0, 0, 0)
OFF = (0, 0, 0, 0)
reset()
-delay_timer = 100 # Add this line
+delay_timer = 100
while True:
if button_y.raw() or delay_timer <= 0:
NEOPIXEL_DATA_PIN = 0
NEOPIXEL_LENGTH = 60
-BURST_SIZE = 10
-
np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW")
-
button_y = Button(15)
+BURST_SIZE = 10
+
BLUE = (0, 0, 64, 0)
RED = (64, 0, 0, 0)
OFF = (0, 0, 0, 0)
reset()
-delay_timer = 100 # Add this line
+delay_timer = 100
while True:
if button_y.raw() or delay_timer <= 0:
NEOPIXEL_DATA_PIN = 0
NEOPIXEL_LENGTH = 60
-BURST_SIZE = 10
-
np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW")
-
button_y = Button(15)
+BURST_SIZE = 10
+
BLUE = (0, 0, 64, 0)
RED = (64, 0, 0, 0)
OFF = (0, 0, 0, 0)
]
def fade_all(first=0, last=NEOPIXEL_LENGTH, fade_by=0.9): # Change this line
- for i in range(NEOPIXEL_LENGTH):
+ for i in range(first, last): # Change this line
fade(i, fade_by=fade_by)
np.show()
np.show()
def explode():
- initial_colour = random.choice(FIREWORK_COLOURS) # Change this line
+ initial_colour = random.choice(FIREWORK_COLOURS)
for i in range(BURST_SIZE):
np.set_pixel(NEOPIXEL_LENGTH - BURST_SIZE + i, initial_colour)
for _ in range(30):
fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE), # Change this line
fade_by=0.8 ) # Change this line
+ time.sleep(0.1) # Add this line
reset()
-delay_timer = 100 # Add this line
+delay_timer = 100
while True:
if button_y.raw() or delay_timer <= 0:
NEOPIXEL_DATA_PIN = 0
NEOPIXEL_LENGTH = 60
-BURST_SIZE = 10
-
np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW")
-
button_y = Button(15)
-display = PicoGraphics(display=DISPLAY_PICO_EXPLORER) # Add this line
+display = PicoGraphics(display=DISPLAY_PICO_EXPLORER) # Add these display lines
WHITE_PEN = display.create_pen(255, 255, 255)
BLACK_PEN = display.create_pen(0, 0, 0)
RED_PEN = display.create_pen(255, 63, 63)
display.set_font("sans")
display.set_thickness(5)
+BURST_SIZE = 10
+
BLUE = (0, 0, 64, 0)
RED = (64, 0, 0, 0)
OFF = (0, 0, 0, 0)
def fade_all(first=0, last=NEOPIXEL_LENGTH, fade_by=0.9): # Change this line
- for i in range(NEOPIXEL_LENGTH):
+ for i in range(first, last):
fade(i, fade_by=fade_by)
np.show()
int(b * fade_by), int(w * fade_by)))
def reset():
+ display.set_pen(BLACK_PEN) # Add these display lines
+ display.clear()
+ display.update()
+
for n in range(NEOPIXEL_LENGTH):
np.set_pixel(n, OFF)
np.set_pixel(0, BLUE)
display.set_pen(BLACK_PEN) # Add these display lines
display.clear()
display.set_pen(RED_PEN)
- display.text("Fire!", 0, 100, scale=4)
+ display.text("Fire", 0, 100, scale=4)
display.update()
for pixel in range(NEOPIXEL_LENGTH - BURST_SIZE):
np.set_pixel(pixel, RED)
np.show()
def explode():
- initial_colour = random.choice(FIREWORK_COLOURS) # Change this line
+ initial_colour = random.choice(FIREWORK_COLOURS)
for i in range(BURST_SIZE):
np.set_pixel(NEOPIXEL_LENGTH - BURST_SIZE + i, initial_colour)
, last= (NEOPIXEL_LENGTH - BURST_SIZE + i)
)
+ animation_delay = random.uniform(0.02, 0.1)
+ animation_delay += random.uniform(0, 0.1)
for _ in range(30):
+
fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE),
fade_by=0.8)
+ time.sleep(animation_delay)
reset()
display.set_pen(BLACK_PEN) # Add these display lines
display.clear()
display.set_pen(WHITE_PEN)
- display.text(str(int(delay_timer / 10.0 + 0.5)), 40, 100, scale=5)
+ display.text(str(int(delay_timer / 10.0) + 1), 40, 100, scale=5)
display.update()
time.sleep(0.1)
NEOPIXEL_DATA_PIN = 0
NEOPIXEL_LENGTH = 60
-BURST_SIZE = 10
-
np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW")
button_y = Button(15)
display.set_font("sans")
display.set_thickness(5)
+BURST_SIZE = 10
+
BLUE = (0, 0, 64, 0)
RED = (64, 0, 0, 0)
OFF = (0, 0, 0, 0)
FIREWORK_COLOURS = [ (255, 128, 128, 0), (128, 255, 128, 0), (128, 128, 255, 0)
, (255, 255, 128, 0), (255, 128, 255, 0), (128, 255, 255, 0)
]
-
+BASE_BUZZER_TONE = 40
+BUZZER_INCREASE = 5
+BUZZER_DECREASE = 2
def fade_all(first=0, last=NEOPIXEL_LENGTH, fade_by=0.9): # Change this line
- for i in range(NEOPIXEL_LENGTH):
+ for i in range(first, last):
fade(i, fade_by=fade_by)
np.show()
def reset():
buzzer.set_tone(0) # Add this line
+
+ display.set_pen(BLACK_PEN)
+ display.clear()
+ display.update()
+
for n in range(NEOPIXEL_LENGTH):
np.set_pixel(n, OFF)
np.set_pixel(0, BLUE)
display.set_pen(BLACK_PEN)
display.clear()
display.set_pen(RED_PEN)
- display.text("Fire!", 0, 100, scale=4)
+ display.text("Fire", 0, 100, scale=4)
display.update()
for pixel in range(NEOPIXEL_LENGTH - BURST_SIZE):
np.set_pixel(pixel, RED)
def explode():
initial_colour = random.choice(FIREWORK_COLOURS)
- buzzer_tone = 40 # Add this line
+ buzzer_tone = BASE_BUZZER_TONE # Add this line
buzzer.set_tone(buzzer_tone) # Add this line
for i in range(BURST_SIZE):
, last= (NEOPIXEL_LENGTH - BURST_SIZE + i)
)
- buzzer_tone += 500 # Add this line
+ buzzer_tone += BUZZER_INCREASE # Add this line
buzzer.set_tone(buzzer_tone) # Add this line
+ animation_delay = random.uniform(0.02, 0.1)
+ animation_delay += random.uniform(0, 0.1)
for _ in range(30):
fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE),
fade_by=0.8)
- buzzer_tone -= 2 # Add this line
+ buzzer_tone -= BUZZER_DECREASE # Add this line
buzzer.set_tone(buzzer_tone) # Add this line
+ time.sleep(animation_delay)
reset()
-delay_timer = 100 # Add this line
+delay_timer = 100
while True:
if button_y.raw() or delay_timer <= 0:
display.set_pen(BLACK_PEN)
display.clear()
display.set_pen(WHITE_PEN)
- display.text(str(int(delay_timer / 10.0 + 0.5)), 40, 100, scale=5)
+ display.text(str(int(delay_timer / 10.0) + 1), 40, 100, scale=5)
display.update()
time.sleep(0.1)