Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6197

Troubleshooting • button stops listening further input after single press

$
0
0
Hi guys, this is my first post here and I just started coding in the PI very recently. I have the Rasp Pi 3 A+ and I need to write a program which takes a button input and does 2 things. If the led is off, the led will turn on after 2 seconds, and if it is on, blink 2 times. However, after pressing the button the first time, it no longer continues to listen for more button presses and the button_pressed() function doesn't ever seem to run a second time. I've been stuck on this for a while yet it seems like such an easy fix. Any help is appreciated.

Code:

from gpiozero import LEDfrom gpiozero import Buttonimport timefrom signal import pauseled = LED(17)button = Button(18)def button_pressed():if led.is_lit:print("off")led.off()time.sleep(1)led.on()time.sleep(1)led.off()time.sleep(1)led.on()time.sleep(1)led.off()else:print("on")time.sleep(2)led.on()returnbutton.when_pressed = button_pressedpause()

Statistics: Posted by andrewpicode — Wed Aug 07, 2024 1:21 pm



Viewing all articles
Browse latest Browse all 6197

Trending Articles