Advertisement
KRITSADA

POP-XT with HC-SR04 Show CM and Inch on GLCD

Aug 27th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <popxt.h>
  2. #define CM 1      //Centimeter
  3. #define INC 0     //Inch
  4. #define TP 4      //Trig_pin
  5. #define EP 6      //Echo_pin  
  6. long Distance(long time, int flag)
  7. {
  8.           long distacne;
  9.           if(flag)
  10.              distacne = (time /29) / 2  ;     // Distance_CM  = ((Duration of high level)*(Sonic :340m/s))/2
  11.                                             // = ((Duration of high level)*(Sonic :0.034 cm/us))/2
  12.                                             // = ((Duration of high level)/(Sonic :29.4 cm/us))/2
  13.          else
  14.               distacne = (time / 74) / 2;      // INC
  15.           return distacne;
  16. }
  17.  
  18. long TP_init()
  19. {
  20.          digitalWrite(TP, LOW);
  21.          delayMicroseconds(2);
  22.          digitalWrite(TP, HIGH);                 // pull the Trig pin to high level for more than 10us impulse
  23.          delayMicroseconds(10);
  24.          digitalWrite(TP, LOW);
  25.          long microseconds = pulseIn(EP,HIGH);   // waits for the pin to go HIGH, and returns the length of the pulse in microseconds
  26.          return microseconds;                    // return microseconds
  27. }
  28. void setup()
  29. {
  30.              pinMode(TP,OUTPUT);       // set TP output pin for trigger
  31.              pinMode(EP,INPUT);        // set EP input pin for echo
  32.              glcdMode(3);
  33.              setTextSize(2);
  34.              glcd(0,0,"Hello");
  35. }
  36. void loop()
  37. {
  38.              long microseconds = TP_init();   // trigger and receive
  39.              glcd(1,1,"mSec= %d   ",microseconds);
  40.              long distacne_cm = Distance(microseconds, CM); // Calculating the distance
  41.              glcd(2,1,"Dist= %d CM  ",distacne_cm);
  42.              delay(300);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement