from machine import Pin, PWM
import time
import urandom
# Buttons
buttons = [
Pin(16, Pin.IN, Pin.PULL_UP),
Pin(17, Pin.IN, Pin.PULL_UP),
Pin(18, Pin.IN, Pin.PULL_UP)
]
# Buzzer
buzzer = PWM(Pin(15))
buzzer.duty_u16(0)
tones = [600, 800, 1000]
sequence = []
def beep(freq, dur=0.25):
buzzer.freq(freq)
buzzer.duty_u16(30000)
time.sleep(dur)
buzzer.duty_u16(0)
time.sleep(0.1)
print("Simon Says - Sound Edition")
while True:
# Add new step
sequence.append(urandom.randint(0, 2))
# Play sequence
for step in sequence:
beep(tones[step])
# Player input
for step in sequence:
while True:
for i, btn in enumerate(buttons):
if btn.value() == 0:
beep(tones[i], 0.15)
if i != step:
beep(200, 0.6)
print("Game Over! Score:", len(sequence)-1)
sequence = []
time.sleep(0.3)
break
else:
continue
break
if not sequence:
break
time.sleep(0.5)