Advertisement
Guest User

HC-sr04

a guest
Sep 10th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. import RPi.GPIO as GPIO    #Importamos la librería GPIO
  2. import time                #Importamos time (time.sleep)
  3. GPIO.setmode(GPIO.BCM)     #Ponemos la placa en modo BCM
  4. GPIO_TRIGGER = 25          #Usamos el pin GPIO 25 como TRIGGER
  5. GPIO_ECHO    = 7           #Usamos el pin GPIO 7 como ECHO
  6. GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  #Configuramos Trigger como salida
  7. GPIO.setup(GPIO_ECHO,GPIO.IN)      #Configuramos Echo como entrada
  8. GPIO.output(GPIO_TRIGGER,False)    #Ponemos el pin 25 como LOW
  9.  
  10.  
  11. try:
  12.     while True:     #Iniciamos un loop infinito
  13.         GPIO.output(GPIO_TRIGGER,True)   #Enviamos un pulso de ultrasonidos
  14.         time.sleep(0.00001)              #Una pequeñña pausa
  15.         GPIO.output(GPIO_TRIGGER,False)  #Apagamos el pulso
  16.         start = time.time()              #Guarda el tiempo actual mediante time.time()
  17.         while GPIO.input(GPIO_ECHO)==0:  #Mientras el sensor no reciba señal...
  18.             start = time.time()          #Mantenemos el tiempo actual mediante time.time()
  19.         while GPIO.input(GPIO_ECHO)==1:  #Si el sensor recibe señal...
  20.             stop = time.time()           #Guarda el tiempo actual mediante time.time() en otra variable
  21.         elapsed = stop-start             #Obtenemos el tiempo transcurrido entre envío y recepción
  22.         distance = (elapsed * 34300)/2   #Distancia es igual a tiempo por velocidad partido por 2   D = (T x V)/2
  23.        
  24.        
  25.          GPIO.output(GPIO_TRIGGER,True)   #Enviamos un pulso de ultrasonidos
  26.         time.sleep(0.00001)              #Una pequeñña pausa
  27.         GPIO.output(GPIO_TRIGGER,False)  #Apagamos el pulso
  28.         start2 = time.time()              #Guarda el tiempo actual mediante time.time()
  29.         while GPIO.input(GPIO_ECHO)==0:  #Mientras el sensor no reciba señal...
  30.             start2 = time.time()          #Mantenemos el tiempo actual mediante time.time()
  31.         while GPIO.input(GPIO_ECHO)==1:  #Si el sensor recibe señal...
  32.             stop2 = time.time()           #Guarda el tiempo actual mediante time.time() en otra variable
  33.         elapsed2 = stop2-start2             #Obtenemos el tiempo transcurrido entre envío y recepción
  34.         distance2 = (elapsed2 * 34300)/2   #Distancia es igual a tiempo por velocidad partido por 2   D = (T x V)/2
  35.        
  36.         distance3 = distance1 - distance2
  37.        
  38.         print distance3  
  39.         time.sleep(1)          
  40. except KeyboardInterrupt:                #Si el usuario pulsa CONTROL+C...
  41.     print "quit"                         #Avisamos del cierre al usuario
  42.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement