from machine import PWM, Pin
import array
from time import sleep, sleep_ms
sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico!")
# methods: deinit(), freq([f]), duty_u16([d]), duty_ns([d])
out = PWM(Pin(0))
tones = array.array('H',(500,550,800,9000,500,550,800,9000,800,500,550))
bach_style_tune = [
(440, 300), # A4 - Start of an arpeggio
(523, 300), # C5
(659, 300), # E5
(440, 300), # A4 - Repeat lower octave
(659, 300), # E5 - Descending motion
(523, 300), # C5
(440, 300), # A4
(392, 300), # G4 - Transition to different chord
(494, 300), # B4
(587, 300), # D5
(392, 300), # G4
(587, 300), # D5 - Descending motion
(494, 300), # B4
(392, 300), # G4
(349, 600), # F4 - Longer note for dramatic effect
(440, 300), # A4 - Restart similar pattern in different scale
(523, 300), # C5
(659, 300), # E5
(440, 300), # A4
(659, 300), # E5
(523, 300), # C5
(440, 300), # A4
(392, 300), # G4 - End on G major chord
(494, 300), # B4
(587, 300), # D5
(392, 900) # G4 - End note
]
bach_style_lower_octave_tune = [
(220, 300), # A3 - Start of an arpeggio, one octave lower
(262, 300), # C4
(330, 300), # E4
(220, 300), # A3 - Repeat lower octave
(330, 300), # E4 - Descending motion
(262, 300), # C4
(220, 300), # A3
(196, 300), # G3 - Transition to different chord
(247, 300), # B3
(294, 300), # D4
(196, 300), # G3
(294, 300), # D4 - Descending motion
(247, 300), # B3
(196, 300), # G3
(175, 600), # F3 - Longer note for dramatic effect
(220, 300), # A3 - Restart similar pattern in different scale
(262, 300), # C4
(330, 300), # E4
(220, 300), # A3
(330, 300), # E4
(262, 300), # C4
(220, 300), # A3
(196, 300), # G3 - End on G major chord
(247, 300), # B3
(294, 300), # D4
(196, 900) # G3 - End note
]
bach_style_transition_verse = [
(440, 300), # A4 - Slightly prolonged note for emphasis
(523, 150), # C5 - Shorten for variation
(659, 150), # E5
(523, 300), # C5 - Return to original note
(440, 300), # A4
(392, 300), # G4 - Maintain base arpeggio form
(494, 150), # B4
(587, 150), # D5
(494, 300), # B4
(392, 300), # G4
(349, 300), # F4 - Prolonged note adds a reflective mood
(440, 300), # A4 - Echo the start of the transition
(523, 150), # C5
(659, 150), # E5
(523, 300), # C5
(440, 300), # A4
(392, 300), # G4 - Final arpeggio in transition
(494, 150), # B4
(587, 150), # D5
(494, 300), # B4
(392, 900) # G4 - Long ending note for closure
]
# Function to play a tone
def play_tone(frequency, duration):
duration = int(duration*.5)
print(frequency,duration)
if frequency == 0:
out.duty_u16(0) # No sound for pause (frequency=0)
else:
out.freq(frequency) # Set frequency for the note
out.duty_u16(32767) # Set duty cycle to 50% to sound buzzer
sleep_ms(duration)
out.duty_u16(0) # Stop the buzzer
#out.deinit()
# Function to play the entire tune
def play_tune(tune):
for note in tune:
play_tone(note[0], note[1])
# Play the tune
while(1):
play_tune(bach_style_tune)
play_tune(bach_style_transition_verse)
play_tune(bach_style_lower_octave_tune)
play_tune(bach_style_transition_verse)
#.03, 0.2
dur = 0.01
pause = 0.055
def spider_sound():
for loop in range(3):
for tone in tones:
print(tone,' ',end='')
out.duty_u16(1<<8)
out.freq(tone)
sleep(dur)
out.duty_u16(0)
sleep(pause)
def flea_sound():
dur = 0.02
out.duty_u16(1<<8)
for tone in range(200,100,-1):
out.freq(tone)
sleep(dur)
def centi_sound():
dur = 0.02
pause = 0.25
out.duty_u16(1<<8)
for loop in range(5):
out.duty_u16(1<<8)
out.freq(100)
sleep(dur)
out.duty_u16(0)
sleep(pause)
#flea_sound()
#centi_sound()
out.deinit()