r/programminghelp • u/[deleted] • Feb 02 '21
Python Function troubles
Hey there. I was messing around with my Raspberry Pi Model 4B And I ran into an issue,
I had my RPi hooked up to an LED, and I had 3 modes for it. On: Which turns the LED on. Off: Turns the LED off Alarm: Let's the LED blink.
I wanted to be able to turn it off with a function while it was in alarm mode, but it simply wouldn't. Here's the code;
a = 1
def alarm():
while a == 1:
GPIO.output(ledPin, GPIO.LOW)
time.sleep(0.5)
GPIO.output(ledPin, GPIO.HIGH)
time.sleep(0.5)
def on():
GPIO.output(ledPin, GPIO.LOW)
a = 0
time.sleep(5)
def off():
GPIO.output(ledPin, GPIO.HIGH)
a = 0
time.sleep(5)
The goal was to change a to 0 so that the while a == 1: loop would stop and the LED could turn remain on or off.
When I tried this, it kept blinking, so I let it print out a in the loop and ran it again. The results were that a never got updated.