I have the following script which increases the counter every time a button is pressed. When the counter reaches a certain number i.e. 10 lets say I want an event to trigger.
from RPi import GPIO
from time import sleep
clk = 25
dt = 8
GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
counter = 0
clkLastState = GPIO.input(clk)
try:
while True:
clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
if clkState != clkLastState:
if dtState != clkState:
counter += 1
else:
counter -= 1
print counter
clkLastState = clkState
sleep(0.01)
finally:
GPIO.cleanup()
For the purposes of an example script it might be easiest just to get it to print something on reaching the required number i.e. 'target reached'.
This question relates to an earlier post of mine here - rotary encoder script for raspberry pi using python. Rather than add to or amend that question I thought it better to break down the problem for the purposes of understanding the various components.
Many thanks