document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  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. }
');