#define LED 10
#define btn 2
from machine import pin
from time import sleep
btn_state = 0;
flag = 0;
lastDebounceTime = 0; // the last time the output pin was toggled
debounceDelay = 200; // the debounce time
def setup:
pinMode(LED, OUTPUT);
pinMode(btn, INPUT);
digitalWrite(LED, LOW);
def loop:
btn_state = digitalRead(btn);
//Filters out the noise by setting a time buffer
if ( (millis() - lastDebounceTime) > debounceDelay):
if(btn_state == HIGH):
if(flag == 0):
digitalWrite(LED, HIGH);
flag = 1;
lastDebounceTime = millis(); //Sets current time
elif(flag == 1):
digitalWrite(LED, LOW);
flag = 0;
lastDebounceTime = millis(); //Sets current time
# modules
from machine import Pin
from time import sleep
LED = Pin(15, Pin.OUT) # creating LED object, setting it as OUT
BUTTON = Pin(16, Pin.IN) # creating Push Button object, setting it as IN
# continuously read the signal from the push button while the board has power
while True:
# if the signal from the button is HIGH (button is pressed), turn LED on
if BUTTON.value() == 1:
LED.on()
sleep(0.1)
# otherwise, turn the LED off
else:
LED.off()