I have the following script which increases the counter
variable every time a button is pressed. When counter
reaches a certain number i.e. 10 lets say I want an event to trigger.
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
button1=22
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
counter = 0
while(1):
if GPIO.input(button1)==0:
counter = counter + 1
sleep (0.5)
print counter
if counter == 10:
print("target reached")
GPIO.cleanup()
I've been informed that a simple way to do this would be check the counter each time it changes and then use an if
statement to trigger the event. Unfortunately I have a very elementary understanding of programming and have little idea how to do this.
Having researched if
statements I came across this one that looked as if it might work -
if counter == 10:
print("target reached")
It runs without error but doesn't do anything when expected.
My question therefore is how to proceed from here?
Thanks
sleep
line as the counting was proving a little unpredictable as it was. Seems to be working OK now.