"""
Press and hold the button for a second to start
playing "we wish you a merry christmas"
and again to stop. for now the snowflake animation pauses
since wokwi doesn't like multithreading
"""
import time
import _thread
time.sleep(0.1) # Wait for USB to become ready
from neopixel import Neopixel
import random
from machine import PWM
from utime import sleep
from melodies import *
from notes import *
num_leds = 10
pin = 0
state_machine = 0
strip = Neopixel(num_leds, state_machine, pin, mode="RGB")
strip.brightness(50)
led_structure = [
[0, 1, 2], # Row 1: D1, D2, D3
[7, 3], # Row 2: D8, D4
[6, 5, 4], # Row 3: D7, D6, D5
[9, 8] # Row 4: D10, D9
]
wait = 0.5
from machine import Pin
import time
button = Pin(14, Pin.IN, Pin.PULL_UP)
snowflakes = [0] * num_leds
song = False
# https://github.com/twisst/Music-for-Raspberry-Pi-Pico/blob/main/play.py
buzzer = PWM(Pin(15))
track = 23
volume = 600
def playtone(frequency):
buzzer.duty_u16(volume)
buzzer.freq(frequency)
def be_quiet():
buzzer.duty_u16(0)
def duration(tempo, t):
wholenote = (60000 / tempo) * 4
if t > 0:
noteDuration = wholenote // t
elif (t < 0):
noteDuration = wholenote // abs(t)
noteDuration *= 1.5
return noteDuration
def playsong(mysong):
try:
print(mysong[0])
tempo = mysong[1]
for thisNote in range(2, len(mysong), 2):
noteduration = duration(tempo, int(mysong[thisNote+1]))
if (mysong[thisNote] == "REST"):
be_quiet()
else:
playtone(notes[mysong[thisNote]])
sleep(noteduration*0.9/1000)
be_quiet()
sleep(noteduration*0.1/1000)
if button.value() == 0:
print("escaping")
song = not song
while button.value() == 0: time.sleep(0.01)
return
except:
be_quiet()
while True:
if button.value() == 0:
while button.value() == 0: time.sleep(0.01)
print("pressed")
song = not song
color = (255, 255, 255)
for led in led_structure[0]:
if random.random() < 0.5:
snowflakes[led] = color
else:
snowflakes[led] = (0, 0, 0)
# this bit is courtesy copilot
for row in range(len(led_structure) - 1, 0, -1):
for i in range(len(led_structure[row])):
snowflakes[led_structure[row][i]] = snowflakes[led_structure[row-1][i % len(led_structure[row-1])]]
print(snowflakes)
for i in range(len(snowflakes)):
if snowflakes[i] != 0:
strip.set_pixel(i, snowflakes[i])
else:
strip.set_pixel(i, (0, 0, 0))
strip.show()
time.sleep(wait)
if song:
playsong(melody[track])
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
rgb1:VDD
rgb1:DOUT
rgb1:VSS
rgb1:DIN
rgb2:VDD
rgb2:DOUT
rgb2:VSS
rgb2:DIN
rgb3:VDD
rgb3:DOUT
rgb3:VSS
rgb3:DIN
rgb4:VDD
rgb4:DOUT
rgb4:VSS
rgb4:DIN
rgb5:VDD
rgb5:DOUT
rgb5:VSS
rgb5:DIN
rgb6:VDD
rgb6:DOUT
rgb6:VSS
rgb6:DIN
rgb7:VDD
rgb7:DOUT
rgb7:VSS
rgb7:DIN
rgb8:VDD
rgb8:DOUT
rgb8:VSS
rgb8:DIN
rgb9:VDD
rgb9:DOUT
rgb9:VSS
rgb9:DIN
rgb10:VDD
rgb10:DOUT
rgb10:VSS
rgb10:DIN
bz1:1
bz1:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r