Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Libraries
- import RPi.GPIO as GPIO
- import time
- import os
- import subprocess #for kill process
- import threading
- import I2C_LCD_driver # SDA (GPIO2), SCL (GPIO3)
- from gpiozero import Buzzer,LED
- from time import sleep
- #Set GPIO MODE (BOARD/BCM)
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- #set GPIO Pins
- ############### sensor1 ######################
- GPIO_TRIGGER = 23
- GPIO_ECHO= 24
- ############### sensor2 ######################
- GPIO_TRIGGER1 = 6
- GPIO_ECHO1 = 5
- ############### sensor3 ######################
- GPIO_TRIGGER2 = 19
- GPIO_ECHO2 = 26
- ##############################################
- PinLED_Sensor1 = 16
- PinLED_Sensor2 = 20
- PinLED_Sensor3 = 21
- ##############################################
- Pin_SERVO = 4 # 7.5 tengah (0 derajat), 2.5 kiri (-90), 12.5 kanan
- Pin_Buzzer = 12
- #set GPIO direction (IN / OUT)
- ############### sensor1 ######################
- GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
- GPIO.setup(GPIO_ECHO, GPIO.IN)
- ############### sensor2 ######################
- GPIO.setup(GPIO_TRIGGER1, GPIO.OUT)
- GPIO.setup(GPIO_ECHO1, GPIO.IN)
- ###
- ############ sensor3 ######################
- GPIO.setup(GPIO_TRIGGER2, GPIO.OUT)
- GPIO.setup(GPIO_ECHO2, GPIO.IN)
- ##############################################
- #set GPIO SERVO
- ##############################################
- GPIO.setup (Pin_SERVO,GPIO.OUT)
- p = GPIO.PWM(Pin_SERVO,50)
- ##############################################
- #Set LCD
- ##############################################
- lcdKu= I2C_LCD_driver.lcd()
- #Set GPIOZERO Buzzer
- ##############################################
- buzzer = Buzzer(Pin_Buzzer)
- #Set GPIOZERO LED
- ##############################################
- LED_Sensor1 =LED(PinLED_Sensor1)
- LED_Sensor2 =LED(PinLED_Sensor2)
- LED_Sensor3 =LED(PinLED_Sensor3)
- #Fungsi Sensor 3
- def sensor3():
- global aktifSen3
- GPIO.output(GPIO_TRIGGER2,False)
- time.sleep(0.0002)
- # set Trigger to HIGH
- GPIO.output(GPIO_TRIGGER2, True)
- # set Trigger after 0.01ms to LOW
- time.sleep(0.00001)
- GPIO.output(GPIO_TRIGGER2, False)
- StartTime = time.time()
- StopTime = time.time()
- TimeElapsed=0
- # save StartTime
- while GPIO.input(GPIO_ECHO2) == 0:
- StartTime = time.time()
- # save time of arrival max about 15cm
- while (GPIO.input(GPIO_ECHO2) == 1) and (TimeElapsed <0.0009) :
- StopTime = time.time()
- TimeElapsed = StopTime - StartTime
- # 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
- sensor3 = (TimeElapsed * 34300) / 2
- print("Sensor3 ", sensor3)
- if sensor3 <10 :
- aktifSen3=True
- ##############################################
- # Fungsi jarak sensor 2
- def sensor2():
- global aktifSen2
- GPIO.output(GPIO_TRIGGER1,False)
- time.sleep(0.0002)
- # set Trigger to HIGH
- GPIO.output(GPIO_TRIGGER1, True)
- # set Trigger after 0.01ms to LOW
- time.sleep(0.00001)
- GPIO.output(GPIO_TRIGGER1, False)
- StartTime = time.time()
- StopTime = time.time()
- TimeElapsed=0
- # save StartTime
- while GPIO.input(GPIO_ECHO1) == 0:
- StartTime = time.time()
- # save time of arrival max about 15cm
- while (GPIO.input(GPIO_ECHO1) == 1) and (TimeElapsed <0.0009) :
- StopTime = time.time()
- TimeElapsed = StopTime - StartTime
- # 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
- sensor2 = (TimeElapsed * 34300) / 2
- print("Sensor2 ",sensor2) #for debugging only
- # print(TimeElapsed) #for debugging only
- if sensor2 <10 :
- aktifSen2=True
- ##############################################
- # Fungsijarak sensor 1
- def sensor1():
- global aktifSen1
- GPIO.output(GPIO_TRIGGER,False)
- time.sleep(0.0002)
- 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()
- TimeElapsed=0
- # save StartTime
- while GPIO.input(GPIO_ECHO) == 0:
- StartTime = time.time()
- while (GPIO.input(GPIO_ECHO)) == 1 and (TimeElapsed < 0.0009):
- StopTime = time.time()
- TimeElapsed = StopTime - StartTime
- # 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
- sensor1 = (TimeElapsed * 34300) / 2
- # print(sensor1)
- # print(TimeElapsed)
- if sensor1<10 :
- aktifSen1=True
- print("Sensor1 ",sensor1)
- #############################################
- #Global variabel
- aktifSen1=False
- aktifSen2=False
- aktifSen3=False
- jarak = 40
- ############################################
- if __name__ == '__main__':
- try:
- p.start(0)
- flagLCD=True
- flagAudio4=False
- os.system('mpg123 -q awal.mp3 &') #Audio 1
- while True:
- if(flagLCD==True):
- lcdKu.lcd_clear()
- lcdKu.lcd_display_string("AutomaticX K-Api")
- flagLCD=False
- if(flagAudio4==True):
- subprocess.call(['killall','mpg123']) #kill all mpg123 process (off audio4)
- flagAudio4=False
- LED_Sensor1.off()
- LED_Sensor2.off()
- LED_Sensor3.off()
- Durasi=0
- aktifSen1=False
- aktifSen2=False
- aktifSen3=False
- TimeAwal=time.time()
- TimeAkhir=time.time()
- sensor1() #cek sensor1
- if aktifSen1==True: #jika kurang dari 10 masuk blok if
- TimeAwal=time.time()# Waktu awal unt hit kecepatan
- LED_Sensor3.off()
- lcdKu.lcd_clear()
- lcdKu.lcd_display_string(time.strftime("%d/%m/%Y"),1,0)
- lcdKu.lcd_display_string(time.strftime("%H:%M:%S"),1,11)
- LED_Sensor1.on()
- subprocess.call(['killall','mpg123']) #kill all mpg123 process
- os.system('mpg123 -q 111.mp3 &') #playing mpg123 player (Audio2)
- while aktifSen2==False: #tunggu sensor2 di bawah 10 cm
- sensor2()
- TimeAkhir=time.time() #Waktu akhir
- time.sleep(0.01)
- LED_Sensor2.blink(0.2,0.2) #jika sensor 2 dibawah 10cm
- LED_Sensor1.off() #Led2 kedip
- p.start (12.5) #palang menutup
- buzzer.beep() #buzzer aktif
- Durasi=TimeAkhir-TimeAwal #hitung durasi
- Kecepatan=jarak/Durasi #hitung kecepatan
- lcdKu.lcd_clear() #tampilkan di LCD
- lcdKu.lcd_display_string("Waktu= ",1,0)
- lcdKu.lcd_display_string("%.2f" %Durasi,1,7)
- lcdKu.lcd_display_string("det",1,13)
- lcdKu.lcd_display_string("Speed= ",2,0)
- lcdKu.lcd_display_string("%.2f" %Kecepatan,2,7)
- lcdKu.lcd_display_string("m/s",2,13)
- print ("Durasi =", Durasi) #tampil di monitor for debugging
- print ("Speed =", Kecepatan)
- subprocess.call(['killall','mpg123']) #kill all mpg123 process
- os.system('mpg123 -q 222.mp3 &') #playing mpg123 player (Audio3)
- waktu=200/Kecepatan
- while waktu >= 0:
- print(waktu,)
- time.sleep(1)
- waktu -= 1
- while aktifSen3==False: #tunggu sensor 3 kurang dari 10cm
- sensor3()
- time.sleep(0.01)
- time.sleep(3) #jika kurang dr 10, ditunda untuk gerong
- LED_Sensor2.off() #Led sen2 padam
- LED_Sensor3.on() #Led Sen3 nyala
- p.ChangeDutyCycle (7.5) #Palang naik
- buzzer.off()
- subprocess.call(['killall','mpg123']) #kill all mpg123 process
- os.system('mpg123 -q 333.mp3 &') #playing mpg123 player (Audio3)
- flagLCD=True
- flagAudio4=False
- time.sleep(3)
- time.sleep(0.01)
- # Reset by pressing CTRL + C
- except KeyboardInterrupt:
- lcdKu.lcd_clear()
- lcdKu.lcd_display_string("Selamat tinggal")
- print("Measurement stopped by User")
- p.stop()
- # GPIO.cleanup()
- #Importations
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement