From: Neil Smith Date: Mon, 14 Oct 2024 09:49:15 +0000 (+0100) Subject: Tinkering and tidying up. X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;ds=sidebyside;p=one-dimensional-fireworks.git Tinkering and tidying up. --- diff --git a/pi-pico-version/README.md b/pi-pico-version/README.md index fc31da8..3b3cc52 100644 --- a/pi-pico-version/README.md +++ b/pi-pico-version/README.md @@ -72,7 +72,9 @@ Again, save and flash the program. Even cooler animation! # 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) diff --git a/pi-pico-version/main1.py b/pi-pico-version/main1.py index f7e30ad..7f74da3 100644 --- a/pi-pico-version/main1.py +++ b/pi-pico-version/main1.py @@ -6,13 +6,11 @@ from pimoroni import Button 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) diff --git a/pi-pico-version/main2.py b/pi-pico-version/main2.py index 0de96a7..024feff 100644 --- a/pi-pico-version/main2.py +++ b/pi-pico-version/main2.py @@ -7,12 +7,12 @@ from pimoroni import Button 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) diff --git a/pi-pico-version/main3.py b/pi-pico-version/main3.py index bd3dc4b..388eae4 100644 --- a/pi-pico-version/main3.py +++ b/pi-pico-version/main3.py @@ -7,12 +7,11 @@ from pimoroni import Button 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) @@ -47,7 +46,7 @@ def explode(): reset() -delay_timer = 100 # Add this line +delay_timer = 100 while True: if button_y.raw() or delay_timer <= 0: diff --git a/pi-pico-version/main4.py b/pi-pico-version/main4.py index a04daf1..0455f34 100644 --- a/pi-pico-version/main4.py +++ b/pi-pico-version/main4.py @@ -7,12 +7,11 @@ from pimoroni import Button 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) @@ -59,7 +58,7 @@ def explode(): reset() -delay_timer = 100 # Add this line +delay_timer = 100 while True: if button_y.raw() or delay_timer <= 0: diff --git a/pi-pico-version/main5.py b/pi-pico-version/main5.py index 9c9a3d6..8dbb890 100644 --- a/pi-pico-version/main5.py +++ b/pi-pico-version/main5.py @@ -7,12 +7,11 @@ from pimoroni import Button 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) @@ -59,7 +58,7 @@ def explode(): reset() -delay_timer = 100 # Add this line +delay_timer = 100 while True: if button_y.raw() or delay_timer <= 0: diff --git a/pi-pico-version/main6.py b/pi-pico-version/main6.py index 710f6b5..71503c1 100644 --- a/pi-pico-version/main6.py +++ b/pi-pico-version/main6.py @@ -7,12 +7,11 @@ from pimoroni import Button 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) @@ -22,7 +21,7 @@ FIREWORK_COLOURS = [ (255, 128, 128, 0), (128, 255, 128, 0), (128, 128, 255, 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() @@ -47,7 +46,7 @@ def shoot_firework(): 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) @@ -60,10 +59,11 @@ def explode(): 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: diff --git a/pi-pico-version/main7.py b/pi-pico-version/main7.py index 46771a7..7ff83cf 100644 --- a/pi-pico-version/main7.py +++ b/pi-pico-version/main7.py @@ -8,18 +8,17 @@ from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER # Add this line 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) @@ -30,7 +29,7 @@ FIREWORK_COLOURS = [ (255, 128, 128, 0), (128, 255, 128, 0), (128, 128, 255, 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() @@ -41,6 +40,10 @@ def fade(n, fade_by=0.9): 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) @@ -50,7 +53,7 @@ def shoot_firework(): 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) @@ -60,7 +63,7 @@ def shoot_firework(): 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) @@ -70,9 +73,13 @@ def explode(): , 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() @@ -90,7 +97,7 @@ while True: 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) diff --git a/pi-pico-version/main8.py b/pi-pico-version/main8.py index 7854eca..e2f9a74 100644 --- a/pi-pico-version/main8.py +++ b/pi-pico-version/main8.py @@ -8,8 +8,6 @@ from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER NEOPIXEL_DATA_PIN = 0 NEOPIXEL_LENGTH = 60 -BURST_SIZE = 10 - np = Neopixel(NEOPIXEL_LENGTH, 0, NEOPIXEL_DATA_PIN, "GRBW") button_y = Button(15) @@ -21,6 +19,8 @@ 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) @@ -28,10 +28,12 @@ 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() @@ -43,6 +45,11 @@ def fade(n, fade_by=0.9): 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) @@ -52,7 +59,7 @@ def shoot_firework(): 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) @@ -64,7 +71,7 @@ def shoot_firework(): 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): @@ -75,18 +82,21 @@ def explode(): , 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: @@ -100,7 +110,7 @@ while True: 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)