Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ultrasonic_Sensor.ino 826B

1234567891011121314151617181920212223242526272829303132333435
  1. // defines pins numbers
  2. const int trigPin = 2; //D4
  3. const int echoPin = 0; //D3
  4. // defines variables
  5. long duration;
  6. int distance;
  7. void setup() {
  8. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  9. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  10. Serial.begin(9600); // Starts the serial communication
  11. }
  12. void loop() {
  13. // Clears the trigPin
  14. digitalWrite(trigPin, LOW);
  15. delayMicroseconds(2);
  16. // Sets the trigPin on HIGH state for 10 micro seconds
  17. digitalWrite(trigPin, HIGH);
  18. delayMicroseconds(10);
  19. digitalWrite(trigPin, LOW);
  20. // Reads the echoPin, returns the sound wave travel time in microseconds
  21. duration = pulseIn(echoPin, HIGH);
  22. // Calculating the distance
  23. distance= duration*0.034/2;
  24. // Prints the distance on the Serial Monitor
  25. Serial.print("Distance: ");
  26. Serial.println(distance);
  27. delay(2000);
  28. }