daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Jan 5th, 2017 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Bibliotheken einbinden
  2. import RPi.GPIO as GPIO
  3. import time
  4.  
  5. #GPIO Modus (BOARD / BCM)
  6. GPIO.setmode(GPIO.BOARD)
  7. GPIO.setwarnings(False)
  8. #GPIO Pins zuweisen
  9. GPIO_TRIGGER = 16
  10. GPIO_ECHO = 18
  11.  
  12. #Richtung der GPIO-Pins festlegen (IN / OUT)
  13. GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
  14. GPIO.setup(GPIO_ECHO, GPIO.IN)
  15.  
  16.  
  17. def distanz():
  18.         # setze Trigger auf HIGH
  19.         GPIO.output(GPIO_TRIGGER, True)
  20.  
  21.         # setze Trigger nach 0.01ms aus LOW
  22.         time.sleep(0.00001)
  23.         GPIO.output(GPIO_TRIGGER, False)
  24.  
  25.         StartZeit = time.time()
  26.         StopZeit = time.time()
  27.  
  28.         # speichere Startzeit
  29.         while GPIO.input(GPIO_ECHO) == 0:
  30.                 StartZeit = time.time()
  31.  
  32.         # speichere Ankunftszeit
  33.         while GPIO.input(GPIO_ECHO) == 1:
  34.                 StopZeit = time.time()
  35.  
  36.         # Zeit Differenz zwischen Start und Ankunft
  37.         TimeElapsed = StopZeit - StartZeit
  38.         # mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
  39.         # und durch 2 teilen, da hin und zurueck
  40.         distanz = (TimeElapsed * 34300) / 2
  41.  
  42.         return distanz
  43.  
  44. if __name__ == '__main__':
  45.  
  46.   try:
  47.  
  48.     file_out = open("Testdatei.txt","w")
  49.  
  50.     while True:
  51.  
  52.       abstand = distanz()
  53.  
  54.       print ("Gemessene Entfernung = %.1f cm" % abstand)
  55.  
  56.       file_out.write("%.1f" % abstand)
  57.  
  58.       time.sleep(5)
  59.  
  60.  
  61.  
  62.   # Beim Abbruch durch STRG+C resetten
  63.  
  64.   except KeyboardInterrupt:
  65.  
  66.     print("Messung vom User gestoppt")
  67.  
  68.     GPIO.cleanup()
  69.  
  70.     file_out.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top