How to debounce the USR switch?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
on4aa
Posts: 70
Joined: Sat Nov 11, 2017 8:41 pm
Location: Europe
Contact:

How to debounce the USR switch?

Post by on4aa » Fri Nov 24, 2017 9:33 pm

I currently have this tiny bit of code on a Pyboard v1.1:

Code: Select all

import pyb

def toggle_led():
    ​​pyb.LED(3).toggle()

pyb.Switch().callback(toggle_led)
However, this set-up is susceptible to contact bounce.
I tried adding a pyb.delay(500) to no avail.
Is there some elegant way to remedy switch bounce on the Pyboard?

PS: There are also points to be gained over on https://stackoverflow.com/q/47479907/2192488

User avatar
on4aa
Posts: 70
Joined: Sat Nov 11, 2017 8:41 pm
Location: Europe
Contact:

Re: How to debounce the USR switch?

Post by on4aa » Sun Nov 26, 2017 10:26 pm

I came up with below solution, which for all intends and purposes works fine for me.
However, there is a far more intelligent (and involved) solution posted elsewhere on this forum.

Code: Select all

import pyb

def toggle_led():
    pyb.disable_irq()
    pyb.delay(100)
    if sw.value(): pyb.LED(3).toggle()
    pyb.enable_irq()

sw = pyb.Switch()
sw.callback(toggle_led)
Serge

User avatar
on4aa
Posts: 70
Joined: Sat Nov 11, 2017 8:41 pm
Location: Europe
Contact:

Re: How to debounce the USR switch?

Post by on4aa » Wed Dec 20, 2017 3:32 pm

There is a much better way without a need for interrupts.

Here is a link to the buttons example in Peter Hinch's excellent uasyncio tutorial.
Serge

Post Reply