from machine import Pin , PWM
from time import sleep , ticks_ms
speaker_pin = 22
speaker = PWM(Pin(22))
def playtone(freq):
speaker.duty_u16(32767) # turn the PWM duty to 50%
speaker.freq(freq)
pin1 = Pin(5 , Pin.IN , Pin.PULL_DOWN)
pin2 = Pin(6 , Pin.IN , Pin.PULL_DOWN)
pin3 = Pin(7 , Pin.IN , Pin.PULL_DOWN)
pin4 = Pin(8 , Pin.IN , Pin.PULL_DOWN)
pin5 = Pin(9 , Pin.IN , Pin.PULL_DOWN)
pin6 = Pin(10 , Pin.IN , Pin.PULL_DOWN)
pin7 = Pin(11 , Pin.IN , Pin.PULL_DOWN)
pin8 = Pin(12 , Pin.IN , Pin.PULL_DOWN)
while True :
if pin1.value() == 1 :
playtone(220)
elif pin2.value() == 1 :
playtone(247)
elif pin3.value() == 1 :
playtone(262)
elif pin4.value() == 1 :
playtone(294)
elif pin5.value() == 1 :
playtone(330)
elif pin6.value() == 1 :
playtone(349)
elif pin7.value() == 1 :
playtone(392)
elif pin8.value() == 1 :
playtone(440)
else :
speaker.duty_u16(0)