import time
time.sleep_ms(100) # Wait for USB to become ready
print("Hello, Pi Pico!")
# Jit Sengupta
# https://wokwi.com/projects/479802863407870977
from Lights import *
from Buzzer import *
from TerminalEmulator import *
red = Light(15, 'Red') # create a red light on pin 15
green = Light(13, 'Green') # create a green light on pin 13
beeper = PassiveBuzzer(14, 'Beeper') # Create a passive buzzer named beeper on 14
term = TerminalEmulator() # Create a terminal emulator with default parameters
# The rest of this code was from last week - I have removed the steps
# But kept the functionality of what each code does
basenotes = {'Do': 523, 'Re': 587, 'Mi':659, 'Fa':698, 'Sol': 784, 'La':880, 'Ti':988, 'Rest':0}
# Step 4 - create the low and high notes
lownotes = {'Do': 262, 'Re': 294, 'Mi':330, 'Fa':349, 'Sol': 392, 'La':440, 'Ti':494, 'Rest':0}
highnotes = {'Do': 523, 'Re': 587, 'Mi':659, 'Fa':698, 'Sol': 784, 'La':880, 'Ti':988, 'Rest':0}
red.on()
# create the "upverse" and "reverse" verses
upverse = [ ('Do', 250, 250), ('Re', 250, 250),('Mi', 250, 250), ('Fa', 250, 250), ('Sol',250,250), ('La', 250, 250), ('Ti', 250, 250)]
reverse = [ ('Ti', 250, 250), ('La', 250, 250),('Sol', 250, 250), ('Fa', 250, 250), ('Mi',250,250), ('Re', 250, 250), ('Do', 250, 250)]
# Step 7 define the play_verse function
# Step 8 and 9 and 10 also here verse parameter, notes parameter and default parameter
def play_verse(myverse, mynotes=basenotes, speed=1):
"""
Play the verse myverse that is sent via the parameter from the mynotes note set
Returns the length of the verse in milliseconds
"""
vlength = 0
for mynote in myverse:
tone = mynote[0]
length = int(mynote[1] / speed)
pause = int(mynote[2] / speed)
beeper.beep(mynotes[tone], length)
time.sleep_ms(pause)
vlength += length + pause
return vlength / 1000
# Play the reverse verse
#play_verse(reverse)
# Step 5 - play the upverse
#play_verse(upverse)
# Step 6 - play the high notes forward and reverse
#play_verse(reverse, lownotes)
#play_verse(reverse, mynotes=lownotes)
red.off()
# Let's pause a bit
time.sleep_ms(500)
# Create the verses of Twinkle Twinkle
v1 = [('Do', 250, 250), ('Do', 250, 250), ('Sol', 250, 250), ('Sol', 250, 250), ('La', 250, 250), ('La', 250, 250), ('Sol', 250, 250), ('Rest', 250, 250)]
v2 = [('Fa', 250, 250), ('Fa', 250, 250), ('Mi', 250, 250), ('Mi', 250, 250), ('Re', 250, 250), ('Re', 250, 250), ('Do', 250, 250), ('Rest', 250, 250)]
v3 = [('Sol', 250, 250), ('Sol', 250, 250), ('Fa', 250, 250), ('Fa', 250, 250), ('Mi', 250, 250), ('Mi', 250, 250), ('Re', 250, 250), ('Rest', 250, 250)]
v4 = [('Sol', 250, 250), ('Sol', 250, 250), ('Fa', 250, 250), ('Fa', 250, 250), ('Mi', 250, 250), ('Mi', 250, 250), ('Re', 250, 250), ('Rest', 250, 250)]
# Create the song as a list of verses
song = [v1, v2, v3, v4, v1, v2]
green.on()
# Play the song
songlength = 0
# Step 12 - create the play_song function
def play_song(mysong, mynotes=basenotes, speed=1):
"""
Play the song mysong using the mynotes dictionary of notes
returns the total length of the song in milliseconds
"""
slength = 0
for verse in mysong:
vlength = play_verse(verse, mynotes, speed)
slength += vlength
return slength
# Step 11 - play the entire song
# Step 12 - use the play_song function
songlength = play_song(song, speed=4)
print(songlength)
time.sleep_ms(2000)
songlength += play_song(song, lownotes, speed=4)
print(songlength)
time.sleep_ms(2000)
songlength += play_song(song, highnotes, speed=4)
print(songlength)
term.print(f"Total length: {songlength /1000} seconds")
green.off()