Python Forum
If conditions with time limit
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If conditions with time limit
#1
Hello All,

I have a question, this seems like it should be very straight forward but I have not been able to get this right yet.

I have a raspberry pi connected to two buttons and one LED. My code below should activate the LED only if both buttons are pushed within 1 second of each other. I am certain that I need to use a while loop here but have not been able to figure it out yet.

Any advice would be appreciated. Thanks in advance.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Button to GPIO23
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Button to GPIO25
GPIO.setup(24, GPIO.OUT)  # LED to GPIO24

while True: #assuming I need to use time library here?
    button_state1 = GPIO.input(23)
    button_state2 = GPIO.input(25)
    if (button_state1 == False and button_state2 == False): #this IF statment should only work if buttons pressed within 1 second of each other.
        GPIO.output(24, True)
        print('Buttons Pressed within 1 second...')
        time.sleep(0.2)
    else:
        GPIO.output(24, False)

GPIO.cleanup()
Reply
#2
add a latch to your GPIO. This can be done simply, see: http://www.mosaic-industries.com/embedde...controller (You can modify to make a bit simpler)
Relying on time is not reliable!
Reply
#3
(Nov-09-2018, 11:08 AM)Larz60+ Wrote: add a latch to your GPIO. This can be done simply, see: http://www.mosaic-industries.com/embedde...controller (You can modify to make a bit simpler)
Relying on time is not reliable!

Thanks Larz60, had a read through that, it is interesting but not sure that it helps me in this case. I need to do this through software an not hardware as I am using a variant of this code with MQTT.

This needs to be a code solution to say that only if these two buttons are pressed within 1 second of each other, the LED will light up.
Reply
#4
You can use datetime something like this
t1 = datetime.datetime.now()
time.sleep(0.9)
t2 = datetime.datetime.now()
delta = t2 - t1
print(delta.total_seconds())
print(delta.total_seconds() < 1)
Reply
#5
I think the way I'd do it, is to keep track of the last time each was pressed, then comparing the times. Something like:
last_press_left = last_press_right = 0
LEFT_PIN = 23
RIGHT_PIN = 25

while True:
    if GPIO.input(LEFT_PIN):
        last_press_left = time.time()
    if GPIO.input(RIGHT_PIN):
        last_press_right = time.time()
    if last_press_left and last_press_right and 1.0 >= abs(last_press_left - last_press_right):
        GPIO.output(24, False)
    time.sleep(0.01)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assign a value if datetime is in between a particular time limit klllmmm 2 2,739 Jan-02-2021, 07:00 AM
Last Post: klllmmm
  Time Limit Exceeded error loves 5 3,134 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
  fibonacci ***Time limit exceeded*** frequency 18 10,170 Nov-29-2018, 09:03 PM
Last Post: frequency
  'Time Limit Exceeded' Problem bkpee3 2 5,411 Nov-14-2018, 03:51 AM
Last Post: bkpee3

Forum Jump:

User Panel Messages

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