Skip to content

Our Third Lab: Reading a Button Press

Solution: Polling Method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Author: J.E. Tannenbaum
# Initial Release: 01/13/2022
# Alternating blinking leds
from machine import Pin
from time import sleep

# Define the pin for each led
led1 = Pin(16, Pin.OUT)
led2 = Pin(17, Pin.OUT)

# define the pin for the button
button = Pin(15, Pin.IN, Pin.PULL_DOWN)

# make sure one is off and one is on
led1.low()
led2.high()

# Toggle when the button is pressed
while True:

    # Was the button pressed?
    if button.value():
        led1.toggle()
        led2.toggle()

        # This is delay to give time to get finger off the button.  
        sleep(0.5)