Skip to main content

Botón SW1 enciende y apaga led azul

Adaptado de https://docs.keyestudio.com/projects/KS5009/en/latest/docs/Python/Python.html#python-projects

from machine import Pin
import time

button1 = Pin(26, Pin.IN, Pin.PULL_UP)
led = Pin(18, Pin.OUT)
count = 0

while True:
    btnVal1 = button1.value()  # Reads the value of button 1
    #print("button1 =",btnVal1)  #Print it out in the shell
    if(btnVal1 == 0):
        time.sleep(0.01)
        while(btnVal1 == 0):
            btnVal1 = button1.value()
            if(btnVal1 == 1):
                count = count + 1
                print(count)
    val = count % 2
    if(val == 1):
        led.value(1)
    else:
        led.value(0)
    time.sleep(0.1) #delay 0.1s