I have this code here. All it does is when I have a button I've wired in pushed down, it prints "Button Pressed" every .3 seconds. I've tried everything, and I can't figure out for the life of me how to make it so this button toggles a variable between True and False, or 0,1 , etc... I'd really appreciate some help. Thanks
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_UP)
while True:
inputValue = GPIO.input(18)
if (inputValue == False):
print("Button press ")
time.sleep(0.3)
Something exactly like this:
You want to know if the state of the button has changed.
You need to keep track of the state and compare it when you get a new value from the GPIO
.
latest_state = None
while True:
inputValue = GPIO.input(18)
if inputValue != latest_state:
latest_state = inputValue
if latest_state:
print("Button pressed")
else:
print("Button depressed")
time.sleep(0.3)