hi
i have been trying to make a motion sensor turn on a buzzer when motion was sensed but for some reason i get this error
Motion Detected!
Traceback (most recent call last):
File "motion.py", line 21, in MOTION
if (pirState == False):
UnboundLocalError: local variable 'pirState' referenced before assignmen
this is my code:
import RPi.GPIO as GPIO
import time
pirVal = False
pirState = False
GPIO.setmode(GPIO.BOARD)
PIR_PIN = 26
GPIO.setup(PIR_PIN, GPIO.IN)
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, False)
def MOTION(PIR_PIN):
print "Motion Detected!"
while True:
pirVal = GPIO.input(PIR_PIN)
if (pirVal == True):
GPIO.output(11, True)
time.sleep(1)
pirVal = False
if (pirState == False):
pirState = True
if (pirState == True):
time.sleep(2)
pirState = False;
print "PIR Motion Test (CTRL+C to exit)"
time.sleep(2)
print "Ready"
try:
GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
while 1:
time.sleep(100)
except KeyboardInterrupt:
print " Cancel"
GPIO.cleanup()
could someone please help as it is the holidays now and i am 11 i have looked throuh the web but i dont understand the answers
the indents are in there but they went when i copied it
thanks in advance
olly