Search code examples
pythoncompiler-errorsraspberry-pisensorsraspberry-pi2

Ultrasonic Sensors with Raspberry pi 2b+ creates compilation error


I have set up 3 HC-SR04 ultrasonic sensors to operate continuously with raspberry pi. Two of the sensors work perfectly while one is being very inconsistent. I have replaced the wires, sensor, and breadboard multiple times, as well as changing the trig and echo pins associated with the sensor. I have debugged the code and have isolated the problem sensor in the code below:

import RPi.GPIO as GPIO
import time

TRIG1 = 13
ECHO1 = 15

##TRIG2 = 22
##ECHO2 = 18
##
##TRIG3 = 37
##ECHO3 = 40

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(TRIG1, GPIO.OUT)
GPIO.output(TRIG1, 0)

##GPIO.setup(TRIG2, GPIO.OUT)
##GPIO.output(TRIG2, 0)
##
##GPIO.setup(TRIG3, GPIO.OUT)
##GPIO.output(TRIG3, 0)

GPIO.setup(ECHO1, GPIO.IN)
##GPIO.setup(ECHO2, GPIO.IN)
##GPIO.setup(ECHO3, GPIO.IN)

while True:
        time.sleep(0.1)

        GPIO.output(TRIG1, 1)
        time.sleep(0.00001)
        GPIO.output(TRIG1, 0)
        print("anything")
        while GPIO.input(ECHO1) == 0:
                print("Works")
                time.sleep(1)
                start1 = time.time()

        while GPIO.input(ECHO1) ==1:
                stop1 = time.time()
        print("sensor 1:")
        print (stop1-start1) * 17000

##        time.sleep(0.1)
##
##        GPIO.output(TRIG2, 1)
##        time.sleep(0.00001)
##        GPIO.output(TRIG2, 0)
##
##        while GPIO.input(ECHO2) == 0:
##                start2 = time.time()
##
##        while GPIO.input(ECHO2) == 1:
##                stop2 = time.time()
##        print("sensor 2:")
##        print (stop2-start2) * 17000
##
##        time.sleep(0.1)
##
##        GPIO.output(TRIG3, 1)
##        time.sleep(0.00001)
##        GPIO.output(TRIG3, 0)
##
##        while GPIO.input(ECHO3) == 0:
##                start3 = time.time()
##
##        while GPIO.input(ECHO3) == 1:
##                stop3 = time.time()
##        print("sensor 3:")
##        print (stop3-start3) * 17000

GPIO.cleanup()

The line that reads "print("anything")" allows the code to compile, but take no distance reading and will continue to print "Works" indicating that it is running continuously in the first while loop. Unexpectedly when the "print("anything")" line is removed, the error:

print (stop1-start1) * 17000
NameError: name 'start1' is not defined

prints to the terminal. The code that is commented out above, which is identical to the code for the functioning sensors works without error when isolated from the code that is currently uncommented. Any thoughts or suggestions would be greatly appreciated.

Thanks.


Solution

  • I think you have a timing issue, that causes your code to run differently depending on "small" timing changes.

    Your code fails due to not having initialized start1.

    When you have a delay in your code (print "anything") is a significant delay, the while loop will run and initialize start1

    When you do not have the delay,

    GPIO.input(ECHO1)
    

    will not equal zero, the while loop will not run and you get the error.

    I think you should initialie your variables and you should check if there are timing requirements you are not aware of.

    Also please note running python on a raspberry pi, will not be a real time system, so please be aware of real time constraints and be careful of making code that is timing sensitive.