From 43572bb8b003fd32de3881b56fc43e0e73d47e35 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 14 Oct 2024 09:52:53 +0100 Subject: [PATCH] More revisions --- microbit-version/README.md | 16 +++--- pi-pico-version/README.md | 11 +++- pi-pico-version/main4.py | 2 +- pi-pico-version/main5.py | 6 +-- pi-pico-version/main6.py | 11 ++-- pi-pico-version/main7.py | 12 +++-- pi-pico-version/main8.py | 107 +++++++++++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 24 deletions(-) create mode 100644 pi-pico-version/main8.py diff --git a/microbit-version/README.md b/microbit-version/README.md index 7d66312..9dd40fb 100644 --- a/microbit-version/README.md +++ b/microbit-version/README.md @@ -20,7 +20,7 @@ Note that Python is really picky about * colons at the end of some lines * the difference between zero and oh, and one and ell. -> [Program 1](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks1.py) +> [Program 1](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/microbit-version/fireworks1.py) When you've typed it in, press "Save" button to save your file then press the "Flash" button to put the program on the Microbit. @@ -31,42 +31,42 @@ Pressing the button is OK, but let's make the firework launch if you shake the M Make the changes indicated to your program. You don't need to type the '# Add this line' comments: that's just to show you what to do. -> [Program 2](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks2.py) +> [Program 2](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/microbit-version/fireworks2.py) Again, save and flash the program. Now try shaking the Microbit and see if it launches a firework. # Program 3: Exploding fireworks Now to make the firework explode at the top! -> [Program 3](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks3.py) +> [Program 3](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/microbit-version/fireworks3.py) Again, save and flash your program. You should now have explosions! # Program 4 What you've got is OK, but let's add some animation to the explosion. Let's make it start small and rapidly grow, and then fade over a bit of time. -> [Program 4](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks4.py) +> [Program 4](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/microbit-version/fireworks4.py) Again, save and flash the program. Cool animation! # Program 5: different colour explosions Always having the same colour explosion is a bit boring. Let's make every explosion a different colour. -> [Program 5](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks5.py) +> [Program 5](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/microbit-version/fireworks5.py) Again, save and flash the program. Even cooler animation! # Program 6: speeding up the animation The animation's a bit slow. We can speed things up by only updating the pixels that change, rather than all of them. -> [Program 6](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks6.py) +> [Program 6](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/microbit-version/fireworks6.py) # Program 7: Microbit display The Microbit has a small display. Let's use that to show the firework is ready. -> [Program 7](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks7.py) +> [Program 7](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/microbit-version/fireworks7.py) # Program 8: Radio control The Micro:bit has a radio. Let's use that to make one firework (sometimes) set off another firework. -> [Program 8](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks8.py) \ No newline at end of file +> [Program 8](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/microbit-version/fireworks8.py) \ No newline at end of file diff --git a/pi-pico-version/README.md b/pi-pico-version/README.md index 00a75f3..aef5911 100644 --- a/pi-pico-version/README.md +++ b/pi-pico-version/README.md @@ -46,7 +46,7 @@ Make the changes indicated to your program. You don't need to type the '# Add th > [Program 2](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks2.py) -Again, save and flash the program. Now leave the button alone and see if it makes the firework launch by itself. +Again, save the program on the Pi Pico as `main.py`. Now leave the button alone and see if it makes the firework launch by itself. # Program 3: Exploding fireworks Now to make the firework explode at the top! @@ -76,7 +76,14 @@ The animation's a bit slow. We can speed things up by only updating the pixels t > [Program 6](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks6.py) -# Program 7: Microbit display +# Program 7: Explorer display The Explorer has a small display. Let's use that to show a countdown. > [Program 7](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks7.py) + +# Program 8: Annoying sound +The Explorer has a small piezo-electric buzzer. We can use that to make an annoying sound when the firework goes off. + +On the Explorer board, put a jumper between the "Audio" pin and GPIO pin 5. + +> [Program 8](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks8.py) \ No newline at end of file diff --git a/pi-pico-version/main4.py b/pi-pico-version/main4.py index 5332d7d..a04daf1 100644 --- a/pi-pico-version/main4.py +++ b/pi-pico-version/main4.py @@ -65,7 +65,7 @@ while True: if button_y.raw() or delay_timer <= 0: # set off a firework shoot_firework() - explode() # Add this line + explode() reset() delay_timer = random.randint(50, 100) else: diff --git a/pi-pico-version/main5.py b/pi-pico-version/main5.py index 2d00888..9c9a3d6 100644 --- a/pi-pico-version/main5.py +++ b/pi-pico-version/main5.py @@ -18,8 +18,8 @@ RED = (64, 0, 0, 0) OFF = (0, 0, 0, 0) # Add this block -FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) - , (255, 255, 128), (255, 128, 255), (128, 255, 255) +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) ] def fade_all(fade_by=0.9): @@ -65,7 +65,7 @@ while True: if button_y.raw() or delay_timer <= 0: # set off a firework shoot_firework() - explode() # Add this line + explode() reset() delay_timer = random.randint(50, 100) else: diff --git a/pi-pico-version/main6.py b/pi-pico-version/main6.py index 3e2996f..710f6b5 100644 --- a/pi-pico-version/main6.py +++ b/pi-pico-version/main6.py @@ -17,8 +17,8 @@ BLUE = (0, 0, 64, 0) RED = (64, 0, 0, 0) OFF = (0, 0, 0, 0) -FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) - , (255, 255, 128), (255, 128, 255), (128, 255, 255) +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) ] def fade_all(first=0, last=NEOPIXEL_LENGTH, fade_by=0.9): # Change this line @@ -52,13 +52,14 @@ def explode(): for i in range(BURST_SIZE): np.set_pixel(NEOPIXEL_LENGTH - BURST_SIZE + i, initial_colour) np.set_pixel(NEOPIXEL_LENGTH - BURST_SIZE - i, initial_colour) - fade_all( fade_by=0.95 # Add this line + fade_all( fade_by=0.9, # Add this line , first=(NEOPIXEL_LENGTH - BURST_SIZE - i) # Add this line , last= (NEOPIXEL_LENGTH - BURST_SIZE + i) # Add this line ) # Add this line for _ in range(30): - fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE)) # Change this line + fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE), # Change this line + fade_by=0.8 ) # Change this line reset() @@ -68,7 +69,7 @@ while True: if button_y.raw() or delay_timer <= 0: # set off a firework shoot_firework() - explode() # Add this line + explode() reset() delay_timer = random.randint(50, 100) else: diff --git a/pi-pico-version/main7.py b/pi-pico-version/main7.py index 26a106a..46771a7 100644 --- a/pi-pico-version/main7.py +++ b/pi-pico-version/main7.py @@ -24,10 +24,11 @@ BLUE = (0, 0, 64, 0) RED = (64, 0, 0, 0) OFF = (0, 0, 0, 0) -FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) - , (255, 255, 128), (255, 128, 255), (128, 255, 255) +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) ] + def fade_all(first=0, last=NEOPIXEL_LENGTH, fade_by=0.9): # Change this line for i in range(NEOPIXEL_LENGTH): fade(i, fade_by=fade_by) @@ -64,13 +65,14 @@ def explode(): for i in range(BURST_SIZE): np.set_pixel(NEOPIXEL_LENGTH - BURST_SIZE + i, initial_colour) np.set_pixel(NEOPIXEL_LENGTH - BURST_SIZE - i, initial_colour) - fade_all( fade_by=0.95 + fade_all( fade_by=0.9 , first=(NEOPIXEL_LENGTH - BURST_SIZE - i) , last= (NEOPIXEL_LENGTH - BURST_SIZE + i) ) for _ in range(30): - fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE)) + fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE), + fade_by=0.8) reset() @@ -80,7 +82,7 @@ while True: if button_y.raw() or delay_timer <= 0: # set off a firework shoot_firework() - explode() # Add this line + explode() reset() delay_timer = random.randint(50, 100) else: diff --git a/pi-pico-version/main8.py b/pi-pico-version/main8.py new file mode 100644 index 0000000..7854eca --- /dev/null +++ b/pi-pico-version/main8.py @@ -0,0 +1,107 @@ +from neopixel import Neopixel +from machine import Pin +import time +import random +from pimoroni import Button, Buzzer # Change this line +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) +buzzer = Buzzer(5) # Add this line +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER) +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) + +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) + ] + + +def fade_all(first=0, last=NEOPIXEL_LENGTH, fade_by=0.9): # Change this line + for i in range(NEOPIXEL_LENGTH): + fade(i, fade_by=fade_by) + np.show() + +def fade(n, fade_by=0.9): + r, g, b, w = np.get_pixel(n) + np.set_pixel(n, + (int(r * fade_by), int(g * fade_by), + int(b * fade_by), int(w * fade_by))) + +def reset(): + buzzer.set_tone(0) # Add this line + for n in range(NEOPIXEL_LENGTH): + np.set_pixel(n, OFF) + np.set_pixel(0, BLUE) + np.show() + +def shoot_firework(): + display.set_pen(BLACK_PEN) + display.clear() + display.set_pen(RED_PEN) + display.text("Fire!", 0, 100, scale=4) + display.update() + for pixel in range(NEOPIXEL_LENGTH - BURST_SIZE): + np.set_pixel(pixel, RED) + np.show() + time.sleep(0.02) + np.set_pixel(pixel, OFF) + np.show() + +def explode(): + initial_colour = random.choice(FIREWORK_COLOURS) + + buzzer_tone = 40 # Add this line + buzzer.set_tone(buzzer_tone) # Add this line + + for i in range(BURST_SIZE): + np.set_pixel(NEOPIXEL_LENGTH - BURST_SIZE + i, initial_colour) + np.set_pixel(NEOPIXEL_LENGTH - BURST_SIZE - i, initial_colour) + fade_all( fade_by=0.9 + , first=(NEOPIXEL_LENGTH - BURST_SIZE - i) + , last= (NEOPIXEL_LENGTH - BURST_SIZE + i) + ) + + buzzer_tone += 500 # Add this line + buzzer.set_tone(buzzer_tone) # Add this line + + for _ in range(30): + fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE), + fade_by=0.8) + buzzer_tone -= 2 # Add this line + buzzer.set_tone(buzzer_tone) # Add this line + +reset() + +delay_timer = 100 # Add this line + +while True: + if button_y.raw() or delay_timer <= 0: + # set off a firework + shoot_firework() + explode() + reset() + delay_timer = random.randint(50, 100) + else: + delay_timer -= 1 + 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.update() + time.sleep(0.1) + + -- 2.34.1