I'm trying to figure out a way to use a button on a Raspberry Pi to toggle between two different conditions in a while
loop. Ideally, by merely pressing the button, I could switch back and forth.
I know this is wrong, but I'm not sure where to go from here.
Roughly, my code looks like this:
from gpiozero import Button
btn=Button(17) #The GPIO pin is 17
def addSurf():
i = i + 1
i = 0
btn.when_pressed = addSurf
while True:
if i % 2 == 0:
#do some stuff
else:
#do some other stuff
Since I started i
at 0 before the while
loop, I figured that by adding integers when the button was pressed, and checking to see if the modulo was zero or not, I could navigate back and forth between the two states.
However, I don't know how to incorporate the .when_pressed
function into the loop so that it's always going to respond to move the program into one state or the other.
Forgive me if I'm a bit of a newbie here, but I tried looking into the documentation for raspberry-gpio-python
for information about event detection and multithreading, but I didn't understand it.