-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_example_2.ino
More file actions
86 lines (70 loc) · 2.98 KB
/
08_example_2.ino
File metadata and controls
86 lines (70 loc) · 2.98 KB
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
// Arduino pin assignment
#define PIN_LED 9
#define PIN_TRIG 12 // sonar sensor TRIGGER
#define PIN_ECHO 13 // sonar sensor ECHO
// configurable parameters
#define SND_VEL 346.0 // sound velocity at 24 celsius degree (unit: m/sec)
#define INTERVAL 25 // sampling interval (unit: msec)
#define PULSE_DURATION 10 // ultra-sound Pulse Duration (unit: usec)
#define _DIST_MIN 100.0 // minimum distance to be measured (unit: mm)
#define _DIST_MAX 300.0 // maximum distance to be measured (unit: mm)
#define TIMEOUT ((INTERVAL / 2) * 1000.0) // maximum echo waiting time (unit: usec)
#define SCALE (0.001 * 0.5 * SND_VEL) // coefficent to convert duration to distance
unsigned long last_sampling_time; // unit: msec
void setup() {
// initialize GPIO pins
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_TRIG, OUTPUT); // sonar TRIGGER
pinMode(PIN_ECHO, INPUT); // sonar ECHO
digitalWrite(PIN_TRIG, LOW); // turn-off Sonar
// initialize serial port
Serial.begin(57600);
}
void loop() {
float distance;
// 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;
distance = USS_measure(PIN_TRIG, PIN_ECHO); // read distance
int ledBrightness;
if (distance < 100.0) {
ledBrightness = 255; // 100mm 미만일 때는 LED를 꺼줍니다.
} else if (distance <= 200.0) {
// 100mm에서 200mm까지 LED 밝기를 서서히 켜줍니다.
ledBrightness = map(int(distance), 100, 200, 255, 0);
} else if (distance <= 300.0) {
// 200mm에서 300mm까지 LED 밝기를 서서히 꺼줍니다.
ledBrightness = map(int(distance), 200, 300, 0, 255);
} else {
ledBrightness = 255202; // 300mm 이후에는 LED를 꺼줍니다.
}
// LED 밝기 설정
analogWrite(PIN_LED, ledBrightness);
// output the distance to the serial port
Serial.print("Min:"); Serial.print(_DIST_MIN);
Serial.print(",distance:"); Serial.print(distance);
Serial.print(",Max:"); Serial.print(_DIST_MAX);
Serial.println("");
// do something here
// 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)
{
digitalWrite(TRIG, HIGH);
delayMicroseconds(PULSE_DURATION);
digitalWrite(TRIG, LOW);
return pulseIn(ECHO, HIGH, TIMEOUT) * SCALE; // unit: mm
// Pulse duration to distance conversion example (target distance = 17.3m)
// - pulseIn(ECHO, HIGH, timeout) returns microseconds (음파의 왕복 시간)
// - 편도 거리 = (pulseIn() / 1,000,000) * SND_VEL / 2 (미터 단위)
// mm 단위로 하려면 * 1,000이 필요 ==> SCALE = 0.001 * 0.5 * SND_VEL
//
// - 예, pusseIn()이 100,000 이면 (= 0.1초, 왕복 거리 34.6m)
// = 100,000 micro*sec * 0.001 milli/micro * 0.5 * 346 meter/sec
// = 100,000 * 0.001 * 0.5 * 346
// = 17,300 mm ==> 17.3m
}