1
votes

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.

enter image description here

We can read input from the sensor using GP4 and GP17

This is the error message I receive when I run my code.

 Traceback (most recent call last):
  File "peter.py", line 22, in <module>
    if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):
NameError: name 'TRUE' is not defined

This is the code

 import RPi.GPIO as GPIO
  import time

  sensor = 4
  sensor2 = 17
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
  GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)

  previous_state = False
  current_state = False

  previous_state2 = False
  current_state2 = False

  while True:
      time.sleep(0.1)
      previous_state = current_state
      previous_state2 = current_state2
      current_state = GPIO.input(sensor)
      current_state2 = GPIO.input(sensor2)
      if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):

          new_state = "HIGH" if current_state else "LOW"
          new_state2 = "HIGH" if current_state2 else "LOW"

          print("GPIO pin %s is %s" % (sensor, new_state, sensor2, new_state2))

The program is pretty simple. The Raspberry Pi GPIO pins to allow us to use pin 4 as an input; it can then detect when the PIR module sends power. The pin continually check for any changes, uses a while True loop for this. This is an infinite loop so the program will run continuously unless we stop it manually with Ctrl + C. Then use two Boolean variables (True or False) for the previous and current states of the pin, the previous state being what the current state was the preceding time around the loop

1

1 Answers

1
votes

In Python use "True", not "TRUE":

if True:
        print("True")
else:
        print("False")

Also change:

if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):

To:

if current_state2==True and current_state != previous_state2 and previous_state==False: