-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchoSensor.ino
100 lines (70 loc) · 1.84 KB
/
EchoSensor.ino
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
int trigger = 2;
int echo = 4;
void setup() {
Serial.begin(9600);
Serial.println("Echo Sensor!");
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(trigger, LOW);
digitalWrite(echo, LOW);
}
void loop() {
unsigned long startTime;
unsigned long triggerEndTime;
unsigned long triggerElapsedTime;
int echoStart;
int counter1;
unsigned long burstStartTime;
int echoStop;
int counter2;
unsigned long echoReceivedTime;
unsigned long echoElapsedTime;
float distance;
startTime = micros();
// Send the trigger
digitalWrite(trigger, HIGH);
do
{
triggerEndTime = micros();
triggerElapsedTime = triggerEndTime - startTime;
} while(triggerElapsedTime < 10);
digitalWrite(trigger, LOW);
// Wait for the burst to begin
counter1 = 0;
do
{
counter1++;
echoStart = digitalRead(echo);
burstStartTime = micros();
} while ((echoStart == 0) && (counter1 < 10000));
// Wait for the echo to return
counter2 = 0;
do
{
counter2++;
echoStop = digitalRead(echo);
echoReceivedTime = micros();
} while ((echoStop != 0) && (counter2 < 10000));
echoElapsedTime = echoReceivedTime - burstStartTime;
// Convert microseconds into centimeters using 343m/s for speed of sound
distance = echoElapsedTime/58.3090379;
Serial.print("startTime = ");
Serial.print(startTime);
// Serial.print(" echoStart = ");
// Serial.print(echoStart);
Serial.print(" c1 = ");
Serial.print(counter1);
Serial.print(" t1 = ");
Serial.print(burstStartTime);
// Serial.print(" echoStop = ");
// Serial.print(echoStop);
Serial.print(" c2 = ");
Serial.print(counter2);
Serial.print(" t2 = ");
Serial.print(echoReceivedTime);
Serial.print(" t3 = ");
Serial.print(echoElapsedTime);
Serial.print(" distance = ");
Serial.print(distance);
Serial.println();
}