I'm trying to make a relay flash on and off one time when triggered by GPIO 23. I'm a beginner so I followed a tutorial to make an LED flash with a button press, but I can't figure out how to make it stop instead of looping over and over.
Here's the code I have so far.
Code: Select all
#!/usr/bin/env python
import RPi.GPIO as GPIO # Import GPIO Module
from time import sleep # Import sleep Module for timing
GPIO.setmode(GPIO.BCM) # Configures pin numbering to Broadcom reference
GPIO.setwarnings(False) # Disable Warnings
GPIO.setup(26, GPIO.OUT) #Set our GPIO pin to output
GPIO.output(26, False) #Set output to off
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set GPIO to input with a pull-down resistor
GPIO.add_event_detect(23, GPIO.RISING, bouncetime=200) # Monitor GPIO pin for a rising edge and debounce for 200mS
while (True):
if GPIO.event_detected(23): # Check to see if button has been pushed
activate = True
while (activate is True): # Execute this code until the button is pushed again
GPIO.output(26, True) # Turn LED on
sleep(1)
GPIO.output(26, False) # Turn LED off
sleep(1)
if GPIO.event_detected(23): # Check for a 2nd button push
activate = False
else:
GPIO.output(26, False) # Turn LED off
GPIO.cleanup()I know this is a simple change, but I've spent 4 hours on it with no luck.
Thanks!