Files
hw-hacking-101/code-snippets/Button_LED_Test.py

55 lines
897 B
Python

## Simple Button and LED test
# yellow led will blink
# red + green will be off button is pressed
from machine import Pin
import time
## define led pins
led_gr_p = 21
led_re_p = 22
led_ye_p = 17
## define switch pins
sw1_p = 32
sw2_p = 33
## init leds
led_gr = Pin(led_gr_p, Pin.OUT)
led_gr.off() # switch on led - inverted logic!
led_re = Pin(led_re_p, Pin.OUT)
led_re.off() # switch on led - inverted logic!
led_ye = Pin(led_ye_p, Pin.OUT)
led_ye.off() # switch on led - inverted logic!
## int buttons (pull-up)
sw1 = Pin(sw1_p, Pin.IN)
sw2 = Pin(sw2_p, Pin.IN)
while True:
if sw1.value():
print("sw1 true")
led_re.on()
#led_ye.on()
else:
print("sw1 false")
led_re.off()
#led_ye.off()
if sw2.value():
print("sw2 true")
led_gr.on()
else:
print("sw2 false")
led_gr.off()
# toggle led
if led_ye.value():
led_ye.off();
else:
led_ye.on();
time.sleep(0.5)