# Project objective: To test a passive buzzer to play a melody at one-second intervals with start/stop functionality using a button.
#
# Hardware and connections used:
# - Passive buzzer GND to Raspberry Pi Pico GND
# - Passive buzzer + Pin to GPIO Pin 15
# - Button one pin to GPIO Pin 14 and the other to GND
#
# Programmer: Adrian Josele G. Quional
from picozero import Speaker, Button
from time import sleep
# Creating Speaker object connected to GPIO 15
speaker = Speaker(15)
# Creating Button object connected to GPIO 14
button = Button(14)
# Melody to be played by the passive buzzer
melody = [262, 294, 330, 349, 392, 440, 494, 523] # C major scale
# Variable to track if the alarm is active
alarm_active = False
# Function to toggle the alarm state
def toggle_alarm():
global alarm_active
alarm_active = not alarm_active
# Attach the toggle_alarm function to the button press event
button.when_pressed = toggle_alarm
# Main loop
while True:
if alarm_active:
for note in melody:
speaker.play(note)
sleep(0.5)
sleep(1)
else:
speaker.off()
sleep(0.1) # Small delay to avoid excessive CPU usage