good morning! I've been working on some python (pretty new to the language) to control some buttons and LEDs utilising gpiozero and I've run into a few issues which I've been trying to troubleshoot and look at other posts around similar problems. I'm no stranger to programming, do most of mine today in JS, but the python syntax and loop structures is giving me some grief and I'm not sure the correct term to look-up to help troubleshoot or find similar questions posted on here... which I looked at a dozen or so to make sure I wasn't wasting anyones time.
The main problem I'm encountering is:
- button press is getting triggered twice (and is erratic)
Erratic meaning when I first press the button, my LEDs turn on/off and then a second press _may_ turn the LEDs off, or it may repeat the on/off sequence again. Other presses, again, may turn the LEDs on (as expected) and the second press _may_ turn the LEDs off, or do the off/on sequence.
I believe the error is in my while loop, but I can't seem to figure out what I need to get the right sequence to work and would love some guidance
Here's the code I've written and a link to a video I made which shows the behaviour I'm seeing. And I do apologise for not supplying a circuit board drawing, I'm also new to this and still trying to figure how I put my current Pi and breadboard into a circuit diagram. https://youtu.be/zipFbDusaXk
from gpiozero import LEDBoard, LED, Button, PWMLED
from time import sleep
from signal import pause
## Define the buttons
static_button = Button(2)
## Model Specific LED Colour Groups
body_leds = LEDBoard(6, 5, 13, 19, 16, 26, 20, 21, pwm=True)
hull_led = LEDBoard(18, pwm=True)
left_red_thruster = LEDBoard(27, pwm=True)
left_green_thruster = LEDBoard(22, pwm=True)
right_red_thruster = LEDBoard(4, pwm=True)
right_green_thruster = LEDBoard(17, pwm=True)
boosters = LEDBoard(24, 25, pwm=True)
## Functions for button actions
def static_display():
print("Turning Serenity on in static mode")
body_leds.toggle()
hull_led.toggle()
left_red_thruster.toggle()
left_green_thruster.toggle()
right_red_thruster.toggle()
right_green_thruster.toggle()
boosters.toggle()
def leds_off():
print("Turning LEDs off")
body_leds.off()
hull_led.off()
left_red_thruster.off()
left_green_thruster.off()
right_red_thruster.off()
right_green_thruster.off()
boosters.off()
while True:
if static_button.is_pressed:
if body_leds.is_active:
leds_off()
static_button.wait_for_press(timeout=None)
else:
static_display()
static_button.wait_for_press(timeout=None)
pause()
wait_for_press(timeout=None)
in the correct spot and my lights work as expected.