Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1.  
  2. #define trigPin D5
  3. #define echoPin D6
  4.  
  5. void setup() {
  6. pinMode(D10, OUTPUT);
  7. Serial.begin (115200);
  8. pinMode(trigPin, OUTPUT);
  9. pinMode(echoPin, INPUT);
  10. }
  11.  
  12. void loop() {
  13. long duration, distance;
  14. digitalWrite(trigPin, LOW); // Added this line
  15. delayMicroseconds(2); // Added this line
  16. digitalWrite(trigPin, HIGH);
  17. delayMicroseconds(10); // Added this line
  18. digitalWrite(trigPin, LOW);
  19. duration = pulseIn(echoPin, HIGH);
  20. distance = (duration/2) / 29.1;
  21.  
  22. Serial.print(distance);
  23. Serial.println(" cm");
  24.  
  25.  
  26.  
  27. if (distance < 100)
  28. {
  29. // actie A
  30. Serial.println("Lichtje aan");
  31. digitalWrite(D10,LOW);
  32. }
  33. else
  34. {
  35. // actie B
  36. Serial.println("Lichtje uit");
  37. digitalWrite(D10,HIGH);
  38. }
  39.  
  40.  
  41. delay(500);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement