from machine import Pin
from time import *
from pin_numbers import *
print('LEDs + button toggle Example')
print('press the button to toggle between 2 LEDs')
r_led = Pin(D1, Pin.OUT)
y_led = Pin(D2, Pin.OUT)
btn = Pin(D8, Pin.IN, Pin.PULL_UP)
btn_val_last = 1
r_led.on()
led_state = 'red'
while True:
#print('btn =', btn.value())
# check that the button changed from high to low:
if btn.value() == 0: # button value is low
if btn_val_last == 1: # last button value was high
# toggle between yellow and red LEDs:
if led_state == 'yellow':
r_led.on()
y_led.off()
led_state = 'red'
elif led_state == 'red':
r_led.off()
y_led.on()
led_state = 'yellow'
print('led_state =', led_state)
# update the last button value:
btn_val_last = btn.value()
sleep_ms(100)