Facebook
From ka, 3 Years ago, written in Plain Text.
This paste is a reply to ultrasonic 1 from ka - view diff
Embed
Download Paste or View Raw
Hits: 123
  1. const int trigPin = 9;
  2. const int echoPin = 10;
  3. long duration;
  4. int distanceCm;
  5. void setup() {
  6.  
  7. pinMode(trigPin, OUTPUT);
  8. pinMode(echoPin, INPUT);
  9. Serial.begin(9600);
  10.  
  11. }
  12. void loop() {
  13. digitalWrite(trigPin, LOW);
  14. delayMicroseconds(2);
  15. digitalWrite(trigPin, HIGH);
  16. delayMicroseconds(10);
  17. digitalWrite(trigPin, LOW);
  18. duration = pulseIn(echoPin, HIGH);
  19. distanceCm= duration*0.034/2;
  20.  
  21.       Serial.println(distanceCm);// if distance less than 0.5 meter and more than 0 (0 or less means over range)
  22.  
  23. delay(100);
  24.  
  25. }
captcha