-
Notifications
You must be signed in to change notification settings - Fork 0
/
3
58 lines (49 loc) · 1.19 KB
/
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// circuit
/*
Arduino SR04
5V --- VCC
A0 --- Trig
A1 --- Echo
GND --- GND
*/
// pin setting
#define TrigPin A0
// __|^|_____________
// 10us or more HITH SIGNAL will drive it work for one time
#define EchoPin A1
// ______|^^^^^^^^|__
// PULSE WIDTH stand for distance(the time of ultrasound transmit, both go and back)
// pulse width WILL NOT long than 38ms, it means timeout
// Distance = Speed x Time
// Speed of sound ~= 340m/s = 0.340mm/us
int count = 0;
long duration;
// PULSE WIDTH
void setup() {
// set Serial communication
Serial.begin(115200);
// set pin mode
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
// init pin
digitalWrite(TrigPin, LOW);
delay(1);
}
void loop() {
Serial.println(count++);
Serial.println(getDistance());
Serial.println("");
Serial.println("");
delay(1000);
}
long getDistance() {
// trig
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
// echo
duration = pulseIn(EchoPin, HIGH); // unit: us
return duration * 0.34029 / 2; // unit: mm
}