from machine import Pin, PWM, I2C
from time import sleep
from pico_i2c_lcd import I2cLcd
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
buzzer = PWM(Pin(15))
tones = {
'C': 261,
'D': 294,
'E': 329,
'F': 349,
'G': 392,
'A': 440,
'B': 493,
' ': 0
}
melody = [
# Verse
('G', 0.4),('G', 0.3), ('D', 0.4), ('C', 0.4), ('G', 1),
('G', 0.3),('G', 0.3), ('G', 0.4), ('E', 0.4), ('D', 0.4), ('C', 0.3), ('A', 1),
('A', 0.4), ('F', 0.4), ('E', 0.4), ('D', 0.4), ('B', 1),
('G', 0.4),('G', 0.3), ('F', 0.4), ('D', 0.4), ('E', 1),
('G', 0.4), ('E', 0.4), ('D', 0.3),('C', 0.2), ('G', 0.6),
('G', 0.4),('E', 0.3), ('D', 0.4),('C', 0.3), ('A', 1),
('A', 0.4), ('A', 0.4), ('F', 0.4), ('E', 0.4), ('D', 0.4), ('G', 0.4), ('G', 0.4), ('G', 0.4),
('G', 0.4), ('A', 0.4),('G', 0.3), ('F', 0.4), ('D', 0.3), ('C', 1.2),
# Chorus
('E', 0.5), ('E', 0.5), ('E', 0.5), ('E', 0.5),
('E', 0.4), ('G', 0.4), ('C', 0.4), ('D', 0.4), ('E', 1.5),
('F', 0.5), ('F', 0.5), ('F', 0.8), ('F', 0.4),
('E', 0.5), ('E', 0.7), ('E', 0.5), ('E', 0.5), ('D', 0.4),
('D', 0.4), ('E', 0.4), ('D', 0.7), ('G', 0.8),
# Repeat Chorus
('E', 0.5), ('E', 0.5), ('E', 0.5), ('E', 0.5),
('E', 0.4), ('G', 0.4), ('C', 0.4), ('D', 0.4), ('E', 1.5),
('F', 0.5), ('F', 0.5), ('F', 0.8), ('F', 0.4),
('E', 0.5), ('E', 0.7), ('E', 0.5), ('E', 0.5), ('D', 0.4),
('D', 0.4), ('E', 0.4), ('D', 0.7), ('G', 0.5)
]
lyrics = [
# Verse
"Dashing","", "through", "the", "snow",
"in", "a", "one", "horse", "open","", "sleigh",
"o'er", "the", "fields", "we", "go",
"laughing","", "all", "the", "way",
"bells", "on", "bobtail","", "ring",
"making","", "spirits","", "bright",
"what", "fun", "it", "is", "to", "ride", "and", "sing",
"a", "sleighing","", "song", "","tonight",
# Chorus
"Jingle", "bells,", "jingle", "bells,",
"jingle","", "all", "the", "way!",
"Oh!", "What", "fun", "it is",
"to", "ride", "in a", "one", "horse",
"open","", "sleigh!", "Hey!",
# Repeat Chorus
"Jingle", "bells,", "jingle", "bells,",
"jingle","", "all", "the", "way!",
"Oh!", "What", "fun", "it is",
"to", "ride", "in a", "one", "horse",
"open","", "sleigh!", "Hey!"
]
def play_tone(note, duration):
if note == " ":
sleep(duration)
else:
buzzer.freq(tones[note])
buzzer.duty_u16(1000)
sleep(duration)
buzzer.duty_u16(0)
def center_text(text, width=16):
spaces = (width - len(text)) // 2
return " " * spaces + text
def display_lyrics(line):
lcd.clear()
lcd.putstr(center_text(line))
def play_song(melody, lyrics):
for i, (note, duration) in enumerate(melody):
if i < len(lyrics):
display_lyrics(lyrics[i])
play_tone(note, duration)
lcd.clear()
lcd.putstr(center_text("Merry Xmas!"))
lcd.move_to(0, 1)
lcd.putstr(center_text("Sir Chavez"))
try:
while True:
lcd.putstr(center_text("Jingle Bells"))
sleep(2)
lcd.clear()
play_song(melody, lyrics)
sleep(5)
except KeyboardInterrupt:
buzzer.duty_u16(0)
lcd.clear()
lcd.putstr(center_text("Stopped"))