Apr-26-2022, 05:11 PM
I am not there yet still learning how to use Pi. I have no idea how to use state and how it works.
get out of while loop and stop repeat
|
Apr-26-2022, 05:11 PM
I am not there yet still learning how to use Pi. I have no idea how to use state and how it works.
Apr-26-2022, 10:09 PM
(This post was last modified: Apr-26-2022, 10:09 PM by deanhystad.)
The concept of state has nothing to do with Python. It is a way of solving problems where your program has to make decisions based on things that happened in the past.
You are already using state. led_off1 is a state variable. It remembers if led1 is on or off. BTW, you need to work on your variable naming. bouton_alert_Del1 is a great name. led_off1 is not a good name. Instead use a name that indicates what led1 being on or off means like active_led_on or alarm_active. You may decide to remap your LED's in the future. You shouldn't have to rename all your variables if this happens. Your program has to remember if I sent an email for the current alert button press. If the button is not pressed, set the state to "Not Sent". If the button is pressed, check the state. If "Not Sent", send an email and set the state to "Sent". Now you only send one email for each alert button press. What is a good name for your alert email sent state variable? You should also be using state for blinking the alert LED. This and a timer eliminates the need to sleep() and will make your program more responsive. active = False alert = False alert_led_on = False alert_time = None while(True): new_state = GPIO.input(bouton_alert_Del1) == 1 if new_state != active: active = new_state if active: # do stuff that you only do once while active else: # do stuff that you only do once while not active new_state = active and GPIO.input(bouton_alert_Del1) == 1 if new_state != alert: alert = new_state if alert: # do stuff that you only once while alert alert_led_on = True alert_time = time.time() + 0.2 else: # do stuff that you only once while not alert alert_led_on = False GPIO.output(ALERT_LED, alert_led_on) # Do repeated stuff here. This blinks the alert LED if alert and time.time() >= alert_time: alert_led_on = not alert_led_on alert_time += 0.2 GPIO.output(ALERT_LED, alert_led_on) |
|