anandvgeorge

ultrasonic.ino

May 25th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #define TRIG 13
  2. #define ECHO 12
  3.  
  4. void setup() {
  5.   Serial.begin (9600);
  6.   pinMode(TRIG, OUTPUT);
  7.   pinMode(ECHO, INPUT);
  8. }
  9.  
  10. void loop() {
  11.   long duration, distance;
  12.   digitalWrite(TRIG, LOW);
  13.   delayMicroseconds(2);
  14.   digitalWrite(TRIG, HIGH);
  15.   delayMicroseconds(10);
  16.   digitalWrite(TRIG, LOW);
  17.   duration = pulseIn(ECHO, HIGH);
  18.   distance = (duration/2) / 29.1;
  19.  
  20.   if (distance >= 400 || distance <= 0){
  21.     Serial.println("Out of range");
  22.   }
  23.   else {
  24.     Serial.print(distance);
  25.     Serial.println(" cm");
  26.   }
  27.   delay(500);
  28. }
Add Comment
Please, Sign In to add comment