Page 1 of 1

Bouncing inputs

Posted: Thu Feb 05, 2015 2:50 pm
by surmanajaja
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.

Re: Bouncing inputs

Posted: Thu Feb 05, 2015 6:26 pm
by dhylands
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

Posted: Fri Feb 06, 2015 10:52 pm
by Damien
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).