* 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.
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
> [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!
> [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
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:
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):
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:
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(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()
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:
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)
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()
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:
--- /dev/null
+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)
+
+