Jesse
Jesse

Reputation: 185

Execute if statement only once in a period of time in Python?

Creating a python script which receives data from an arduino distance sensor. I am receiving a value each nanosecond. Whenever this value is higher than 50, I want to warn the user. (will eventually do this with a notification program, but for now I am just printing warning). I have the following:

while 1:                                                   # Keep receiving data
    data = ser.readline()                                  # Getting data from arduino
    value = [int(s) for s in data.split() if s.isdigit()]  # Converting the Arduino ouput to only get  the integer value
    if value:                                              # Avoid empty values
        distance = value[0]                                # List to int
            if distance > 50:                              # If bigger than 50 cm, warn user.
                warn_user()                 

I only want to execute the warn_user() function once in 30 seconds, after that, the if statement shouldn't trigger anymore, only when the values drop under 50 and THEN > 50 again. I tried working with True/False statements, timer sleeps but this did not work. Any tips? thanks.

Upvotes: 0

Views: 641

Answers (2)

Ryan Stein
Ryan Stein

Reputation: 8000

You need only track what you're looking for to accomplish your goal: the timestamp of the last warning and whether the distance was below the value you're tracking.

import time

distance_was_safe = True  # tracks if distance went below specified range
last_warned = 0           # tracks timestamp since last warning

safe_distance = 50  # 50 cm
warn_interval = 30  # 30 sec, warn no more than once every interval


while True:
    # Get data from Arduino.
    data = ser.readline()                                  

    # Convert the Arduino output to only get the integer values.
    value = [int(s) for s in data.split() if s.isdigit()]

    # Avoid empty output.
    if value:                                              
        # Get the first integer.
        distance = value[0]

            # If greater than safe zone, potentially warn the user.
            if distance > safe_distance:

                # Warn the user if distance was below range,
                # and it has been enough time since the last warning.
                if distance_was_safe and time.time() - last_warned > warn_interval:
                    distance_was_safe = False
                    last_warned = time.time()
                    warn_user()

            else:
                # Distance was less than warning distance, reset the flag.
                distance_was_safe = True

Upvotes: 2

Daniele Grattarola
Daniele Grattarola

Reputation: 1547

You just have to add some more logical conditions to control the program's flow. Something like this would work:

from time import time
warning_enabled = True
time_of_warning = 0

while 1:
    data = ser.readline()
    value = [int(s) for s in data.split() if s.isdigit()]
    if value:
        distance = value[0]
            if distance > 50 and warning_enabled:
                warn_user()
                warning_enabled = False
                time_of_warning = time()
            if time() - time_of_warning > 30 and not warning_enabled and distance < 50:
                warning_enabled = True

What this does is that it keeps track of the last time when the warning was fired and uses the warning_enable flag to make the second if only fire when possible.

Cheers

Upvotes: 2

Related Questions