Bouncing inputs

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
surmanajaja
Posts: 1
Joined: Thu Feb 05, 2015 2:22 pm

Bouncing inputs

Post by surmanajaja » Thu Feb 05, 2015 2:50 pm

Hi


I have this testcode to test bouncing inputs, and I can't figure out how to solve this by code. each time I pull my X3 hi I get 1 to four calbacks. seems like disabling interrupts is way too slow. How to solve this by code? Ideas?

Code: Select all

import pyb

pin = pyb.Pin('X3',pyb.Pin.IN,pyb.Pin.PULL_DOWN) 
led = pyb.LED(2)

def cbCutDown( line ):
  global pin, led, x1
  x1.disable()
  print('cb')
  pyb.delay(100)
  if( pin.value() ):

    led.toggle()

  x1.enable()

x1 = pyb.ExtInt( pin, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_DOWN, cbCutDown )

cosntant polling of pin state is not the solution I want, couse I have to do some other stuff too.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Bouncing inputs

Post by dhylands » Thu Feb 05, 2015 6:26 pm

Push buttons normally require several milliseconds (10-20) of debounce.

One common technique is to disable interrupts and set a timer to re-enable interrupts. Depending on your application you can test when the initial interrupt occurs (if you're not concerned with noise) or you can test after the timeout.

This article gives some really good background and approaches for debounce:
http://www.ganssle.com/debouncing.htm

I have a library written in C, but I haven't ported it to python yet:
https://github.com/dhylands/projects/bl ... n/Switch.h
https://github.com/dhylands/projects/bl ... n/Switch.c

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Post by Damien » Fri Feb 06, 2015 10:52 pm

In your callback you could record into a global variable the current time (pyb.millis) and then don't register a push if you get called again within 20ms (using pyb . elapsed_millis).

Post Reply