I need to control a servo motor with 4 PIR sensors. The servo motor should move to a particular direction (change duty cycle) when each sensor gets activated.
the code is as follows :
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN1 = 12
PIR_PIN2 = 16
PIR_PIN3 = 20
PIR_PIN4 = 21
GPIO.setup(PIR_PIN1, GPIO.IN)
GPIO.setup(PIR_PIN2, GPIO.IN)
GPIO.setup(PIR_PIN3, GPIO.IN)
GPIO.setup(PIR_PIN4, GPIO.IN)
GPIO.setup(18,GPIO.OUT)
p=GPIO.PWM(18,50)
p.start(2.5)
def MOTION1(PIR_PIN1):
p.ChangeDutyCycle(7.5)
print "Motion 1 Detected!"
servo_movement()
def MOTION2(PIR_PIN2):
p.ChangeDutyCycle(2.5)
print "Motion 2 Detected!"
servo_movement()
def MOTION3(PIR_PIN3):
p.ChangeDutyCycle(2.5)
print "Motion 3 Detected!"
servo_movement()
def MOTION4(PIR_PIN4):
p.ChangeDutyCycle(7.5)
print "Motion 4 Detected!"
servo_movement()
print "PIR Module Test (CTRL+C to exit)"
time.sleep(2)
print "Ready"
def servo_movement():
try:
GPIO.add_event_detect(PIR_PIN1, GPIO.RISING, callback=MOTION1)
while 1:
time.sleep(100)
GPIO.add_event_detect(PIR_PIN2, GPIO.RISING, callback=MOTION2)
while 1:
time.sleep(100)
GPIO.add_event_detect(PIR_PIN3, GPIO.RISING, callback=MOTION3)
while 1:
time.sleep(100)
GPIO.add_event_detect(PIR_PIN4, GPIO.RISING, callback=MOTION4)
while 1:
time.sleep(100)
except KeyboardInterrupt:
print "Quit"
p.stop()
GPIO.cleanup()
servo_movement()
Problem with the code is that, when the 1st sensor is activated, it detects and prints and the servo motor changes the duty cycle. But other sensor inputs are not processed and hence the servo motor does not change the duty cycle. Instead an error shows : "Conflicting edge detection already enabled for this GPIO channel".
Someone please help to modify this code so that i can change the duty cycle of servo motor when each of the sensor gets activated.
