DenDanne
Posts: 2
Joined: Sat Sep 18, 2021 5:21 pm

Make led stop blinking when releasing button

Sat Sep 18, 2021 5:53 pm

I have a GPIO led button project.

What I want to do is to have the led blink if I hold the button, but when I release it, it shall stop blinking.
What I have done is to make it blink when I hold the button, but then when I release it, it never stops blinking.

"import time
from gpiozero import Button, LED

button = Button(14)
led1 = LED(23)

def button_held():
a = True
while(a):
led1.on()
time.sleep(0.5)
led1.off()
time.sleep(0.5)

led1.when_held = button_held
"
The approach I've tried is to somehow make that if button.when_released is activated, then a = False which makes the while loop stop. But I dont really know how to do that. Or maybe there is a better way? Thanks!

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

Re: Make led stop blinking when releasing button

Sun Sep 19, 2021 2:01 pm

Hi.

The problem with your code is that you have a while true loop in your function so once you enter the loop then code will just run the loop for ever no matter what button your press.

try changing you code so there is no while true loop the LED will still flash but one the button is released the LED will stay off

example

Code: Select all

import time
from gpiozero import Button, LED
from signal import pause

button = Button(14)
led1 = LED(23)

def pushed():

    led1.on()
    time.sleep(0.5)
    led1.off()
    time.sleep(0.5)

button.when_pressed = pushed
pause()
code based on example in the gpiozero docs.

and please when posting code especially python use code tags .. viewtopic.php?f=32&t=84477
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

DenDanne
Posts: 2
Joined: Sat Sep 18, 2021 5:21 pm

Re: Make led stop blinking when releasing button

Sun Sep 19, 2021 3:24 pm

But dont I need some kind of loop if the led should blink forever if I hold the button forever? But when I release the button (maybe using the when_released function in GPIOZERO) it should cancel the loop and stop blinking. That is what how Im thinking but I cant find a way to do that without using some kind of loop.

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

Re: Make led stop blinking when releasing button

Sun Sep 19, 2021 6:03 pm

DenDanne wrote:
Sun Sep 19, 2021 3:24 pm
But dont I need some kind of loop if the led should blink forever if I hold the button forever? But when I release the button (maybe using the when_released function in GPIOZERO) it should cancel the loop and stop blinking. That is what how Im thinking but I cant find a way to do that without using some kind of loop.
As gpiozero has an led.blink option you don't need a loop, you just need to call a led.blink to start it blinking and an led.off to stop it like this

Code: Select all

import time
from gpiozero import Button, LED
from signal import pause

button = Button(14)
led1 = LED(23)

def pushed():

    led1.blink()
    
def released():

    led1.off()   

button.when_pressed = pushed
button.when_released = released
pause()
tested working code.

If you intend to use gpiozero then read the documentation .. https://gpiozero.readthedocs.io/en/v1.6 ... umentation
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”