Nov-09-2018, 05:58 AM
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.
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()