Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import explorerhat
  4.  
  5. GPIO.setmode(GPIO.BCM)
  6.  
  7. TRIG = 31
  8. ECHO = 23
  9.  
  10. print "Distance Measurement In Progress"
  11.  
  12. GPIO.setup(TRIG,GPIO.OUT)
  13. GPIO.setup(ECHO,GPIO.IN)
  14.  
  15. GPIO.output(TRIG, False)
  16. print "Waiting For Sensor To Settle"
  17. time.sleep(1)
  18.  
  19. while True:
  20.     GPIO.output(TRIG, True)
  21.     time.sleep(0.00001)
  22.     GPIO.output(TRIG, False)
  23.    
  24.     while GPIO.input(ECHO)==0:
  25.       pulse_start = time.time()
  26.    
  27.     while GPIO.input(ECHO)==1:
  28.       pulse_end = time.time()
  29.    
  30.     pulse_duration = pulse_end - pulse_start
  31.    
  32.     distance = pulse_duration * 17150
  33.    
  34.     distance = round(distance, 2)
  35.    
  36.     if distance < 10:
  37.         explorerhat.light.off()
  38.         explorerhat.light[0].on()
  39.         explorerhat.light[1].on()
  40.         explorerhat.light[2].on()
  41.         explorerhat.light[3].on()
  42.     elif distance < 20:
  43.         explorerhat.light.off()
  44.         explorerhat.light[0].on()
  45.         explorerhat.light[1].on()
  46.         explorerhat.light[2].on()
  47.     elif distance < 30:
  48.         explorerhat.light.off()
  49.         explorerhat.light[0].on()
  50.         explorerhat.light[1].on()
  51.     elif distance < 40:
  52.         explorerhat.light.off()
  53.         explorerhat.light[0].on()
  54.     else:
  55.         explorerhat.light.off()
  56.        
  57.     print "Distance:",distance,"cm"
  58.    
  59.     time.sleep(1)
  60.  
  61.  
  62.  
  63. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement