Skip to content

Commit c430672

Browse files
committed
Fix timeout in parking sensor that turns light off when parked
1 parent 2f1df52 commit c430672

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

libraries/MySensors/examples/ParkingSensor/ParkingSensor.ino

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
#define PANIC_DISTANCE 5 // Mix distance we red warning indication should be active (in cm)
5757
#define PARKED_DISTANCE 20 // Distance when "parked signal" should be sent to controller (in cm)
5858

59+
#define PARK_OFF_TIMEOUT 20000 // Number of milliseconds until turning off light when parked.
60+
5961
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
6062
// example for more information on possible values.
6163

@@ -67,10 +69,11 @@ NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and
6769
#define CHILD_ID 1
6870
MySensor gw;
6971
MyMessage msg(CHILD_ID,V_TRIPPED);
70-
int oldParkedStatus=-1;
72+
#endif
7173
unsigned long sendInterval = 5000; // Send park status at maximum every 5 second.
7274
unsigned long lastSend;
73-
#endif
75+
76+
int oldParkedStatus=-1;
7477

7578
unsigned long blinkInterval = 100; // blink interval (milliseconds)
7679
unsigned long lastBlinkPeriod;
@@ -109,56 +112,63 @@ void loop() {
109112
if (now-lastDebouncePeriod > distDebounce) {
110113
lastDebouncePeriod = now;
111114

112-
#ifdef SEND_STATUS_TO_CONTROLLER
113115
// Update parked status
114116
int parked = displayDist != 0 && displayDist<PARKED_DISTANCE;
115117
if (parked != oldParkedStatus && now-lastSend > sendInterval) {
116118
if (parked)
117119
Serial.println("Car Parked");
118120
else
119121
Serial.println("Car Gone");
122+
#ifdef SEND_STATUS_TO_CONTROLLER
120123
gw.send(msg.set(parked));
124+
#endif
121125
oldParkedStatus = parked;
122126
lastSend = now;
123127
}
124-
#endif
125-
126-
if (displayDist == 0) {
127-
// No reading from sensor, assume no object found
128-
numLightPixels--;
129-
} else {
130-
skipZero = 0;
131-
int newLightPixels = NUMPIXELS - (NUMPIXELS*(displayDist-PANIC_DISTANCE)/MAX_DISTANCE);
132-
if (newLightPixels>numLightPixels) {
133-
// Fast raise
134-
numLightPixels += max((newLightPixels - numLightPixels) / 2, 1);
135-
} else if (newLightPixels<numLightPixels) {
136-
// Slow decent
137-
numLightPixels--;
138-
}
139-
}
140128

141-
if (numLightPixels>=NUMPIXELS) {
142-
// Do some intense red blinking
143-
if (now-lastBlinkPeriod > blinkInterval) {
144-
blinkColor = !blinkColor;
145-
lastBlinkPeriod = now;
129+
if (parked && now-lastSend > PARK_OFF_TIMEOUT) {
130+
// We've been parked for a while now. Turn off all pixels
131+
for(int i=0;i<NUMPIXELS;i++){
132+
pixels.setPixelColor(i, pixels.Color(0,0,0));
146133
}
147-
for(int i=0;i<numLightPixels;i++){
148-
pixels.setPixelColor(i, pixels.Color(blinkColor?255*MAX_INTESITY/100:0,0,0));
149-
}
150134
} else {
151-
for(int i=0;i<numLightPixels;i++){
152-
int r = 255 * i/NUMPIXELS;
153-
int g = 255 - r;
154-
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
155-
pixels.setPixelColor(i, pixels.Color(r*MAX_INTESITY/100,g*MAX_INTESITY/100,0));
135+
if (displayDist == 0) {
136+
// No reading from sensor, assume no object found
137+
numLightPixels--;
138+
} else {
139+
skipZero = 0;
140+
int newLightPixels = NUMPIXELS - (NUMPIXELS*(displayDist-PANIC_DISTANCE)/MAX_DISTANCE);
141+
if (newLightPixels>numLightPixels) {
142+
// Fast raise
143+
numLightPixels += max((newLightPixels - numLightPixels) / 2, 1);
144+
} else if (newLightPixels<numLightPixels) {
145+
// Slow decent
146+
numLightPixels--;
147+
}
156148
}
157-
// Turn off the rest
158-
for(int i=numLightPixels;i<NUMPIXELS;i++){
159-
pixels.setPixelColor(i, pixels.Color(0,0,0));
149+
150+
if (numLightPixels>=NUMPIXELS) {
151+
// Do some intense red blinking
152+
if (now-lastBlinkPeriod > blinkInterval) {
153+
blinkColor = !blinkColor;
154+
lastBlinkPeriod = now;
155+
}
156+
for(int i=0;i<numLightPixels;i++){
157+
pixels.setPixelColor(i, pixels.Color(blinkColor?255*MAX_INTESITY/100:0,0,0));
158+
}
159+
} else {
160+
for(int i=0;i<numLightPixels;i++){
161+
int r = 255 * i/NUMPIXELS;
162+
int g = 255 - r;
163+
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
164+
pixels.setPixelColor(i, pixels.Color(r*MAX_INTESITY/100,g*MAX_INTESITY/100,0));
165+
}
166+
// Turn off the rest
167+
for(int i=numLightPixels;i<NUMPIXELS;i++){
168+
pixels.setPixelColor(i, pixels.Color(0,0,0));
169+
}
160170
}
161171
}
162172
pixels.show(); // This sends the updated pixel color to the hardware.
163173
}
164-
}
174+
}

0 commit comments

Comments
 (0)