As promised, and for anyone who is interested here is the final working code for this project. I may (as suggested by Stephen earlier) try to implement something which switches the red LED off after a while, but all in all I’m pretty happy with how this all came out. I’ve also posted a link to an updated photo gallery of the finished article.
Script:
Parking-Sensor2.py
Measure distance using an ultrasonic module
in a loop.
#------------------------
Import required Python libraries
-----------------------
from future import print_function
import time
import RPi.GPIO as GPIO
-----------------------
Define some functions
-----------------------
def measure():
This function measures a distance
new_reading=False
counter=0
counter2=0
start = time.time()
stop = time.time()
GPIO.output(GPIO_TRIGGER, True)
Wait 10us
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
while GPIO.input(GPIO_ECHO)==0:
pass
counter += 1
if counter==5000:
new_reading=True
break
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
pass
counter2 +=1
if counter2==5000:
new_reading=True
break
stop = time.time()
if new_reading:
return False
elapsed = stop-start
distance = (elapsed * speedSound)/2
return distance
def measure_average():
This function takes 3 measurements and
returns the average.
distance1=measure()
time.sleep(0.1)
distance2=measure()
time.sleep(0.1)
distance3=measure()
distance = distance1 + distance2 + distance3
distance = distance / 3
return distance
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)
-----------------------
Main Script
-----------------------
Use BCM GPIO references
instead of physical pin numbers
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
Define GPIO to use on Pi
GPIO_TRIGGER = 4
GPIO_ECHO = 18
GREEN = 17
YELLOW = 27
RED = 22
Speed of sound in cm/s at temperature
temperature = 25
speedSound = 33100 + (0.6*temperature)
print(“Ultrasonic Measurement”)
print(“Speed of sound is”,speedSound/100,"m/s at ",temperature,“deg”)
Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
GPIO.setup(GREEN,GPIO.OUT) # Green LED
GPIO.setup(YELLOW,GPIO.OUT) # Yellow LED
GPIO.setup(RED,GPIO.OUT) # Red LED
Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
Allow module to settle
time.sleep(3)
Wrap main content in a try block so we can
catch the user pressing CTRL-C and run the
GPIO cleanup function. This will also prevent
the user seeing lots of unnecessary error
messages.
try:
while True:
distance = measure_average()
print("Distance : {0:5.1f}".format(distance))
time.sleep(0.1)
if distance >= 90:
no_light()
elif 88 > distance > 40:
green_light()
elif 38 > distance > 16:
yellow_light()
elif distance <=14:
red_light()
except KeyboardInterrupt:
User pressed CTRL-C
Reset GPIO settings
GPIO.cleanup()
Google Photos Link - https://photos.app.goo.gl/HFCBzBYbQXewVT4F7
Again, a big thanks to Stephen for putting up with all my silly questions and for all the help along the way.