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.

   pi@raspberrypi:~ $ sudo python2 peter.py
     File "peter.py", line 23
       new_state = "HIGH" and new_state2 = "HIGH" if current_state and        current_state2 else "LOW"
   SyntaxError: can't assign to operator

This is the code

import RPi.GPIO as GPIO
import time

sensor = 4
sensor2 = 17
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:
    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(FALSE) != previous_state2(FALSE) and previous_state(FALSE):
        new_state = "HIGH" and new_state2 = "HIGH" if current_state and current_state2 else "LOW"
        print("GPIO pin %s is %s" % (sensor, new_state, sensor2, new_state2))

Your getting the error because you are trying to assign someyhing that isn't a variable. Try changing the next to last line to four lines like this:

new_state = "HIGH" 
new_state2 = "HIGH" 
if not(current_state and current_state2):
    new_state = "LOW"

it is not clear from your code what variable you are trying to assign LOW to, in the last line.

  • 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. – peter pan May 23 '16 at 2:48
  • Hi Steve, I got this error message pastebin.com/raw/KzTq72xu – peter pan May 23 '16 at 2:55
  • change the last line to: print("'{0}' is longer than '{1}'".format(sensor, new_state)) you are also passing four values in to a print statement that only needs two. – Steve Robillard May 23 '16 at 3:07
  • Hi Steve. I don't think the code is working correctly. pastebin.com/TStJuN8v and pastebin.com/d1PVUJfi. Could you please have a look at these too. When I wave my hand, it doesn't work properly – peter pan May 23 '16 at 3:13
  • @peterpan If you have additional questions, they should not be handled by an ongoing chat. Rather they should be separate questions. I don't know where you got this code but there are several more issues with it. – Steve Robillard May 23 '16 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.