Skip to content

Commit e83ee80

Browse files
committed
chore: PLAY_CONFIRMATIONS to avoid sporadic triggers
1 parent efa72e2 commit e83ee80

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

xmas-tree/xmas-tree.ino

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
#define ECHO_PIN A0
1313
#define TRIG_PIN A1
1414

15-
#define PLAY_DISTANCE 30
15+
#define PLAY_DISTANCE 75
1616
#define MODE_LOOPS 3
1717
#define LOOP_DELAY 50
18+
#define PLAY_CONFIRMATIONS 10
1819
#define PLAY_COOLDOWN_DELAY 10000
1920

2021
/*
@@ -47,6 +48,7 @@ int _modeLoop = 0;
4748
bool _loopFinished = false;
4849
int _activeLed = -1;
4950
int _activeLedPair = -1;
51+
int _playConfirmation = 0;
5052

5153
void setup() {
5254
for (int l = 0; l < LED_PIN_NUM; l++) {
@@ -149,13 +151,32 @@ int nextMode() {
149151
return SINGLE;
150152
}
151153

154+
bool validatePlayConfirmation() {
155+
if (_playConfirmation < PLAY_CONFIRMATIONS) {
156+
_playConfirmation++;
157+
return false;
158+
}
159+
160+
_playConfirmation = 0;
161+
return true;
162+
}
163+
164+
void discardPlayConfirmation() {
165+
_playConfirmation = 0;
166+
}
167+
152168
void loop() {
153169
int distance = calculateDistance();
154170

155171
if (distance < PLAY_DISTANCE and distance > 5) {
156-
mode = X_PLAY;
172+
if (validatePlayConfirmation()) {
173+
mode = X_PLAY;
174+
} else {
175+
mode = nextMode();
176+
}
157177
} else {
158178
mode = nextMode();
179+
discardPlayConfirmation();
159180
}
160181

161182
switch (mode) {

0 commit comments

Comments
 (0)