-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_example_2.ino
124 lines (111 loc) · 3.64 KB
/
08_example_2.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Arduino pin assignment
#define PIN_LED 9
#define PIN_TRIG 12
#define PIN_ECHO 13
// configurable parameters
#define SND_VEL 346.0 // sound velocity at 24 celsius degree (unit: m/s)
#define INTERVAL 25 // sampling interval (unit: ms)
#define _DIST_MIN 100 // minimum distance to be measured (unit: mm)
#define _DIST_MAX 300 // maximum distance to be measured (unit: mm)
// global variables
float timeout; // unit: us
float dist_min, dist_max, dist_raw; // unit: mm
float last_raw;
unsigned long last_sampling_time; // unit: ms
float scale; // used for pulse duration to distance conversion
void setup() {
// initialize GPIO pins
pinMode(PIN_LED,OUTPUT);
pinMode(PIN_TRIG,OUTPUT);
digitalWrite(PIN_TRIG, LOW);
pinMode(PIN_ECHO,INPUT);
// initialize USS related variables
dist_min = _DIST_MIN;
dist_max = _DIST_MAX;
timeout = (INTERVAL / 2) * 1000.0; // precalculate pulseIn() timeout value. (unit: us)
dist_raw = 0.0; // raw distance output from USS (unit: mm)
last_raw = 0.0;
scale = 0.001 * 0.5 * SND_VEL;
// initialize serial port
Serial.begin(57600);
// initialize last sampling time
last_sampling_time = 0;
}
void loop() {
// wait until next sampling time.
// millis() returns the number of milliseconds since the program started. Will overflow after 50 days.
if(millis() < last_sampling_time + INTERVAL) return;
// get a distance reading from the USS
dist_raw = USS_measure(PIN_TRIG,PIN_ECHO);
// output the read value to the serial port
Serial.print("Min:0,");
Serial.print("raw:");
// turn on the LED if the distance is between dist_min and dist_max
if (dist_raw < dist_min || dist_raw > dist_max) {
dist_raw = last_raw;
Serial.print(dist_raw);
analogWrite(PIN_LED, LED_light(dist_raw));
}
else {
Serial.print(dist_raw);
analogWrite(PIN_LED, LED_light(dist_raw));
}
Serial.print(",");
Serial.println("Max:400");
last_raw = dist_raw;
// update last sampling time
last_sampling_time += INTERVAL;
}
// get a distance reading from USS. return value is in millimeter.
float USS_measure(int TRIG, int ECHO)
{
float reading;
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
reading = pulseIn(ECHO, HIGH, timeout) * scale; // unit: mm
if(reading < dist_min || reading > dist_max) reading = 0.0; // return 0 when out of range.
return reading;
// Pulse duration to distance conversion example (target distance = 17.3m)
// - round trip distance: 34.6m
// - expected pulse duration: 0.1 sec, or 100,000us
// - pulseIn(ECHO, HIGH, timeout) * 0.001 * 0.5 * SND_VEL
// = 100,000 micro*sec * 0.001 milli/micro * 0.5 * 346 meter/sec
// = 100,000 * 0.001 * 0.5 * 346 * micro * sec * milli * meter
// ----------------------------
// micro * sec
// = 100 * 173 milli*meter = 17,300 mm = 17.3m
// pulseIn() returns microseconds.
}
float LED_light(float dist_raw)
{
int light;
if ((dist_raw < 210)&&(dist_raw > 190)) {
light = 0;
}
else if ((dist_raw < 160)&&(dist_raw > 140)) {
light = 127;
}
else if ((dist_raw < 260)&&(dist_raw > 240)) {
light = 127;
}
else if ((dist_raw < 120)&&(dist_raw > 100)) {
light = 250;
}
else if ((dist_raw < 300)&&(dist_raw > 280)) {
light = 250;
}
else if (dist_raw == 0) {
light = 255;
}
else if ((dist_raw < 140)&&(dist_raw > 120)) {
light = 200;
}
else if ((dist_raw < 280)&&(dist_raw > 260)) {
light = 200;
}
else {
light = 60;
}
return light;
}