I am developing a robot with raspberry pi. To detect obstacles I am using
FC-51 IR sensor. This is my current code.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) ObstaclePin = 18 tServoPin = 15 GPIO.setup(tServoPin, GPIO.OUT) GPIO.setup(ObstaclePin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(tServoPin, GPIO.OUT) tPwm = GPIO.PWM(tServoPin, 50) tPwm.start(0) def loop(): while True: if (1 == GPIO.input(ObstaclePin)): print ("Barrier is detected !") turn_right(tPwm) else: print ("No barrier found !") def turn_right(tPwm): for i in range(90, 180): positiont = 1./18.*(i)+2 tPwm.ChangeDutyCycle(positiont) time.sleep(0.02) for i in range(180, 90, -1): positiont = 1./18.*(i)+2 tPwm.ChangeDutyCycle(positiont) time.sleep(0.05) def destroy(): GPIO.cleanup() if __name__ == '__main__': try: loop() except KeyboardInterrupt: destroy()
This above code works fine when I comment
turn_right line in
loop() function. That is it print "Barrier is detected !" whenever I close a object to it and when I take object away from it the obstacle indicator turn off. But when I uncomment that code line IR sensor's obstacle indicator keeps blinking after obstacle detection. But there is no object close to it. While blinking it is also not fully illuminated. And servo does not stop. I think IR sensor keep blinking it's indicator as a reason for this
I have connected IR sensor to pin 16 and servo to pin 15 in Raspberry pi. and both VCC and GND connect to a bread board. And an Arduino supply 5v for the bread board.
I can not understand the logic behind this. What am i doing wrong here??
Any help would be great Thank you very much in advance!!!