from machine import Pin, Timer import time led = Pin(25, Pin.OUT) btn_blu = Pin(5, Pin.IN, Pin.PULL_UP) btn_red = Pin(3, Pin.IN, Pin.PULL_UP) led_blu = Pin(4, Pin.OUT) led_red = Pin(2, Pin.OUT) def user_interaction(timeout): def _toggle_leds(timer): led_blu.toggle() led_red.toggle() tmr = Timer() tmr.init(freq=2, mode=Timer.PERIODIC, callback=_toggle_leds) startime = time.ticks_ms() timeout *= 1000 while time.ticks_diff(time.ticks_ms(), startime) < timeout: b, r = btn_blu.value(), btn_red.value() if (b + r) < 2: tmr.deinit() return 'B' if not b else 'R' tmr.deinit() led_blu.value(0) led_red.value(0) return 'T' # Timeout def recording(timeout): timeout *= 1000 startime = time.ticks_ms() endtime = time.ticks_add(startime, timeout) while time.ticks_diff(time.ticks_ms(), startime) < timeout: if not btn_blu.value(): led.value(0) return 'I' restime = time.ticks_diff(endtime, time.ticks_ms()) if restime < 5000: led.value((restime // 500) % 2) time.sleep_ms(50) return 'OK' if __name__ == '__main__': print(user_interaction(20)) #print(recording(20))