Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <popxt.h>
- #define CM 1 //Centimeter
- #define INC 0 //Inch
- #define TP 4 //Trig_pin
- #define EP 6 //Echo_pin
- long Distance(long time, int flag)
- {
- long distacne;
- if(flag)
- distacne = (time /29) / 2 ; // Distance_CM = ((Duration of high level)*(Sonic :340m/s))/2
- // = ((Duration of high level)*(Sonic :0.034 cm/us))/2
- // = ((Duration of high level)/(Sonic :29.4 cm/us))/2
- else
- distacne = (time / 74) / 2; // INC
- return distacne;
- }
- long TP_init()
- {
- digitalWrite(TP, LOW);
- delayMicroseconds(2);
- digitalWrite(TP, HIGH); // pull the Trig pin to high level for more than 10us impulse
- delayMicroseconds(10);
- digitalWrite(TP, LOW);
- long microseconds = pulseIn(EP,HIGH); // waits for the pin to go HIGH, and returns the length of the pulse in microseconds
- return microseconds; // return microseconds
- }
- void setup()
- {
- pinMode(TP,OUTPUT); // set TP output pin for trigger
- pinMode(EP,INPUT); // set EP input pin for echo
- glcdMode(3);
- setTextSize(2);
- glcd(0,0,"Hello");
- }
- void loop()
- {
- long microseconds = TP_init(); // trigger and receive
- glcd(1,1,"mSec= %d ",microseconds);
- long distacne_cm = Distance(microseconds, CM); // Calculating the distance
- glcd(2,1,"Dist= %d CM ",distacne_cm);
- delay(300);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement