From: Neil Smith Date: Sun, 13 Oct 2024 16:55:40 +0000 (+0100) Subject: Added files for pi pico X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=a20c1378ed8e57b99bdcc28b43a07094f8ecf4db;p=one-dimensional-fireworks.git Added files for pi pico --- diff --git a/README.md b/README.md index 7d66312..c6680a0 100644 --- a/README.md +++ b/README.md @@ -1,72 +1,3 @@ -In this session, you'll use a Raspberry Pi to program a Micro:bit, and the Microbit will control a NeoPixel strip and make it a one-dimensional firework. +This project takes you through builidng and programming a "one dimensional firework". It uses two hardware platforms: a Microbit, and a Raspberry Pi Pico. -# Connecting the Micro:bit and Neopixel strip -* Connect the black lead on the Neopixel strip to the GND hole on the Microbit. -* Connect the red lead on the Neopixel to the 3V hole on the Microbit. -* Connect the white lead on the Neopixel to the 0 hole on the Microbit. -* Use the USB cable to connect the Microbit to the Raspberry Pi - -# Starting the Mu editor -Find the "Start" menu → Programming → mu -Once you've opened Mu, press the Mode button and press "BBC micro:bit" - -# Program 1: launching the firework -Type this program into the Mu editor. It will do the animation for a firework launching. - -Note that Python is really picky about -* spacing and indentation -* upper and lower case letters -* round and square brackets -* 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) - -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. - -When you press the A button on the Microbit, you should see a little light shoot along the strip. - -# Program 2: making the launch motion-sensitive -Pressing the button is OK, but let's make the firework launch if you shake the Microbit. - -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) - -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) - -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) - -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) - -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 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 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 +Look in the appropriate directory for full instructions. diff --git a/fireworks1.py b/fireworks1.py deleted file mode 100644 index fa3bb5c..0000000 --- a/fireworks1.py +++ /dev/null @@ -1,35 +0,0 @@ -from microbit import * -import neopixel - -NP_COUNT = 60 -BURST_SIZE = 10 - -np = neopixel.NeoPixel(pin0, NP_COUNT) - -BLUE = (0, 0, 64) -RED = (64, 0, 0) -OFF = (0, 0, 0) - - -def reset(pixels): - for n in range(NP_COUNT): - pixels[n] = OFF - pixels[0] = BLUE - np.show() - -def shoot_firework(pixels): - for pixel in range(NP_COUNT - BURST_SIZE): - pixels[pixel] = RED - pixels.show() - sleep(20) - pixels[pixel] = OFF - pixels.show() - - -reset(np) - -while True: - if button_a.is_pressed(): - # set off a firework - shoot_firework(np) - reset(np) diff --git a/fireworks2.py b/fireworks2.py deleted file mode 100644 index bdfdaf6..0000000 --- a/fireworks2.py +++ /dev/null @@ -1,37 +0,0 @@ -from microbit import * -import neopixel - -NP_COUNT = 60 -BURST_SIZE = 10 - -np = neopixel.NeoPixel(pin0, NP_COUNT) - -BLUE = (0, 0, 64) -RED = (64, 0, 0) -OFF = (0, 0, 0) - - -def reset(pixels): - for n in range(NP_COUNT): - pixels[n] = OFF - pixels[0] = BLUE - np.show() - -def shoot_firework(pixels): - for pixel in range(NP_COUNT - BURST_SIZE): - pixels[pixel] = RED - pixels.show() - sleep(20) - pixels[pixel] = OFF - pixels.show() - -reset(np) -last_gesture = accelerometer.current_gesture() # Add this line - -while True: - gesture = accelerometer.current_gesture() # Add this line - if gesture != last_gesture or button_a.is_pressed(): # Change this line - # set off a firework - shoot_firework(np) - reset(np) - last_gesture = gesture # Add this line \ No newline at end of file diff --git a/fireworks3.py b/fireworks3.py deleted file mode 100644 index a7f28cf..0000000 --- a/fireworks3.py +++ /dev/null @@ -1,52 +0,0 @@ -from microbit import * -import neopixel - -NP_COUNT = 60 -BURST_SIZE = 10 - -np = neopixel.NeoPixel(pin0, NP_COUNT) - -BLUE = (0, 0, 64) -RED = (64, 0, 0) -OFF = (0, 0, 0) - - -######################################### -# Add this function -def explode(pixels): - initial_colour = (255, 128, 128) - - for i in range(BURST_SIZE): - pixels[NP_COUNT - BURST_SIZE + i] = initial_colour - pixels[NP_COUNT - BURST_SIZE - i] = initial_colour - pixels.show() - sleep(500) -# End of function to add -######################################### - -def reset(pixels): - for n in range(NP_COUNT): - pixels[n] = OFF - pixels[0] = BLUE - np.show() - -def shoot_firework(pixels): - for pixel in range(NP_COUNT - BURST_SIZE): - pixels[pixel] = RED - pixels.show() - sleep(20) - pixels[pixel] = OFF - pixels.show() - - -reset(np) -last_gesture = accelerometer.current_gesture() - -while True: - gesture = accelerometer.current_gesture() - if gesture != last_gesture or button_a.is_pressed(): - # set off a firework - shoot_firework(np) - explode(np) # Add this line - reset(np) - last_gesture = gesture diff --git a/fireworks4.py b/fireworks4.py deleted file mode 100644 index dba124f..0000000 --- a/fireworks4.py +++ /dev/null @@ -1,64 +0,0 @@ -from microbit import * -import neopixel - -NP_COUNT = 60 -BURST_SIZE = 10 - -np = neopixel.NeoPixel(pin0, NP_COUNT) - -BLUE = (0, 0, 64) -RED = (64, 0, 0) -OFF = (0, 0, 0) - -######################################### -# Add these functions -def fade_all(pixels, fade_by=0.9): - for i in range(NP_COUNT): - fade(pixels, i, fade_by=fade_by) - pixels.show() - -def fade(pixels, n, fade_by=0.9): - r, g, b = pixels[n] - pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) -# End of functions to add -######################################### - -def explode(pixels): - initial_colour = (255, 128, 128) - - for i in range(BURST_SIZE): - pixels[NP_COUNT - BURST_SIZE + i] = initial_colour - pixels[NP_COUNT - BURST_SIZE - i] = initial_colour - fade_all(pixels, fade_by=0.95) # Add this line - # pixels.show() # Remove this line - # time.sleep(0.5) # Remove this line - - for _ in range(30): # Add this line - fade_all(pixels) # Add this line - -def reset(pixels): - for n in range(NP_COUNT): - pixels[n] = OFF - pixels[0] = BLUE - np.show() - -def shoot_firework(pixels): - for pixel in range(NP_COUNT - BURST_SIZE): - pixels[pixel] = RED - pixels.show() - sleep(20) - pixels[pixel] = OFF - pixels.show() - - -reset(np) -last_gesture = accelerometer.current_gesture() - -while True: - gesture = accelerometer.current_gesture() - if gesture != last_gesture or button_a.is_pressed(): - # set off a firework - shoot_firework(np) - explode(np) - reset(np) - last_gesture = gesture \ No newline at end of file diff --git a/fireworks5.py b/fireworks5.py deleted file mode 100644 index 6f991ab..0000000 --- a/fireworks5.py +++ /dev/null @@ -1,64 +0,0 @@ -from microbit import * -import neopixel -import random # Add this line - -NP_COUNT = 60 -BURST_SIZE = 10 - -np = neopixel.NeoPixel(pin0, NP_COUNT) - -BLUE = (0, 0, 64) -RED = (64, 0, 0) -OFF = (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) - ] - -def fade_all(pixels, fade_by=0.9): - for i in range(NP_COUNT): - fade(pixels, i, fade_by=fade_by) - pixels.show() - -def fade(pixels, n, fade_by=0.9): - r, g, b = pixels[n] - pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) - -def explode(pixels): - initial_colour = random.choice(FIREWORK_COLOURS) # Change this line - - for i in range(BURST_SIZE): - pixels[NP_COUNT - BURST_SIZE + i] = initial_colour - pixels[NP_COUNT - BURST_SIZE - i] = initial_colour - fade_all(pixels, fade_by=0.95) # Add this line - - for _ in range(30): - fade_all(pixels) - -def reset(pixels): - for n in range(NP_COUNT): - pixels[n] = OFF - pixels[0] = BLUE - np.show() - -def shoot_firework(pixels): - for pixel in range(NP_COUNT - BURST_SIZE): - pixels[pixel] = RED - pixels.show() - sleep(20) - pixels[pixel] = OFF - pixels.show() - - -reset(np) -last_gesture = accelerometer.current_gesture() - -while True: - gesture = accelerometer.current_gesture() - if gesture != last_gesture or button_a.is_pressed(): - # set off a firework - shoot_firework(np) - explode(np) - reset(np) - last_gesture = gesture \ No newline at end of file diff --git a/fireworks6.py b/fireworks6.py deleted file mode 100644 index 1bef8b6..0000000 --- a/fireworks6.py +++ /dev/null @@ -1,66 +0,0 @@ -from microbit import * -import neopixel -import random - -NP_COUNT = 60 -BURST_SIZE = 10 - -np = neopixel.NeoPixel(pin0, NP_COUNT) - -BLUE = (0, 0, 64) -RED = (64, 0, 0) -OFF = (0, 0, 0) - -FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) - , (255, 255, 128), (255, 128, 255), (128, 255, 255) - ] - -def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): # Change this line - for i in range(first, last): # Change this line - fade(pixels, i, fade_by=fade_by) - pixels.show() - -def fade(pixels, n, fade_by=0.9): - r, g, b = pixels[n] - pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) - -def explode(pixels): - initial_colour = random.choice(FIREWORK_COLOURS) - - for i in range(BURST_SIZE): - pixels[NP_COUNT - BURST_SIZE + i] = initial_colour - pixels[NP_COUNT - BURST_SIZE - i] = initial_colour - fade_all( pixels, fade_by=0.95 # Change this line - , first=(NP_COUNT - BURST_SIZE - i) # Add this line - , last= (NP_COUNT - BURST_SIZE + 1) # Add this line - ) # Add this line - - for _ in range(30): - fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) # Change this line - -def reset(pixels): - for n in range(NP_COUNT): - pixels[n] = OFF - pixels[0] = BLUE - np.show() - -def shoot_firework(pixels): - for pixel in range(NP_COUNT - BURST_SIZE): - pixels[pixel] = RED - pixels.show() - sleep(20) - pixels[pixel] = OFF - pixels.show() - - -reset(np) -last_gesture = accelerometer.current_gesture() - -while True: - gesture = accelerometer.current_gesture() - if gesture != last_gesture or button_a.is_pressed(): - # set off a firework - shoot_firework(np) - explode(np) - reset(np) - last_gesture = gesture \ No newline at end of file diff --git a/fireworks7.py b/fireworks7.py deleted file mode 100644 index f65d731..0000000 --- a/fireworks7.py +++ /dev/null @@ -1,83 +0,0 @@ -from microbit import * -import neopixel -import random - -NP_COUNT = 60 -BURST_SIZE = 10 -DISPLAY_ANIMATION_SPEED = 500 # Add this line - -np = neopixel.NeoPixel(pin0, NP_COUNT) - -BLUE = (0, 0, 64) -RED = (64, 0, 0) -OFF = (0, 0, 0) - -FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) - , (255, 255, 128), (255, 128, 255), (128, 255, 255) - ] - -def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): - for i in range(first, last): - fade(pixels, i, fade_by=fade_by) - pixels.show() - -def fade(pixels, n, fade_by=0.9): - r, g, b = pixels[n] - pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) - -def explode(pixels): - initial_colour = random.choice(FIREWORK_COLOURS) - - for i in range(BURST_SIZE): - pixels[NP_COUNT - BURST_SIZE + i] = initial_colour - pixels[NP_COUNT - BURST_SIZE - i] = initial_colour - fade_all( pixels, fade_by=0.95 - , first=(NP_COUNT - BURST_SIZE - i) - , last= (NP_COUNT - BURST_SIZE + 1) - ) - - for _ in range(30): - fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) - -def reset(pixels): - for n in range(NP_COUNT): - pixels[n] = OFF - pixels[0] = BLUE - np.show() - display.show(Image.DIAMOND) # Add this line - - -def shoot_firework(pixels): - display.show(Image.ARROW_S) # Add this line - for pixel in range(NP_COUNT - BURST_SIZE): - pixels[pixel] = RED - pixels.show() - sleep(20) - pixels[pixel] = OFF - pixels.show() - - -reset(np) -display_timer = 0 # Add this line -last_gesture = accelerometer.current_gesture() - -while True: - gesture = accelerometer.current_gesture() - if gesture != last_gesture or button_a.is_pressed(): - # set off a firework - shoot_firework(np) - explode(np) - reset(np) - last_gesture = gesture - - ######################################### - # Add these lines - else: - # animate the waiting - display_timer = (display_timer + 1) % DISPLAY_ANIMATION_SPEED - if display_timer > (DISPLAY_ANIMATION_SPEED / 2): - display.show(Image.DIAMOND_SMALL) - else: - display.show(Image.DIAMOND) - # End of lines to add - ######################################### diff --git a/fireworks8.py b/fireworks8.py deleted file mode 100644 index 3777594..0000000 --- a/fireworks8.py +++ /dev/null @@ -1,95 +0,0 @@ -from microbit import * -import neopixel -import random -import radio # Add this line - -NP_COUNT = 60 -BURST_SIZE = 10 -DISPLAY_ANIMATION_SPEED = 500 - -np = neopixel.NeoPixel(pin0, NP_COUNT) - -radio.on() # Add this line - -BLUE = (0, 0, 64) -RED = (64, 0, 0) -OFF = (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) - ] - -def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): - for i in range(first, last): - fade(pixels, i, fade_by=fade_by) - pixels.show() - -def fade(pixels, n, fade_by=0.9): - r, g, b = pixels[n] - pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) - -def explode(pixels): - initial_colour = random.choice(FIREWORK_COLOURS) - - for i in range(BURST_SIZE): - pixels[NP_COUNT - BURST_SIZE + i] = initial_colour - pixels[NP_COUNT - BURST_SIZE - i] = initial_colour - fade_all( pixels, fade_by=0.95 - , first=(NP_COUNT - BURST_SIZE - i) - , last= (NP_COUNT - BURST_SIZE + 1) - ) - - for _ in range(30): - fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) - -def reset(pixels): - for n in range(NP_COUNT): - pixels[n] = OFF - pixels[0] = BLUE - np.show() - display.show(Image.DIAMOND) - - -def shoot_firework(pixels): - display.show(Image.ARROW_S) - for pixel in range(NP_COUNT - BURST_SIZE): - pixels[pixel] = RED - pixels.show() - sleep(20) - pixels[pixel] = OFF - pixels.show() - - -def all_firework(): - radio.send('fire') # Add this line - shoot_firework(np) - explode(np) - reset(np) - - -reset(np) -display_timer = 0 -radio_timer = 0 -last_gesture = accelerometer.current_gesture() - -while True: - gesture = accelerometer.current_gesture() - if gesture != last_gesture or button_a.is_pressed(): - # set off a firework - all_firework() - last_gesture = gesture - elif radio_timer > 0 and radio_timer == display_timer: - all_firework() - radio_timer = 0 - last_gesture = gesture - else: - # animate the waiting - display_timer = (display_timer + 1) % DISPLAY_ANIMATION_SPEED - if display_timer > (DISPLAY_ANIMATION_SPEED / 2): - display.show(Image.DIAMOND_SMALL) - else: - display.show(Image.DIAMOND) - received = radio.receive() - if received == 'fire' and random.randrange(5) == 0: - radio_timer = (display_timer + random.randint(50, 350)) % DISPLAY_ANIMATION_SPEED diff --git a/microbit-version/README.md b/microbit-version/README.md new file mode 100644 index 0000000..7d66312 --- /dev/null +++ b/microbit-version/README.md @@ -0,0 +1,72 @@ +In this session, you'll use a Raspberry Pi to program a Micro:bit, and the Microbit will control a NeoPixel strip and make it a one-dimensional firework. + +# Connecting the Micro:bit and Neopixel strip +* Connect the black lead on the Neopixel strip to the GND hole on the Microbit. +* Connect the red lead on the Neopixel to the 3V hole on the Microbit. +* Connect the white lead on the Neopixel to the 0 hole on the Microbit. +* Use the USB cable to connect the Microbit to the Raspberry Pi + +# Starting the Mu editor +Find the "Start" menu → Programming → mu +Once you've opened Mu, press the Mode button and press "BBC micro:bit" + +# Program 1: launching the firework +Type this program into the Mu editor. It will do the animation for a firework launching. + +Note that Python is really picky about +* spacing and indentation +* upper and lower case letters +* round and square brackets +* 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) + +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. + +When you press the A button on the Microbit, you should see a little light shoot along the strip. + +# Program 2: making the launch motion-sensitive +Pressing the button is OK, but let's make the firework launch if you shake the Microbit. + +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) + +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) + +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) + +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) + +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 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 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 diff --git a/microbit-version/fireworks1.py b/microbit-version/fireworks1.py new file mode 100644 index 0000000..fa3bb5c --- /dev/null +++ b/microbit-version/fireworks1.py @@ -0,0 +1,35 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) + +while True: + if button_a.is_pressed(): + # set off a firework + shoot_firework(np) + reset(np) diff --git a/microbit-version/fireworks2.py b/microbit-version/fireworks2.py new file mode 100644 index 0000000..bdfdaf6 --- /dev/null +++ b/microbit-version/fireworks2.py @@ -0,0 +1,37 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + +reset(np) +last_gesture = accelerometer.current_gesture() # Add this line + +while True: + gesture = accelerometer.current_gesture() # Add this line + if gesture != last_gesture or button_a.is_pressed(): # Change this line + # set off a firework + shoot_firework(np) + reset(np) + last_gesture = gesture # Add this line \ No newline at end of file diff --git a/microbit-version/fireworks3.py b/microbit-version/fireworks3.py new file mode 100644 index 0000000..a7f28cf --- /dev/null +++ b/microbit-version/fireworks3.py @@ -0,0 +1,52 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + + +######################################### +# Add this function +def explode(pixels): + initial_colour = (255, 128, 128) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + pixels.show() + sleep(500) +# End of function to add +######################################### + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) # Add this line + reset(np) + last_gesture = gesture diff --git a/microbit-version/fireworks4.py b/microbit-version/fireworks4.py new file mode 100644 index 0000000..dba124f --- /dev/null +++ b/microbit-version/fireworks4.py @@ -0,0 +1,64 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +######################################### +# Add these functions +def fade_all(pixels, fade_by=0.9): + for i in range(NP_COUNT): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) +# End of functions to add +######################################### + +def explode(pixels): + initial_colour = (255, 128, 128) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all(pixels, fade_by=0.95) # Add this line + # pixels.show() # Remove this line + # time.sleep(0.5) # Remove this line + + for _ in range(30): # Add this line + fade_all(pixels) # Add this line + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture \ No newline at end of file diff --git a/microbit-version/fireworks5.py b/microbit-version/fireworks5.py new file mode 100644 index 0000000..6f991ab --- /dev/null +++ b/microbit-version/fireworks5.py @@ -0,0 +1,64 @@ +from microbit import * +import neopixel +import random # Add this line + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (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) + ] + +def fade_all(pixels, fade_by=0.9): + for i in range(NP_COUNT): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) # Change this line + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all(pixels, fade_by=0.95) # Add this line + + for _ in range(30): + fade_all(pixels) + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture \ No newline at end of file diff --git a/microbit-version/fireworks6.py b/microbit-version/fireworks6.py new file mode 100644 index 0000000..1bef8b6 --- /dev/null +++ b/microbit-version/fireworks6.py @@ -0,0 +1,66 @@ +from microbit import * +import neopixel +import random + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) + , (255, 255, 128), (255, 128, 255), (128, 255, 255) + ] + +def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): # Change this line + for i in range(first, last): # Change this line + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all( pixels, fade_by=0.95 # Change this line + , first=(NP_COUNT - BURST_SIZE - i) # Add this line + , last= (NP_COUNT - BURST_SIZE + 1) # Add this line + ) # Add this line + + for _ in range(30): + fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) # Change this line + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture \ No newline at end of file diff --git a/microbit-version/fireworks7.py b/microbit-version/fireworks7.py new file mode 100644 index 0000000..f65d731 --- /dev/null +++ b/microbit-version/fireworks7.py @@ -0,0 +1,83 @@ +from microbit import * +import neopixel +import random + +NP_COUNT = 60 +BURST_SIZE = 10 +DISPLAY_ANIMATION_SPEED = 500 # Add this line + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) + , (255, 255, 128), (255, 128, 255), (128, 255, 255) + ] + +def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): + for i in range(first, last): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all( pixels, fade_by=0.95 + , first=(NP_COUNT - BURST_SIZE - i) + , last= (NP_COUNT - BURST_SIZE + 1) + ) + + for _ in range(30): + fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + display.show(Image.DIAMOND) # Add this line + + +def shoot_firework(pixels): + display.show(Image.ARROW_S) # Add this line + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +display_timer = 0 # Add this line +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture + + ######################################### + # Add these lines + else: + # animate the waiting + display_timer = (display_timer + 1) % DISPLAY_ANIMATION_SPEED + if display_timer > (DISPLAY_ANIMATION_SPEED / 2): + display.show(Image.DIAMOND_SMALL) + else: + display.show(Image.DIAMOND) + # End of lines to add + ######################################### diff --git a/microbit-version/fireworks8.py b/microbit-version/fireworks8.py new file mode 100644 index 0000000..3777594 --- /dev/null +++ b/microbit-version/fireworks8.py @@ -0,0 +1,95 @@ +from microbit import * +import neopixel +import random +import radio # Add this line + +NP_COUNT = 60 +BURST_SIZE = 10 +DISPLAY_ANIMATION_SPEED = 500 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +radio.on() # Add this line + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (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) + ] + +def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): + for i in range(first, last): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all( pixels, fade_by=0.95 + , first=(NP_COUNT - BURST_SIZE - i) + , last= (NP_COUNT - BURST_SIZE + 1) + ) + + for _ in range(30): + fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + display.show(Image.DIAMOND) + + +def shoot_firework(pixels): + display.show(Image.ARROW_S) + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +def all_firework(): + radio.send('fire') # Add this line + shoot_firework(np) + explode(np) + reset(np) + + +reset(np) +display_timer = 0 +radio_timer = 0 +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + all_firework() + last_gesture = gesture + elif radio_timer > 0 and radio_timer == display_timer: + all_firework() + radio_timer = 0 + last_gesture = gesture + else: + # animate the waiting + display_timer = (display_timer + 1) % DISPLAY_ANIMATION_SPEED + if display_timer > (DISPLAY_ANIMATION_SPEED / 2): + display.show(Image.DIAMOND_SMALL) + else: + display.show(Image.DIAMOND) + received = radio.receive() + if received == 'fire' and random.randrange(5) == 0: + radio_timer = (display_timer + random.randint(50, 350)) % DISPLAY_ANIMATION_SPEED diff --git a/microbit-version/neopixel-final.py b/microbit-version/neopixel-final.py new file mode 100644 index 0000000..334669f --- /dev/null +++ b/microbit-version/neopixel-final.py @@ -0,0 +1,88 @@ +from microbit import * +import neopixel +import random +import time + +NP_COUNT = 60 +BURST_SIZE = 10 +DISPLAY_ANIMATION_SPEED = 500 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +GREEN = (0, 64, 0) +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) + , (255, 255, 128), (255, 128, 255), (128, 255, 255) + ] + +def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): + for i in range(first, last): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + display.show(Image.DIAMOND) + +def shoot_firework(pixels): + display.show(Image.ARROW_S) + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + time.sleep(0.02) + pixels[pixel] = OFF + pixels.show() + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all( pixels, fade_by=0.95 + , first=(NP_COUNT - BURST_SIZE - i) + , last= (NP_COUNT - BURST_SIZE + 1) + ) + # fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE), fade_by=0.95) + # time.sleep(0.01) + + for _ in range(30): + fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) + # time.sleep(0.05) + +reset(np) +display_timer = 0 + +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture + else: + # animate the waiting + display_timer = (display_timer + 1) % DISPLAY_ANIMATION_SPEED + if display_timer > (DISPLAY_ANIMATION_SPEED / 2): + display.show(Image.DIAMOND_SMALL) + else: + display.show(Image.DIAMOND) + + + + + + \ No newline at end of file diff --git a/microbit-version/parts.db b/microbit-version/parts.db new file mode 100644 index 0000000..e69de29 diff --git a/microbit-version/strip-test1.py b/microbit-version/strip-test1.py new file mode 100644 index 0000000..0468e6f --- /dev/null +++ b/microbit-version/strip-test1.py @@ -0,0 +1,45 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT): + # pixels[pixel] = (32, 0, 32) + if pixel % 3 == 0: + pixels[pixel] = (32, 32, 0) + elif pixel % 3 == 2: + pixels[pixel] = (32, 0, 32) + else: + pixels[pixel] = (0, 32, 32) + pixels.show() + sleep(20) + + #for pixel in range(NP_COUNT): + # pixels[pixel] = OFF + # pixels.show() + + +reset(np) + +while True: + if button_a.is_pressed(): + # set off a firework + shoot_firework(np) + # reset(np) + if button_b.is_pressed(): + reset(np) \ No newline at end of file diff --git a/pi-pico-version/README.md b/pi-pico-version/README.md new file mode 100644 index 0000000..00a75f3 --- /dev/null +++ b/pi-pico-version/README.md @@ -0,0 +1,82 @@ +In this session, you'll use a Raspberry Pi to control a NeoPixel strip and make it a one-dimensional firework. + +# Connecting the NeoPixel to the Pi + +1. Disconnect the Pi from the USB cable +2. Connect the _red_ lead on the Neopixel to the _3V3_ pin on the breakout board +3. Connect the _black_ lead on the Neopixel to the _GND_ pin on the breakout board +4. Connect the _white_ lead on the Neopixel to pin _0_ on the breakout board +5. Reconnect the USB cable + +# Flash the Pimoroini firmware + +Follow the [Pimoroni instructions](https://learn.pimoroni.com/article/getting-started-with-pico#installing-the-custom-firmware) . As of time of writing, I'm using [pico-v1.23.0-1-pimoroni-micropython.uf2](https://github.com/pimoroni/pimoroni-pico/releases) + +# Starting the Thonny editor + +Have it auto-detect the Pi. + +# Installing the Neopixel library + +Neopixels need a library to drive the Neopixels. I use "blaz-r"'s [pi_pico_neopixel](https://github.com/blaz-r/pi_pico_neopixel?tab=readme-ov-file) library. Download the `neopixels.py` file, open it in Thonny, then save it as `neopixels.py` on the Pi. + +See the [library documentation](https://github.com/blaz-r/pi_pico_neopixel/wiki/Library-methods-documentation) for other things it can do. + + +# Program 1: launching the firework +Type this program into the Thonny editor. It will do the animation for a firework launching. + +Note that Python is really picky about +* spacing and indentation +* upper and lower case letters +* round and square brackets +* 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/pi-pico-version/main1.py) + +When you've typed it in, "Save As…" the file to `main.py` on the Pi Pico. Then press the "Run" button to put the program on the Pi Pico. + +When you press the Y button on the Pico Explorer board, you should see a little light shoot along the strip. + +# Program 2: making the launch automatic +Pressing the button is OK, but let's make the firework launch after a short delay. + +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) + +Again, save and flash the program. 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! + +> [Program 3](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/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) + +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) + +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. + +> [Program 6](https://github.com/NeilNjae/one-dimensional-fireworks/blob/master/fireworks6.py) + +# Program 7: Microbit 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) diff --git a/pi-pico-version/main1.py b/pi-pico-version/main1.py new file mode 100644 index 0000000..f7e30ad --- /dev/null +++ b/pi-pico-version/main1.py @@ -0,0 +1,42 @@ +from neopixel import Neopixel +from machine import Pin +import time +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) + +BLUE = (0, 0, 64, 0) +RED = (64, 0, 0, 0) +OFF = (0, 0, 0, 0) + +def reset(): + for n in range(NEOPIXEL_LENGTH): + np.set_pixel(n, OFF) + np.set_pixel(0, BLUE) + np.show() + +def shoot_firework(): + 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() + +reset() + +while True: + if button_y.raw(): + # set off a firework + shoot_firework() + reset() + + diff --git a/pi-pico-version/main2.py b/pi-pico-version/main2.py new file mode 100644 index 0000000..0de96a7 --- /dev/null +++ b/pi-pico-version/main2.py @@ -0,0 +1,48 @@ +from neopixel import Neopixel +from machine import Pin +import time +import random # Add this line +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) + +BLUE = (0, 0, 64, 0) +RED = (64, 0, 0, 0) +OFF = (0, 0, 0, 0) + +def reset(): + for n in range(NEOPIXEL_LENGTH): + np.set_pixel(n, OFF) + np.set_pixel(0, BLUE) + np.show() + +def shoot_firework(): + 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() + +reset() + +delay_timer = 100 # Add this line + +while True: + if button_y.raw() or delay_timer <= 0: # Modify this line + # set off a firework + shoot_firework() + reset() + delay_timer = random.randint(50, 100) # Add this line + else: # Add this line + delay_timer -= 1 # Add this line + time.sleep(0.1) # Add this line + + diff --git a/pi-pico-version/main3.py b/pi-pico-version/main3.py new file mode 100644 index 0000000..bd3dc4b --- /dev/null +++ b/pi-pico-version/main3.py @@ -0,0 +1,63 @@ +from neopixel import Neopixel +from machine import Pin +import time +import random # Add this line +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) + +BLUE = (0, 0, 64, 0) +RED = (64, 0, 0, 0) +OFF = (0, 0, 0, 0) + +def reset(): + for n in range(NEOPIXEL_LENGTH): + np.set_pixel(n, OFF) + np.set_pixel(0, BLUE) + np.show() + +def shoot_firework(): + 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() + +######################################### +# Add this function +def explode(): + initial_colour = (255, 128, 128, 0) + + 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) + np.show() + time.sleep(5) +# End of function to add +######################################### + + +reset() + +delay_timer = 100 # Add this line + +while True: + if button_y.raw() or delay_timer <= 0: + # set off a firework + shoot_firework() + explode() # Add this line + reset() + delay_timer = random.randint(50, 100) + else: + delay_timer -= 1 + time.sleep(0.1) + + diff --git a/pi-pico-version/main4.py b/pi-pico-version/main4.py new file mode 100644 index 0000000..5332d7d --- /dev/null +++ b/pi-pico-version/main4.py @@ -0,0 +1,75 @@ +from neopixel import Neopixel +from machine import Pin +import time +import random # Add this line +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) + +BLUE = (0, 0, 64, 0) +RED = (64, 0, 0, 0) +OFF = (0, 0, 0, 0) + +######################################### +# Add these functions +def fade_all(fade_by=0.9): + 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))) +# End of functions to add +######################################### + +def reset(): + for n in range(NEOPIXEL_LENGTH): + np.set_pixel(n, OFF) + np.set_pixel(0, BLUE) + np.show() + +def shoot_firework(): + 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 = (255, 128, 128, 0) + + 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) + # np.show() # Remove this line + # time.sleep(5) # Remove this line + for _ in range(30): # Add this line + fade_all() # 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() # Add this line + reset() + delay_timer = random.randint(50, 100) + else: + delay_timer -= 1 + time.sleep(0.1) + + diff --git a/pi-pico-version/main5.py b/pi-pico-version/main5.py new file mode 100644 index 0000000..2d00888 --- /dev/null +++ b/pi-pico-version/main5.py @@ -0,0 +1,75 @@ +from neopixel import Neopixel +from machine import Pin +import time +import random # Add this line +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) + +BLUE = (0, 0, 64, 0) +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) + ] + +def fade_all(fade_by=0.9): + 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(): + for n in range(NEOPIXEL_LENGTH): + np.set_pixel(n, OFF) + np.set_pixel(0, BLUE) + np.show() + +def shoot_firework(): + 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) # Change 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) + + for _ in range(30): + fade_all() + +reset() + +delay_timer = 100 # Add this line + +while True: + if button_y.raw() or delay_timer <= 0: + # set off a firework + shoot_firework() + explode() # Add this line + reset() + delay_timer = random.randint(50, 100) + else: + delay_timer -= 1 + time.sleep(0.1) + + diff --git a/pi-pico-version/main6.py b/pi-pico-version/main6.py new file mode 100644 index 0000000..3e2996f --- /dev/null +++ b/pi-pico-version/main6.py @@ -0,0 +1,78 @@ +from neopixel import Neopixel +from machine import Pin +import time +import random # Add this line +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) + +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) + ] + +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(): + for n in range(NEOPIXEL_LENGTH): + np.set_pixel(n, OFF) + np.set_pixel(0, BLUE) + np.show() + +def shoot_firework(): + 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) # Change 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.95 # 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 + +reset() + +delay_timer = 100 # Add this line + +while True: + if button_y.raw() or delay_timer <= 0: + # set off a firework + shoot_firework() + explode() # Add this line + reset() + delay_timer = random.randint(50, 100) + else: + delay_timer -= 1 + time.sleep(0.1) + + diff --git a/pi-pico-version/main7.py b/pi-pico-version/main7.py new file mode 100644 index 0000000..26a106a --- /dev/null +++ b/pi-pico-version/main7.py @@ -0,0 +1,95 @@ +from neopixel import Neopixel +from machine import Pin +import time +import random +from pimoroni import Button +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 +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), (128, 255, 128), (128, 128, 255) + , (255, 255, 128), (255, 128, 255), (128, 255, 255) + ] + +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(): + 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) # Add these display lines + 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) # Change 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.95 + , first=(NEOPIXEL_LENGTH - BURST_SIZE - i) + , last= (NEOPIXEL_LENGTH - BURST_SIZE + i) + ) + + for _ in range(30): + fade_all(first=(NEOPIXEL_LENGTH - 2 * BURST_SIZE)) + +reset() + +delay_timer = 100 # Add this line + +while True: + if button_y.raw() or delay_timer <= 0: + # set off a firework + shoot_firework() + explode() # Add this line + reset() + delay_timer = random.randint(50, 100) + else: + delay_timer -= 1 + 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.update() + time.sleep(0.1) + +