I'm having trouble programming the logic of 2 PIR sensors to print a message in console whenever a user place both hands on the PIR sensors.I have managed to successfully attach the PIR sensors to the raspberry pi using GPIO,GND and 5v port. The code that I currently have does print out a message in console whenever someone waves there hand across one but i'm having difficulty modifying the code to print an error message out when someone waves their hand on both the PIR sensors.
We can read input from the sensor using GP4 and GP17
import RPi.GPIO as GPIO
sensor = 4
sensor2 = 17
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
previous_state = False
current_state = False
previous_state = current_state
current_state = GPIO.input(sensor)
if current_state(TRUE) != previous_state(FALSE):
new_state = "HIGH" if current_state else "LOW"
print("GPIO pin %s is %s" % (sensor, new_state))
I haven't checked the code for syntax errors and don't have access to a Pi at the moment, but it's an example of what I was referring to in this comment:
"Use RPi.GPIO's add_event_detect interrupt driven callbacks instead. Then you can eliminate the infinite loop entirely and let the script do other stuff while still reacting to the sensors. You can use the time difference between rising edges to define what "simultaneously" means to you (a max difference of 1 second between sensor 1 and sensor 2 being triggered, for instance)"
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN, GPIO.PUD_DOWN) GPIO.setup(4, GPIO.IN, GPIO.PUD_DOWN) # change GPIO.RISING to GPIO.FALIING if your PIRs are active low GPIO.add_event_detect(17, GPIO.RISING, callback=callback_func) GPIO.add_event_detect(4, GPIO.RISING, callback=callback_func) pir1_last = pir2_last = time.time() def callback_func(pin): global pir1_last global pir2_last if GPIO.input(17): pir1_last = time.time() if GPIO.input(4): pir2_last = time.time() t_diff = abs(pir1_last - pir2_last) if t_diff < 0.5: print "it's been less than 0.5 seconds since both PIRs were activated" def main(): while True: print "Not blocking! You're free to do other stuff here" time.sleep(10)