Advertisement
computermuseo

sonar arduino

Mar 24th, 2015
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. int trigPin = 2;
  2. int echoPin = 4;
  3.  
  4. void setup() {
  5. Serial.begin(9600);
  6. }
  7.  
  8. void loop(){
  9. long duration;
  10. float cm;
  11. pinMode(echoPin, INPUT);
  12. pinMode(trigPin, OUTPUT);
  13. digitalWrite(trigPin, LOW);
  14. delayMicroseconds(2);
  15. digitalWrite(trigPin, HIGH);
  16. delayMicroseconds(10);
  17. digitalWrite(trigPin, LOW);
  18. duration = pulseIn(echoPin, HIGH);
  19. cm = microsecondsToCentimeters(duration);
  20. Serial.print(cm);
  21. Serial.print("cm");
  22. Serial.println();
  23. delay(100);
  24. }
  25. float microsecondsToCentimeters(long microseconds){
  26. return (microseconds*0.034029)/2;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement