Stop running after few minute
Posted: Sun Apr 03, 2016 4:02 am
Hi,
I built a circuits using 3 PING sensors to measure the distance. With 1 sensor, it works fine. When I use 2 or 3 sensors, they only run for few minutes and stop execute the code. The following codes are what I used. I also want to have the Pi start the code automatically as it is powered up. I appreciate any helps. Thank you.
print "Ultrasonic Measurement"
try:
while True:
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER1,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO1,GPIO.IN) # Echo
GPIO.setup(GPIO_TRIGGER2,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO2,GPIO.IN) # Echo
GPIO.setup(GPIO_TRIGGER3,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO3,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER1, False)
GPIO.output(GPIO_TRIGGER2, False)
GPIO.output(GPIO_TRIGGER3, False)
# Allow module to settle
time.sleep(0.5)
# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER1, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER1, False)
start = time.time()
while GPIO.input(GPIO_ECHO1)==0:
start1 = time.time()
while GPIO.input(GPIO_ECHO1)==1:
stop1 = time.time()
# Calculate pulse length
elapsed1 = stop1-start1
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance1 = elapsed1 * 34000
# That was the distance there and back so halve the value
distance1 = distance1 / 2
print "Distance 1 : %.1f" % distance
#sensor 2
# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER2, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER2, False)
start2 = time.time()
while GPIO.input(GPIO_ECHO2)==0:
start2 = time.time()
while GPIO.input(GPIO_ECHO2)==1:
stop2 = time.time()
# Calculate pulse length
elapsed2 = stop2-start2
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance2 = elapsed12 * 34000
# That was the distance there and back so halve the value
distance2 = distance2 / 2
print "Distance 2 : %.1f" % distance
#sensor 3
# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER3, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER3, False)
start3 = time.time()
while GPIO.input(GPIO_ECHO3)==0:
start3 = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop3 = time.time()
# Calculate pulse length
elapsed3 = stop3-start3
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance3 = elapsed3 * 34000
# That was the distance there and back so halve the value
distance3 = distance3 / 2
print "Distance 3 : %.1f" % distance
except KeyBoardInterrupt:
# Reset GPIO settings
GPIO.cleanup()
I built a circuits using 3 PING sensors to measure the distance. With 1 sensor, it works fine. When I use 2 or 3 sensors, they only run for few minutes and stop execute the code. The following codes are what I used. I also want to have the Pi start the code automatically as it is powered up. I appreciate any helps. Thank you.
print "Ultrasonic Measurement"
try:
while True:
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER1,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO1,GPIO.IN) # Echo
GPIO.setup(GPIO_TRIGGER2,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO2,GPIO.IN) # Echo
GPIO.setup(GPIO_TRIGGER3,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO3,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER1, False)
GPIO.output(GPIO_TRIGGER2, False)
GPIO.output(GPIO_TRIGGER3, False)
# Allow module to settle
time.sleep(0.5)
# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER1, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER1, False)
start = time.time()
while GPIO.input(GPIO_ECHO1)==0:
start1 = time.time()
while GPIO.input(GPIO_ECHO1)==1:
stop1 = time.time()
# Calculate pulse length
elapsed1 = stop1-start1
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance1 = elapsed1 * 34000
# That was the distance there and back so halve the value
distance1 = distance1 / 2
print "Distance 1 : %.1f" % distance
#sensor 2
# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER2, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER2, False)
start2 = time.time()
while GPIO.input(GPIO_ECHO2)==0:
start2 = time.time()
while GPIO.input(GPIO_ECHO2)==1:
stop2 = time.time()
# Calculate pulse length
elapsed2 = stop2-start2
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance2 = elapsed12 * 34000
# That was the distance there and back so halve the value
distance2 = distance2 / 2
print "Distance 2 : %.1f" % distance
#sensor 3
# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER3, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER3, False)
start3 = time.time()
while GPIO.input(GPIO_ECHO3)==0:
start3 = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop3 = time.time()
# Calculate pulse length
elapsed3 = stop3-start3
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance3 = elapsed3 * 34000
# That was the distance there and back so halve the value
distance3 = distance3 / 2
print "Distance 3 : %.1f" % distance
except KeyBoardInterrupt:
# Reset GPIO settings
GPIO.cleanup()