kagelar

Untitled

Jul 28th, 2020
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. const int trigPin = 7;
  2. const int echoPin = 6;
  3. const int buzzPin = 5;
  4. const int ledPin = 4;
  5.  
  6. long duration;
  7.  
  8. long microsecondsToCentimeters(long microseconds) {
  9. return microseconds / 29 / 2;
  10. }
  11.  
  12. void setup() {
  13. Serial.begin(9600);
  14. pinMode(trigPin, OUTPUT);
  15. pinMode(echoPin, INPUT);
  16. pinMode(ledPin, OUTPUT);
  17. }
  18.  
  19. void loop() {
  20. long duration, cm;
  21. pinMode(trigPin, OUTPUT);
  22. digitalWrite(trigPin, LOW);
  23. delayMicroseconds(2);
  24. digitalWrite(trigPin, HIGH);
  25. delayMicroseconds(10);
  26. digitalWrite(trigPin, LOW);
  27. pinMode(echoPin, INPUT);
  28. duration = pulseIn(echoPin, HIGH);
  29. cm = microsecondsToCentimeters(duration);
  30. Serial.print(cm);
  31. Serial.print("cm");
  32. Serial.println();
  33. delay(100);
  34.  
  35. digitalWrite(ledPin, HIGH); //here i need to do something with if and else but i dont know how
  36. digitalWrite(buzzPin, HIGH);
  37.  
  38. }
Add Comment
Please, Sign In to add comment