Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RPi GPIO Flashing LED
#1
The code works a treat in that button pressed LED ON button pressed again LED OFF. What I am trying to do is get the LED to flash when it is ON rather than permanently lit. I think I need to put in a while True loop but unsure where. Placing it in the def Loop() does make it flash but it flashes during the on and off state. Whats confusing me is the def swLed section as this is the place where the LED is turned on and off but if I put the loop in there nothing happens. Any help most appreciated - still trying to get my head around Python! Thanks

import RPi.GPIO as GPIO
import time

LedPin = 11                                                     # pin11 --- led
BtnPin = 12                                                     # pin12 --- button

Led_status = 1

def setup():
        GPIO.setmode(GPIO.BOARD)                                # Numbers GPIOs by physical location
        GPIO.setup(LedPin, GPIO.OUT)                            # Set LedPin's mode is output
        GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)   # Set BtnPin's mode is input, and pull up to high level(3.3V)
        GPIO.output(LedPin, GPIO.HIGH)                          # Set LedPin high(+3.3V) to off led

def swLed(ev=None):
        global Led_status
        Led_status = not Led_status
        GPIO.output(LedPin, Led_status)                         # switch led status(on-->off; off-->on)

def loop():
        GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=swLed, bouncetime=200) # wait for falling, set bouncetime to prevent callback function 
                                                                                    # from being called multiple times when the button is pressed
        while True:
                time.sleep(1)                                   # Don't do anything

if __name__ == '__main__':                                      # Program start from here
        setup()
        try:
                loop()
        except KeyboardInterrupt:                               # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
                destroy()
Reply
#2
I think you'll have to start a Thread in swLed() that could trigger a while loop to blink the LED (if button is pressed [ON]).
If the press button is OFF you'll have to set a "global stop variable" (keep_blinking = False) to break the blink LED loop.

def my_thread_function():
    led_state = 1
    while keep_blinking:
        GPIO.output(LedPin, led_state)
        led_state = not led_state
        time.sleep(0.1)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function return boolean based on GPIO pin reading caslor 2 1,130 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 744 Jan-30-2023, 08:05 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 953 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 3,856 Feb-19-2021, 06:29 AM
Last Post: LouF
  Picture changing triggered by GPIO q_nerk 2 2,518 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 2,280 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  raspberry pi tank gpio help jatgm1 1 2,369 May-06-2020, 09:00 PM
Last Post: Larz60+
  Where should I place GPIO.cleanup() shallanq 2 2,110 Apr-11-2020, 05:02 AM
Last Post: shallanq
  Orange PI Win steering LEDs connect to GPIO djmcg 0 1,503 Dec-27-2019, 08:26 AM
Last Post: djmcg
  GPIO time in HIGH LOW boris_za 1 2,228 Dec-07-2019, 01:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020