Skip to content

Instantly share code, notes, and snippets.

@brianfay
Created November 29, 2013 22:30
Show Gist options
  • Save brianfay/7712807 to your computer and use it in GitHub Desktop.
Save brianfay/7712807 to your computer and use it in GitHub Desktop.
Using a momentary push-button switch as input to a Raspberry Pi - sometimes extra button "presses" would be recorded when releasing it. This filters the button press so that it only registers if has been in it's current state for a couple of polls.
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN, pull_up_down=gpio.PUD_UP)
minNum = 3
oneCount = 0
zeroCount = 0
currentMode = gpio.input(17)
while True:
newMode = gpio.input(17)
if(newMode == 0):
zeroCount += 1
else:
oneCount += 1
if(currentMode != newMode):
if(newMode == 0):
if(zeroCount >= minNum):
currentMode = newMode
zeroCount = 0
oneCount = 0
print("State Changed to UP")
else:
if(oneCount >= minNum):
currentMode = newMode
zeroCount = 0
oneCount = 0
print("State Changed to DOWN")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment