Guest User

hc

a guest
Dec 27th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import RPi.GPIO as GPIO import time import threading GPIO.setmode(GPIO.BCM)
  2.  
  3. TRIG = 14
  4. ECHO = 15
  5.  
  6. L = 24
  7.  
  8. TRIG_W = 23
  9. ECHO_W = 18
  10.  
  11. distance1 = 0
  12. distance2 = 0
  13.  
  14. a = 0
  15. b = 0
  16. c = 0
  17.  
  18. GPIO.setup(TRIG,GPIO.OUT)
  19. GPIO.setup(ECHO,GPIO.IN)
  20. GPIO.setup(TRIG_W,GPIO.OUT)
  21. GPIO.setup(ECHO_W,GPIO.IN)
  22. GPIO.setup(L,GPIO.OUT)
  23.  
  24. def ultrasonic1():
  25.     try:
  26.             GPIO.output(TRIG, False)
  27.             time.sleep(1)
  28.          
  29.             GPIO.output(TRIG, True)
  30.             time.sleep(0.00001)
  31.             GPIO.output(TRIG, False)
  32.          
  33.             while GPIO.input(ECHO)==0:
  34.                 pulse_start = time.time()
  35.      
  36.             while GPIO.input(ECHO)==1:
  37.                 pulse_end = time.time()
  38.      
  39.             pulse_duration = pulse_end - pulse_start
  40.        
  41.             global distance1   
  42.             distance1 = pulse_duration * 17150
  43.      
  44.             distance1 = round(distance1, 2)
  45.          
  46.             print "Distance1:",distance1,"cm"
  47.             if distance1 < 20:
  48.                 global a
  49.                 a += 1
  50.             time.sleep(0.2)
  51.     except KeyboardInterrupt:
  52.         print "KeyboardInterrupt detected!"
  53.         GPIO.cleanup()
  54. def ultrasonic2():
  55.         try:
  56.                         GPIO.output(TRIG_W, False)
  57.                         time.sleep(1)
  58.  
  59.                         GPIO.output(TRIG_W, True)
  60.                         time.sleep(0.00001)
  61.                         GPIO.output(TRIG_W, False)
  62.  
  63.                         while GPIO.input(ECHO_W)==0:
  64.                                 pulse_start = time.time()
  65.          
  66.                         while GPIO.input(ECHO_W)==1:
  67.                                 pulse_end = time.time()
  68.          
  69.                         pulse_duration = pulse_end - pulse_start
  70.                         global distance2
  71.                         distance2 = pulse_duration * 17150
  72.  
  73.                         distance2 = round(distance2, 2)
  74.  
  75.                         print "Distance2:",distance2,"cm"
  76.             if distance2 < 20:
  77.                 global b
  78.                 b += 1
  79.                         time.sleep(0.2)
  80.         except KeyboardInterrupt:
  81.                 print "KeyboardInterrupt detected!"
  82.                 GPIO.cleanup()
  83.  
  84. try:
  85.     while True:
  86.                 e1 = threading.Event()
  87.                 e2 = threading.Event()
  88.                 t1 = threading.Thread(target=ultrasonic1)
  89.                 t2 = threading.Thread(target=ultrasonic2)
  90.                 t1.start()
  91.                 t2.start()
  92.         if b > a:
  93.             c = 1
  94.         elif a > b:
  95.             c = 0
  96.         if c <= 0:
  97.             print "a = ", a, "b = ", b
  98.             print "c = ", c
  99.             print "turn off"
  100.                         GPIO.output(L,GPIO.LOW)
  101.         elif c >= 1:
  102.             print "a = ", a, "b = ", b
  103.                         print "c = ", c
  104.                         print "turn on"
  105.                         GPIO.output(L,GPIO.HIGH)
  106.                 time.sleep(0.5)
  107. except KeyboardInterrupt:
  108.         print "KeyboardInterrupt detected!"
  109.     t1.join()
  110.         t2.join()
  111.         GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment