import time
time.sleep_ms(100) # Wait for USB to become ready
print("Hello, Pi Pico!")
# Eden Haik
# https://wokwi.com/projects/479803151029213185
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}
lownotes = {'Do': 262, 'Re': 294, 'Mi':330, 'Fa':349, 'Sol': 392, 'La':440, 'Ti':494, 'Rest':0}
highnotes = {'Do': 1047, 'Re': 1175, 'Mi':1319, 'Fa':1397, 'Sol': 1568, 'La':1760, 'Ti':1976, '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, 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 seconds
"""
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 int(vlength/ 1000)
#Play the reverse verse
#play_verse(reverse)
# Step 5 - Play the upreverse verse
#play_verse(upverse)
#Step 6- highnotes
#play_verse(reverse, highnotes)
#play_verse(upverse, mynotes=highnotes)
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)]
#play_verse(v1, mynotes= lownotes)
# Create the song as a list of verses
song = [v1, v2, v3, v4, v1, v2]
#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 seconds
"""
slength = 0
for verse in mysong:
vlength = play_verse (verse, mynotes, speed)
slength += vlength
return slength
#Step 11- play the full song
slength = play_song (song, speed=4)
slength += play_song(song, lownotes, speed=4)
slength += play_song(song, highnotes, speed=4)
green.on()
# Play the song
term.print(f"Total length: {slength / 1000} seconds")
green.off()