This should be an easy noob question. I'm having no end of problems trying to get used to MicroPython syntax. What a steep learning curve...
Anyway, I'd like to put a simple key trap on the following code so that when a button is pressed and the mode value changed, the program won't continue until the button is released. Is this called Debouncing too? Can someone fill in the missing bit or point me in the right direction please?
Code: Select all
from machine import Pin
import utime
global mode
global mode_max
global mode_min
mode = 1
mode_max = 5
mode_min = 1
led_1 = Pin(0, Pin.OUT)
led_2 = Pin(2, Pin.OUT)
led_3 = Pin(5, Pin.OUT)
led_4 = Pin(7, Pin.OUT)
led_5 = Pin(10, Pin.OUT)
button_up = Pin(18, Pin.IN, Pin.PULL_DOWN)
button_down = Pin(16, Pin.IN, Pin.PULL_DOWN)
while True:
if button_up.value() == 1:
print("Pressed Up ", mode)
led_1.value(1)
mode = mode+1
if mode >= mode_max:
mode = mode_max
utime.sleep(.01)
else:
led_1.value(0)
if button_down.value() == 1:
print("Pressed Down ", mode)
led_2.value(1)
mode = mode-1
if mode <= mode_min:
mode = mode_min
utime.sleep(.01)
else:
led_2.value(0)
My ultimate aim is to be able to count up and down, lighting the relevent LED to indicate the 'Mode'. Using a Pi Pico as the platform.