TeguhSobari

ULTRASONIC

Mar 3rd, 2017
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.47 KB | None | 0 0
  1. #define echo 12
  2. #define trig 13
  3. void setup() {
  4.   // put your setup code here, to run once:
  5. pinMode(trig, OUTPUT);
  6. pinMode(echo, INPUT);
  7. Serial.begin(9600);
  8. }
  9.  
  10. void loop() {
  11.   // put your main code here, to run repeatedly:
  12.  long dur, dis;
  13.   digitalWrite(trig, LOW);
  14.   delayMicroseconds(2);
  15.   digitalWrite(trig, HIGH);
  16.   delayMicroseconds(10);
  17.   digitalWrite(trig, LOW);
  18.   dur = pulseIn(echo, HIGH);
  19.   dis = (dur/2)/29.1;
  20.   Serial.println(dis);
  21.   delay(100);
  22. }
Add Comment
Please, Sign In to add comment