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 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #define DISTANCE 15
  2. // defines pins numbers
  3. const int trigPin = 2; //D4
  4. const int echoPin = 0; //D3
  5. // defines variables
  6. long duration;
  7. int distance;
  8. int bool1 = 0;
  9. void setup() {
  10. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  11. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  12. Serial.begin(9600); // Starts the serial communication
  13. }
  14. void loop() {
  15. // Clears the trigPin
  16. digitalWrite(trigPin, LOW);
  17. delayMicroseconds(2);
  18. // Sets the trigPin on HIGH state for 10 micro seconds
  19. digitalWrite(trigPin, HIGH);
  20. delayMicroseconds(10);
  21. digitalWrite(trigPin, LOW);
  22. // Reads the echoPin, returns the sound wave travel time in microseconds
  23. duration = pulseIn(echoPin, HIGH);
  24. // Calculating the distance
  25. distance = duration * 0.034 / 2;
  26. // Prints the distance on the Serial Monitor
  27. if (bool1 == 0) {
  28. //if ((distance != (DISTANCE - 1)) && (distance != (DISTANCE)) && (distance != (DISTANCE + 1))) { //+-1
  29. if(((DISTANCE+2)<distance)|| ((DISTANCE-2)>distance)){ //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern
  30. Serial.print("Motion detected: ");
  31. Serial.println(distance);
  32. bool1 = 1;
  33. }
  34. }
  35. else if(bool1==1){
  36. if(((DISTANCE+2)<distance)|| ((DISTANCE-2)>distance)){ //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern
  37. Serial.print("Still motion detected: ");
  38. Serial.println(distance);
  39. }
  40. else{
  41. //Flag wieder auf 0
  42. bool1=0;
  43. }
  44. }
  45. delay(100);
  46. }