stockton
Posts: 136
Joined: Mon Aug 24, 2015 6:06 am
Location: South Africa
Contact: Website

Raspberry Zero parking meter sensor & LEDS

Tue Apr 14, 2020 1:59 pm

I have created the following code, mainly from cut and paste for the HC-SR04 code but the bit about the LEDS is mine.
For the life of me I cannot see why I only ever get a green led no matter the distance.

Code: Select all

#Libraries
import RPi.GPIO as GPIO
import time

#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)

#set GPIO Pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24

GPIO_RED = 22
GPIO_AMBER = 27
GPIO_GREEN = 17

#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.setup(GPIO_RED, GPIO.OUT)
GPIO.setup(GPIO_AMBER, GPIO.OUT)
GPIO.setup(GPIO_GREEN, GPIO.OUT)

def KlaarAlmal():
    GPIO.output(GPIO_RED, False)
    GPIO.output(GPIO_AMBER, False)
    GPIO.output(GPIO_RED, False)

def Rooi():
    KlaarAlmal()
    GPIO.output(GPIO_RED, True)

def Geel():
    KlaarAlmal()
    GPIO.output(GPIO_AMBER, True)

def Groen():
    KlaarAlmal()
    GPIO.output(GPIO_GREEN, True)

def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)

    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)

    StartTime = time.time()
    StopTime = time.time()

    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()

    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()

    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2

    return distance

if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
        if dist < 10:
            Rooi()
        elif dist > 10 and dist < 50:
            Geel()
        elif dist > 50 and dist < 75:
            Groen()
        elif dist > 75:
            KlaarAlmal()

        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()

I have also written a different bit of code, that uses "from gpiozero import LED" rather than the "import RPi.GPIO as GPIO" used above, that shows that my wiring and soldering is OK as I can get each of the LEDs to switch on and off.
I do not understand the difference between gpiozero and RPi.GPIO. Where can I go to read up on that for I suspect that that is the issue.
Can you please tell me how I got this wrong ?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Raspberry Zero parking meter sensor & LEDS

Tue Apr 14, 2020 2:20 pm

Looks like you have an indentation problem.

This is your code

Code: Select all

if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
        if dist < 10:
            Rooi()
        elif dist > 10 and dist < 50:
            Geel()
        elif dist > 50 and dist < 75:
            Groen()
        elif dist > 75:
            KlaarAlmal()

        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
This is what it should be

Code: Select all

if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
            if dist < 10:
                Rooi()
            elif dist > 10 and dist < 50:
                Geel()
            elif dist > 50 and dist < 75:
                Groen()
            elif dist > 75:
                KlaarAlmal()

        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
Your code was just reading the distance over and over again withouth actually getting to the if-elif statements to set the correct LEDs.

Also in your "KlaarAmal" function, you are setting the RED LED off twice rather than setting the green LED off

Code should be

Code: Select all

def KlaarAlmal():
    GPIO.output(GPIO_RED, False)
    GPIO.output(GPIO_AMBER, False)
    GPIO.output(GPIO_GREEN, False)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

stockton
Posts: 136
Joined: Mon Aug 24, 2015 6:06 am
Location: South Africa
Contact: Website

Re: Raspberry Zero parking meter sensor & LEDS

Tue Apr 14, 2020 2:36 pm

Thank you it now works. The two reds was silly but I do need to learn about indentation.

Return to “Python”