-1

I am using a Pi 4 to build a reverse park sensor based on these tutorials:

https://forum.core-electronics.com.au/t/raspi-parking-sensor/3736/6

https://www.instructables.com/id/Raspberry-Pi-Park-Sensor/

So I have set up my wiring and connections all correctly. When I reformat my code and make sure it is correct and formatted with the error: Syntax Error: ‘return’ outside function. Can someone please tell me how to fix this issue. I am new to coding and Raspberry Pi's and I would like some assistance please. I have tried indenting etc. but still hitting a brick wall.

The "return distance" part is the issue that keeps popping up when i try to run the code.

The code is:

    import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
TRIG = 4
ECHO = 18
GREEN = 17
YELLOW = 27
RED = 22
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(GREEN,GPIO.OUT)
GPIO.setup(YELLOW,GPIO.OUT)
GPIO.setup(RED,GPIO.OUT)
start = 1
def no_light ():
    GPIO.output(GREEN, GPIO.LOW)
    GPIO.output(YELLOW, GPIO.LOW)
    GPIO.output(RED, GPIO.LOW)

def green_light():
    GPIO.output(GREEN, GPIO.HIGH)
    GPIO.output(YELLOW, GPIO.LOW)
    GPIO.output(RED, GPIO.LOW)

def yellow_light():
    GPIO.output(GREEN, GPIO.LOW)
    GPIO.output(YELLOW, GPIO.HIGH)
    GPIO.output(RED, GPIO.LOW)

def red_light():
    GPIO.output(GREEN, GPIO.LOW)
    GPIO.output(YELLOW, GPIO.LOW)
    GPIO.output(RED, GPIO.HIGH)

def get_distance():
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)
    global start


while GPIO.input(ECHO) == False: start = time.time()
while GPIO.input(ECHO) == True: end = time.time()
signal_time = end-start
distance = signal_time / 0.000058
return distance

while True:
    distance = get_distance()
    time.sleep(0.05)
    print("distance")

if distance >= 60:
    no_light()

elif 90 > distance > 30:
    green_light()

elif 28 > distance > 16:
    yellow_light()

elif distance <=15:
    red_light()

while False:
    print ("error")


GPIO.cleanup()
  • Please add the line number for the error. Also, this question should probably be on the Code Review Stack Exchange site. – SkyPlayX Mar 20 at 9:52
  • 1
    And what is the output after you indent the code below the get_distance function? – SkyPlayX Mar 20 at 9:54
  • The code is: while GPIO.input(ECHO) == False: start = time.time() while GPIO.input(ECHO) == True: end = time.time() signal_time = end-start distance = signal_time / 0.000058 return distance The "return distance" part is the issue that keeps popping up when i try to run the code. – BND Mar 20 at 10:12
  • 1
    This is a general Python question. Might be best to work through some Python tutorials. – joan Mar 20 at 10:46
  • 1
    Google for Python tutorial and then work through the exercises. – joan Mar 20 at 11:27
2

In your code, the return command is placed outside the get_distance function. The return command can only be used when it is in a function, so it returns the output of the function you specify. When you use return distance, you should keep it in the function or it won’t make any sense to Python. Right now, it ends with global start.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.