Python Forum
get out of while loop and stop repeat
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get out of while loop and stop repeat
#11
I am not there yet still learning how to use Pi. I have no idea how to use state and how it works.
Reply
#12
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 918 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,355 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Repeat request by else stsxbel 2 1,185 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  Avoid multiple repeat in indent Frankduc 8 2,908 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  Stop/continue While loop block Moris526 68 25,635 Mar-28-2021, 09:21 PM
Last Post: Larz60+
  How to discard list repeat values akanowhere 7 3,718 Dec-28-2020, 09:14 PM
Last Post: akanowhere
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,802 Nov-23-2020, 11:01 PM
Last Post: perfringo
  while loop will not stop looping TheTechRobo 5 3,742 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  is there a way: repeat key word args Skaperen 2 2,249 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Python Script to repeat Photoshop action in folders and subfolders silfer 2 4,568 Jul-25-2019, 03:12 PM
Last Post: silfer

Forum Jump:

User Panel Messages

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