Advertisement
Guest User

rpi

a guest
Aug 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.62 KB | None | 0 0
  1. #Libraries
  2. import RPi.GPIO as GPIO
  3. import time
  4. import os
  5. import subprocess #for kill process
  6. import threading
  7. import I2C_LCD_driver  # SDA (GPIO2), SCL (GPIO3)
  8. from gpiozero import Buzzer,LED
  9. from time import sleep
  10.  
  11. #Set GPIO MODE (BOARD/BCM)
  12. GPIO.setmode(GPIO.BCM)
  13. GPIO.setwarnings(False)
  14.  
  15. #set GPIO Pins
  16. ############### sensor1 ######################
  17. GPIO_TRIGGER = 23
  18. GPIO_ECHO= 24
  19. ############### sensor2 ######################
  20. GPIO_TRIGGER1 = 6              
  21. GPIO_ECHO1 = 5                  
  22. ############### sensor3 ######################
  23. GPIO_TRIGGER2 = 19
  24. GPIO_ECHO2 = 26
  25. ##############################################
  26. PinLED_Sensor1 = 16
  27. PinLED_Sensor2 = 20
  28. PinLED_Sensor3 = 21
  29. ##############################################
  30. Pin_SERVO = 4  # 7.5 tengah (0 derajat), 2.5 kiri (-90), 12.5 kanan  
  31. Pin_Buzzer = 12
  32.  
  33. #set GPIO direction (IN / OUT)
  34. ############### sensor1 ######################
  35. GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
  36. GPIO.setup(GPIO_ECHO, GPIO.IN)
  37. ############### sensor2 ######################
  38. GPIO.setup(GPIO_TRIGGER1, GPIO.OUT)
  39. GPIO.setup(GPIO_ECHO1, GPIO.IN)
  40. ###
  41. ############ sensor3 ######################
  42. GPIO.setup(GPIO_TRIGGER2, GPIO.OUT)
  43. GPIO.setup(GPIO_ECHO2, GPIO.IN)
  44. ##############################################
  45.  
  46. #set GPIO SERVO
  47. ##############################################
  48. GPIO.setup (Pin_SERVO,GPIO.OUT)
  49. p = GPIO.PWM(Pin_SERVO,50)
  50. ##############################################
  51.  
  52. #Set LCD
  53. ##############################################
  54. lcdKu= I2C_LCD_driver.lcd()
  55.  
  56. #Set GPIOZERO Buzzer
  57. ##############################################
  58. buzzer = Buzzer(Pin_Buzzer)
  59.  
  60. #Set GPIOZERO LED
  61. ##############################################
  62. LED_Sensor1 =LED(PinLED_Sensor1)
  63. LED_Sensor2 =LED(PinLED_Sensor2)
  64. LED_Sensor3 =LED(PinLED_Sensor3)
  65.  
  66.  
  67. #Fungsi Sensor 3
  68. def sensor3():
  69.     global aktifSen3
  70.     GPIO.output(GPIO_TRIGGER2,False)
  71.     time.sleep(0.0002)
  72.     # set Trigger to HIGH
  73.     GPIO.output(GPIO_TRIGGER2, True)
  74.  
  75.     # set Trigger after 0.01ms to LOW
  76.     time.sleep(0.00001)
  77.     GPIO.output(GPIO_TRIGGER2, False)
  78.  
  79.     StartTime = time.time()
  80.     StopTime = time.time()
  81.     TimeElapsed=0
  82.     # save StartTime
  83.     while GPIO.input(GPIO_ECHO2) == 0:
  84.         StartTime = time.time()
  85.    
  86.     # save time of arrival max about 15cm
  87.     while (GPIO.input(GPIO_ECHO2) == 1) and  (TimeElapsed <0.0009) :
  88.         StopTime = time.time()
  89.         TimeElapsed = StopTime - StartTime
  90.     # time difference between start and arrival
  91.     TimeElapsed = StopTime - StartTime
  92.     # multiply with the sonic speed (34300 cm/s)
  93.     # and divide by 2, because there and back
  94.     sensor3 = (TimeElapsed * 34300) / 2
  95.     print("Sensor3 ", sensor3)
  96.     if sensor3 <10 :
  97.         aktifSen3=True
  98. ##############################################
  99. # Fungsi jarak sensor 2
  100. def sensor2():
  101.     global aktifSen2
  102.     GPIO.output(GPIO_TRIGGER1,False)
  103.     time.sleep(0.0002)
  104.     # set Trigger to HIGH
  105.     GPIO.output(GPIO_TRIGGER1, True)
  106.  
  107.     # set Trigger after 0.01ms to LOW
  108.     time.sleep(0.00001)
  109.     GPIO.output(GPIO_TRIGGER1, False)
  110.  
  111.     StartTime = time.time()
  112.     StopTime = time.time()
  113.     TimeElapsed=0
  114.     # save StartTime
  115.     while GPIO.input(GPIO_ECHO1) == 0:
  116.         StartTime = time.time()
  117.    
  118.     # save time of arrival max about 15cm
  119.     while (GPIO.input(GPIO_ECHO1) == 1) and  (TimeElapsed <0.0009) :
  120.         StopTime = time.time()
  121.         TimeElapsed = StopTime - StartTime
  122.  
  123.     # time difference between start and arrival
  124.     # TimeElapsed = StopTime - StartTime
  125.     # multiply with the sonic speed (34300 cm/s)
  126.     # and divide by 2, because there and back
  127.     sensor2 = (TimeElapsed * 34300) / 2
  128.     print("Sensor2 ",sensor2)   #for debugging only
  129. #    print(TimeElapsed) #for debugging only
  130.     if sensor2 <10 :
  131.         aktifSen2=True
  132. ##############################################
  133.  
  134. # Fungsijarak sensor 1
  135. def sensor1():
  136.     global aktifSen1
  137.     GPIO.output(GPIO_TRIGGER,False)
  138.     time.sleep(0.0002)    
  139.  
  140.     GPIO.output(GPIO_TRIGGER, True)
  141.         # set Trigger after 0.01ms to LOW
  142.     time.sleep(0.00001)
  143.     GPIO.output(GPIO_TRIGGER, False)
  144.  
  145.     StartTime = time.time()
  146.     StopTime = time.time()
  147.     TimeElapsed=0
  148.         # save StartTime
  149.     while GPIO.input(GPIO_ECHO) == 0:
  150.         StartTime = time.time()
  151.  
  152.     while (GPIO.input(GPIO_ECHO)) == 1 and (TimeElapsed < 0.0009):
  153.         StopTime = time.time()
  154.         TimeElapsed = StopTime - StartTime
  155.     # time difference between start and arrival
  156.     # TimeElapsed = StopTime - StartTime
  157.     # multiply with the sonic speed (34300 cm/s)
  158.     # and divide by 2, because there and back
  159.     sensor1 = (TimeElapsed * 34300) / 2
  160. #    print(sensor1)
  161. #    print(TimeElapsed)
  162.     if sensor1<10 :
  163.         aktifSen1=True
  164.     print("Sensor1 ",sensor1)
  165. #############################################
  166. #Global variabel
  167. aktifSen1=False
  168. aktifSen2=False
  169. aktifSen3=False
  170. jarak = 40
  171. ############################################
  172.  
  173.  
  174.  
  175. if __name__ == '__main__':
  176.     try:
  177.         p.start(0)
  178.         flagLCD=True
  179.         flagAudio4=False
  180.         os.system('mpg123 -q awal.mp3 &') #Audio 1
  181.         while True:
  182.             if(flagLCD==True):
  183.                 lcdKu.lcd_clear()
  184.                 lcdKu.lcd_display_string("AutomaticX K-Api")
  185.                 flagLCD=False
  186.             if(flagAudio4==True):
  187.                 subprocess.call(['killall','mpg123']) #kill all mpg123 process (off audio4)
  188.                 flagAudio4=False
  189.             LED_Sensor1.off()
  190.             LED_Sensor2.off()
  191.             LED_Sensor3.off()
  192.             Durasi=0
  193.             aktifSen1=False
  194.             aktifSen2=False
  195.             aktifSen3=False
  196.             TimeAwal=time.time()
  197.             TimeAkhir=time.time()
  198.             sensor1()  #cek sensor1
  199.             if aktifSen1==True: #jika kurang dari 10 masuk blok if
  200.                 TimeAwal=time.time()# Waktu awal unt hit kecepatan
  201.                 LED_Sensor3.off()
  202.                 lcdKu.lcd_clear()
  203.                 lcdKu.lcd_display_string(time.strftime("%d/%m/%Y"),1,0)
  204.                 lcdKu.lcd_display_string(time.strftime("%H:%M:%S"),1,11)
  205.                 LED_Sensor1.on()
  206.                 subprocess.call(['killall','mpg123']) #kill all mpg123 process
  207.                 os.system('mpg123 -q 111.mp3 &') #playing mpg123 player (Audio2)
  208.                 while aktifSen2==False: #tunggu sensor2 di bawah 10 cm
  209.                       sensor2()
  210.                       TimeAkhir=time.time() #Waktu akhir
  211.                       time.sleep(0.01)
  212.                 LED_Sensor2.blink(0.2,0.2)   #jika sensor 2 dibawah 10cm
  213.                 LED_Sensor1.off()   #Led2 kedip
  214.                 p.start (12.5)      #palang menutup
  215.                 buzzer.beep()       #buzzer aktif
  216.                 Durasi=TimeAkhir-TimeAwal #hitung durasi
  217.                 Kecepatan=jarak/Durasi #hitung kecepatan
  218.                 lcdKu.lcd_clear()   #tampilkan di LCD
  219.                 lcdKu.lcd_display_string("Waktu= ",1,0)
  220.                 lcdKu.lcd_display_string("%.2f" %Durasi,1,7)
  221.                 lcdKu.lcd_display_string("det",1,13)
  222.                 lcdKu.lcd_display_string("Speed= ",2,0)
  223.                 lcdKu.lcd_display_string("%.2f" %Kecepatan,2,7)
  224.                 lcdKu.lcd_display_string("m/s",2,13)
  225.                 print ("Durasi =", Durasi) #tampil di monitor for debugging
  226.                 print ("Speed =", Kecepatan)
  227.                 subprocess.call(['killall','mpg123']) #kill all mpg123 process
  228.                 os.system('mpg123 -q 222.mp3 &') #playing mpg123 player (Audio3)
  229.                 waktu=200/Kecepatan
  230.                 while waktu >= 0:
  231.                     print(waktu,)
  232.                     time.sleep(1)
  233.                     waktu -= 1
  234.                 while aktifSen3==False: #tunggu sensor 3 kurang dari 10cm
  235.                       sensor3()
  236.                       time.sleep(0.01)
  237.                                    
  238.                 time.sleep(3)   #jika kurang dr 10, ditunda untuk gerong
  239.                 LED_Sensor2.off() #Led sen2 padam
  240.                 LED_Sensor3.on() #Led Sen3 nyala
  241.                 p.ChangeDutyCycle (7.5) #Palang naik
  242.                 buzzer.off()
  243.                 subprocess.call(['killall','mpg123']) #kill all mpg123 process
  244.                 os.system('mpg123 -q 333.mp3 &') #playing mpg123 player (Audio3)
  245.                 flagLCD=True
  246.                 flagAudio4=False
  247.                 time.sleep(3)
  248.             time.sleep(0.01)
  249.        
  250.  
  251. # Reset by pressing CTRL + C
  252.     except KeyboardInterrupt:
  253.         lcdKu.lcd_clear()
  254.         lcdKu.lcd_display_string("Selamat tinggal")
  255.         print("Measurement stopped by User")
  256.         p.stop()
  257. #        GPIO.cleanup()
  258.  
  259.  
  260. #Importations
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement