import machine
import asyncio
from pico_song_data import b1_freqs, b1_durs_ms, b2_freqs, b2_durs_ms
# Initialize the PWM pins for the buzzers
buzzer_1 = machine.PWM(machine.Pin(14))
buzzer_2 = machine.PWM(machine.Pin(15))
async def play_track(buzzer, freqs, durs_ms):
"""
Plays a sequence of frequencies and durations asynchronously.
"""
# Iterate through both arrays simultaneously
for freq, dur in zip(freqs, durs_ms):
if freq > 0:
buzzer.freq(freq)
# 32768 is exactly 50% duty cycle (65535 / 2)
buzzer.duty_u16(32768)
else:
# Turn off the buzzer for rests
buzzer.duty_u16(0)
# Yield control back to the main loop so the other buzzer can update
await asyncio.sleep(dur / 1000.0)
# Ensure the buzzer is completely muted when the track finishes
buzzer.duty_u16(0)
async def main():
print("Starting dual-track playback...")
# Schedule both tracks to start at the exact same time
task1 = asyncio.create_task(play_track(buzzer_1, b1_freqs, b1_durs_ms))
task2 = asyncio.create_task(play_track(buzzer_2, b2_freqs, b2_durs_ms))
# Wait for both tracks to finish playing
await asyncio.gather(task1, task2)
print("Song complete!")
# Kick off the asynchronous event loop
try:
asyncio.run(main())
except KeyboardInterrupt:
# Failsafe to mute buzzers if you hit stop in your IDE
buzzer_1.duty_u16(0)
buzzer_2.duty_u16(0)