from picozero import Speaker
from machine import Pin
from utime import sleep
from time import ticks_ms
push_bt1 = Pin(0, Pin.IN, Pin.PULL_UP)
push_bt2 = Pin(1, Pin.IN, Pin.PULL_UP)
# creating a Speaker object
speaker = Speaker(16)
# global value
button1_is_pressed = False
button2_is_pressed = False
debounce_time=0
DEBOUNCE_WAIT_TIME=500
speaker_freq = 500
speaker_is_playing = False
# Interrupt Service Routine for Button Pressed Events with debounce
def button1_pressed(pin):
global button1_is_pressed
global debounce_time
if (ticks_ms() - debounce_time) > DEBOUNCE_WAIT_TIME:
button1_is_pressed = True
debounce_time=ticks_ms()
def button2_pressed(pin):
global button2_is_pressed
global debounce_time
if (ticks_ms() - debounce_time) > DEBOUNCE_WAIT_TIME:
button2_is_pressed = True
debounce_time=ticks_ms()
# here is how we associate the falling value on the input pin with the callback function
push_bt1.irq(handler=button1_pressed, trigger=Pin.IRQ_FALLING)
push_bt2.irq(handler=button2_pressed, trigger=Pin.IRQ_FALLING)
while True:
if (button2_is_pressed == True):
button2_is_pressed = False
if (speaker_is_playing == False):
speaker_is_playing = True
speaker.play(tune=speaker_freq, duration=1, volume=1, n=None, wait=False)
else:
speaker_is_playing = False
speaker.off()
if (button1_is_pressed == True):
button1_is_pressed = False
if (speaker_freq == 10000):
speaker_freq = 500
else:
speaker_freq +=500
if (speaker_is_playing == True):
speaker.play(tune=speaker_freq, duration=1, volume=1, n=None, wait=False)
sleep(0.05)