im just a noob in robotics.
im doing a research on robotic and ultrasonic sensor. When using a hc-sr04 and connected to raspberry pi with 50ms and lower time.sleep, my data seems not very stable.
I want to use an infinite loop for my sensor..but i dun know how to use it using python..btw this is my copied code from someone`s page
Code: Select all
#!/usr/bin/python import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO_TRIGGER = 23 GPIO_ECHO = 24 print "Ultrasonic Measurement" GPIO.setup(GPIO_TRIGGER,GPIO.OUT) GPIO.setup(GPIO_ECHO,GPIO.IN) try: while True: GPIO.output(GPIO_TRIGGER, False) time.sleep(0.05) GPIO.output(GPIO_TRIGGER, True) time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) start = time.time() while GPIO.input(GPIO_ECHO)==0: start = time.time() while GPIO.input(GPIO_ECHO)==1: stop = time.time() elapsed = stop-start distance = elapsed * 34000 distance = distance / 2 print "Distance : %.1f" % distance except KeyboardInterupt: GPIO.cleanup()