Simple Key Trap

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
DestinysAgent
Posts: 3
Joined: Fri Feb 12, 2021 12:01 pm

Simple Key Trap

Post by DestinysAgent » Fri Feb 12, 2021 12:12 pm

Hi Guys,

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.

DestinysAgent
Posts: 3
Joined: Fri Feb 12, 2021 12:01 pm

Re: Simple Key Trap

Post by DestinysAgent » Fri Feb 12, 2021 2:02 pm

UPDATE: Issue solved - Thanks

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: Simple Key Trap

Post by Christian Walther » Fri Feb 12, 2021 3:03 pm

Reword your requirement a little and you have the answer: “don't continue until the button is released” = “while the button is pressed, don’t continue” =

Code: Select all

while button_up.value() == 1:
    utime.sleep(.01)
Substitute that for your original utime.sleep(.01).

Blocking the program while the button is pressed is fine for such a simple program, but in a more complex application you may not want to do that: e.g. when you also have a display that you still want to update regularly. A better solution in that case is: store the value of the button in a variable, then in the next iteration compare the current state to the saved state and only perform the action when the previous state was “released” and the new state is “pressed”.

I wouldn’t call this “debouncing”, although your current code debounces as a side effect because it always sleeps for at least 10 ms after a button press is detected, by which time the bouncing should be over. The revised code (with my first suggestion) no longer does that, but you could always insert another sleep(.01) before the while loop if you observe bouncing.

By the way, in your

Code: Select all

global mode
the global doesn’t do anything. Variables assigned at the top level of the module like that are always global. The global keyword is only needed inside functions, where assigning to a variable would otherwise create a variable local to that function.

UPDATE: answer already written, so you get it anyway :)

DestinysAgent
Posts: 3
Joined: Fri Feb 12, 2021 12:01 pm

Re: Simple Key Trap

Post by DestinysAgent » Sat Feb 13, 2021 10:54 am

Thanks for your advice Christian.

Post Reply