import board import touchio from adafruit_debouncer import Debouncer import pwmio speaker = pwmio.PWMOut(board.GP16, variable_frequency=True) volume = 32768 speaker.duty_cycle = 0 pads = [board.GP0, board.GP1, board.GP2, board.GP3, board.GP4, board.GP5, board.GP6, board.GP7, board.GP8, board.GP9, board.GP10, board.GP11] notes = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B'] freqs = [262, 277, 294, 311, 330, 349, 370, 392, 410, 440, 466, 494] btns = [] n_btns = 0 for pad in pads: print(pad) touchin = touchio.TouchIn(pad) btns.append(Debouncer(touchin)) n_btns += 1 def play(freq): speaker.duty_cycle = volume speaker.frequency = freq def stop(): speaker.duty_cycle = 0 while True: for i in range(n_btns): btn = btns[i] btn.update() if btn.rose: print(f"{notes[i]} pressed!") play(freqs[i]) if btn.fell: print(f"{notes[i]} released!") stop()