Question: so below is my raspberry pi 3 code (Python). I am having trouble stopping my DF15RSMG Servo when there is nothing in front of the

so below is my raspberry pi 3 code (Python). I am having trouble stopping my DF15RSMG Servo when there is nothing in front of the ultrasensor (Does not stop running clockwise). Initially , when there is movement in front of the sensor , the servo rotates clockwise/counterclockwise . Therefore , how would i be able to stop my servo from running when there is no object/movement in front of my ultrasensor? --------------------------------------------------------------------------------- import RPi.GPIO as GPIO import time

GPIO.setmode(GPIO.BOARD) GPIO_TRIGGER = 12 GPIO_ECHO = 18

GPIO.setup(GPIO_TRIGGER, GPIO.OUT) GPIO.setup(GPIO_ECHO, GPIO.IN) GPIO.setup(32, GPIO.OUT)

p = GPIO.PWM(32,50) def distance( ) : GPIO.output (GPIO_TRIGGER, True) time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) StartTime = time.time( ) StopTime = time.time( ) while GPIO.input (GPIO_ECHO) == 0 : StartTime = time.time( ) while GPIO.input (GPIO_ECHO) == 1 : StopTime = time.time( ) TimeElapsed = StopTime - StartTime distance = (TimeElapsed * 34300) / 2 return distance try: while True: p.start(7.5) dist=distance( ) print ("Distance = %. If cm" % dist) time.sleep(1) if dist < 5 : p.ChangeDutyCycle(7.5) time.sleep(1) p.ChangeDutyCycle(2.5) time.sleep(1) p.ChangeDutyCycle(7.5) time.sleep(1) elif dist > = 5 : except KeyboardInterrupt : p.stop( ) GPIO.cleanup( )

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!