-
Notifications
You must be signed in to change notification settings - Fork 2
/
rangefinder.ino
110 lines (86 loc) · 2.13 KB
/
rangefinder.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
#define echoPin 6 // Echo Pin
#define trigPin 5 // Trigger Pin
#define LEDPin 13 // Onboard LED
#define MY_XB_ADDRESS 0x2
#define BASE_XB_ADDRESS 0x1
#define RANGE_THRESHOLD 90
#include "MedianFilter.h"
#include "XBee.h"
MedianFilter filter = MedianFilter();
XBee xbee = XBee();
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
xbee.begin(Serial);
}
long timeOfLastHeartbeat = 0;
long timeOfLastRead = 0;
long timeOfLastCompute = 0;
typedef enum {NOTHINGINRANGE, SOMETHINGINRANGE} range_state_t;
range_state_t currentState = NOTHINGINRANGE;
void loop() {
long now = millis();
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// Serial.println("Got packet");
}
if (now > timeOfLastRead + 50) {
// Just read and store the latest datapoint
timeOfLastRead = now;
long distance = getDistance();
filter.addDataPoint(distance);
}
if (now > timeOfLastCompute + 200) {
timeOfLastCompute = now;
// Compute median, update state
long median = filter.getMedian();
if (currentState == NOTHINGINRANGE) {
if (median < RANGE_THRESHOLD) {
currentState = SOMETHINGINRANGE;
updateLED();
sendToBase(median);
}
} else if (currentState == SOMETHINGINRANGE) {
if (median > RANGE_THRESHOLD) {
currentState = NOTHINGINRANGE;
updateLED();
}
}
}
if (now > timeOfLastHeartbeat + 10000) {
// Send heartbeat
timeOfLastHeartbeat = now;
sendToBase(-1);
}
delay(1);
}
void updateLED() {
if (currentState == SOMETHINGINRANGE) {
digitalWrite(LEDPin, HIGH);
} else {
digitalWrite(LEDPin, LOW);
}
}
void sendToBase(uint8_t distance) {
uint8_t payload[] = {distance};
Tx16Request tx = Tx16Request(BASE_XB_ADDRESS, payload, sizeof(payload));
xbee.send(tx);
}
long getDistance() {
triggerPing();
return readDistance();
}
void triggerPing() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}
long readDistance() {
long duration = pulseIn(echoPin, HIGH);
long distance = duration / 58.2;
return distance;
}