-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUltrasonic.cpp
More file actions
39 lines (35 loc) · 776 Bytes
/
Ultrasonic.cpp
File metadata and controls
39 lines (35 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Ultrasonic.cpp - Library for HC-SR04 Ultrasonic Ranging Module.library
//edited by Prasanjeet keshri & hacked by Satyam Kumar
Created by ITead studio. Apr 20, 2010.
iteadstudio.com
*/
#include "Arduino.h"
#include "Ultrasonic.h"
Ultrasonic::Ultrasonic(int TP, int EP)
{
pinMode(TP,OUTPUT);
pinMode(EP,INPUT);
Trig_pin=TP;
Echo_pin=EP;
}
long Ultrasonic::Timing()
{
digitalWrite(Trig_pin, LOW);
delayMicroseconds(30);
digitalWrite(Trig_pin, HIGH);
delayMicroseconds(60);
digitalWrite(Trig_pin, LOW);
duration = pulseIn(Echo_pin,HIGH);
return duration;
}
long Ultrasonic::Ranging(int sys)
{
Timing();
distacne_cm = duration /29 / 2 ;
distance_inc = duration / 74 / 2;
if (sys)
return distacne_cm;
else
return distance_inc;
}