#import the libraries used
import time
import pigpio
import RPi.GPIO as GPIO
#create an instance of the pigpio library
pi = pigpio.pi()
#define the pin used by the Buzzer
#this pin will be used by the pigpio library
#which takes the pins in GPIO forms
#we will use GPIO18, which is pin 12
buzzer = 18
#set the pin used by the buzzer as OUTPUT
pi.set_mode(buzzer, pigpio.OUTPUT)
GPIO.setmode(GPIO.BOARD)
#define the pins used by the ultrasonic module
trig = 16
echo = 13
redled = 36
greenled = 38
blueled = 40
#set the trigger pin as OUTPUT and the echo as INPUT
GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)
#set the pins for the led as OUTPUT
GPIO.setup(redled, GPIO.OUT)
GPIO.setup(greenled, GPIO.OUT)
GPIO.setup(blueled, GPIO.OUT)
def calculate_distance():
#set the trigger to HIGH
GPIO.output(trig, GPIO.HIGH)
#sleep 0.00001 s and the set the trigger to LOW
time.sleep(0.00001)
GPIO.output(trig, GPIO.LOW)
#save the start and stop times
start = time.time()
stop = time.time()
#modify the start time to be the last time until
#the echo becomes HIGH
while GPIO.input(echo) == 0:
start = time.time()
#modify the stop time to be the last time until
#the echo becomes LOW
while GPIO.input(echo) == 1:
stop = time.time()
#get the duration of the echo pin as HIGH
duration = stop - start
#calculate the distance
distance = 34300/2 * duration
if distance < 0.5 and distance > 400:
return 0
else:
#return the distance
return distance
try:
while True:
if calculate_distance() < 10:
#turn on the buzzer at a frequency of
#500Hz for 50 ms
pi.hardware_PWM(buzzer, 500, 200000)
time.sleep(0.02)
#turn off the buzzer and wait 50 ms
#pi.hardware_PWM(buzzer, 0, 0)
#time.sleep(0.05)
#the next 4 instructions are used
#to create the flashing effect
#turn on the red Led and wait 35 ms
GPIO.output(redled, GPIO.HIGH)
time.sleep(0.035)
#turn off the red Led and wait 35 ms
GPIO.output(redled, GPIO.LOW)
time.sleep(0.025)
#turn off the buzzer and wait 50 ms
pi.hardware_PWM(buzzer, 0, 0)
time.sleep(0.05)
elif calculate_distance() > 10 and calculate_distance() < 25:
#turn on the green Led and wait 300 ms
GPIO.output(greenled, GPIO.HIGH)
time.sleep(0.3)
#turn off the green Led and wait 200 ms
GPIO.output(greenled, GPIO.LOW)
time.sleep(0.2)
elif calculate_distance() > 25:
#turn on the blue Led and wait 300 ms
GPIO.output(blueled, GPIO.HIGH)
time.sleep(0.5)
#turn off the blue Led and wait 200 ms
GPIO.output(blueled, GPIO.LOW)
time.sleep(0.2)
else:
#turn off the buzzer
pi.hardware_PWM(buzzer, 0, 0)
#wait 100 ms before the next run
time.sleep(0.1)
except KeyboardInterrupt:
pass
#turn off the buzzer
pi.write(buzzer, 0)
#stop the connection with the daemon
pi.stop()
#clean all the used ports
GPIO.cleanup()
Comments