mikeandlauren
Posts: 6
Joined: Wed Jun 08, 2016 11:10 pm

Make an LED flash once.

Sun Jul 16, 2017 3:11 pm

Hello,

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'm trying to press the button once and the LED flashes once for 1 second. Then stop. Then if it sees another button press, it flashes again and so on.

I know this is a simple change, but I've spent 4 hours on it with no luck.

Thanks!

pcmanbob
Posts: 2047
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Make an LED flash once.

Sun Jul 16, 2017 3:25 pm

Try This.

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
        GPIO.output(26, True)  # Turn LED on
        sleep(1)
        GPIO.output(26, False) # Turn LED off
        sleep(1)
            
    else:
        GPIO.output(26, False)  # Turn LED off
        sleep(0.1)
        
GPIO.cleanup()
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

mikeandlauren
Posts: 6
Joined: Wed Jun 08, 2016 11:10 pm

Re: Make an LED flash once.

Sun Jul 16, 2017 4:37 pm

That worked! Thank you so much. One more question. How would I then have it flash the LED one more time when I let go of the push button.

To explain my project, I'm trying to get my camera to record when I say "Alexa turn the camera on." That turns on relay number 3 which connects 3.3v to GPIO 23. That then activates this script to flash relay connected to the camera remote (GPIO 26). So I need GPIO 26 to flash 3.3v when it sees a push button on (relay 3) and then when I say "turn the camera off" relay 3 goes off and triggers another 3.3v to GPIO 26 to turn off the camera.

Does that make sense? I had to reverse the True/False in your code because of the way the relay module works.

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, True)  #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
        GPIO.output(26, False)  # Turn LED on
        sleep(1)
        GPIO.output(26, True) # Turn LED off
        sleep(1)
            
    else:
        GPIO.output(26, True)  # Turn LED off
        sleep(0.1)
        
GPIO.cleanup()

mikeandlauren
Posts: 6
Joined: Wed Jun 08, 2016 11:10 pm

Re: Make an LED flash once.

Sun Jul 16, 2017 5:04 pm

Nevermind, I think I got it.

I changed GPIO.RISING to GPIO.FALLING and now it functions exactly as I need it to. Is this an acceptable way to accomplish it?

EDIT: Now I have one problem left. Whenever I turn anything on or off on the same circuit as the relay, I inadvertently trigger the "camera on" switch. Is there a way to turn down the sensitivity of the gpio.falling command?

Mike

pcmanbob
Posts: 2047
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Make an LED flash once.

Sun Jul 16, 2017 9:35 pm

If it works then its ok.

The problem is that your relay board is active low so switching pi on or off will trigger your relay. It's just a symptom of the type of board you purchased. You could add a transistor circuit to each input on the relay board to make it active high then you would not suffer the problem.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

Return to “Beginners”

Who is online

Users browsing this forum: HawaiianPi and 22 guests