Skip to content

Instantly share code, notes, and snippets.

@codelectron
Last active May 4, 2020 11:51
Show Gist options
  • Save codelectron/ca794fe1fef6c71b08b27008e5501776 to your computer and use it in GitHub Desktop.
Save codelectron/ca794fe1fef6c71b08b27008e5501776 to your computer and use it in GitHub Desktop.
from RPi import GPIO
from time import sleep
clk = 17
dt = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_UP)
clkLastState = GPIO.input(clk)
def my_callback(channel):
global clkLastState
global counter
try:
clkState = GPIO.input(clk)
if clkState != clkLastState:
dtState = GPIO.input(dt)
if dtState != clkState:
counter += 1
else:
counter -= 1
print counter
clkLastState = clkState
#sleep(0.01)
finally:
print "Ending"
counter = 0
clkLastState = GPIO.input(clk)
GPIO.add_event_detect(17, GPIO.FALLING , callback=my_callback, bouncetime=300)
raw_input("Enter anything")
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment