forked from balassy/useless-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useless-box.ino
217 lines (183 loc) · 3.83 KB
/
useless-box.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Platform libraries.
#include <Arduino.h> // To add IntelliSense for platform constants.
// Third-party libraries.
// My classes.
#include "speed-servo.h"
#include "status-led.h"
#include "proximity-sensor.h"
#include "config.h" // To store configuration and secrets.
SpeedServo lidServo;
SpeedServo switchServo;
StatusLed led;
ProximitySensor sensor;
int lastSwitchState = 0;
long playCount = 0;
bool isLidOpen = false;
bool monitorSensor = false;
void setup() {
initSerial();
initServos();
initLed();
initSensor();
pinMode(PIN_SWITCH, INPUT);
Serial.printf("Application version: %s\n", APP_VERSION);
Serial.println("Setup completed.");
}
void initSerial() {
Serial.begin(115200);
Serial.println();
Serial.println("Initializing serial connection DONE.");
}
void initServos() {
lidServo.attach(PIN_LID_SERVO);
lidServo.moveNowTo(LID_START_POSITION);
switchServo.attach(PIN_SWITCH_SERVO);
switchServo.moveNowTo(SWITCH_START_POSITION);
}
void initLed() {
led.setPin(LED_BUILTIN);
led.turnOff();
}
void initSensor() {
sensor.attach(PIN_SENSOR_SDA, PIN_SENSOR_SCL, SENSOR_TRIGGER_THRESHOLD);
}
void loop() {
int switchState = digitalRead(PIN_SWITCH);
boolean isSwitchTurnedOn = (switchState != lastSwitchState) && (switchState == LOW);
if (isSwitchTurnedOn) {
led.turnOn();
run();
isLidOpen = false;
led.turnOff();
} else {
// Check the proximity sensor.
if (sensor.isInRange()) {
if (!isLidOpen && monitorSensor) {
openLidFast();
isLidOpen = true;
}
} else {
if (isLidOpen) {
closeLidFast();
isLidOpen = false;
}
}
}
lastSwitchState = switchState;
// Wait 250 ms before next reading (required for the sensor).
delay(250);
}
void run() {
switch (playCount % 10) {
case 0:
case 1:
runSlow();
break;
case 2:
runWaitThenFast();
break;
case 3:
runFast();
break;
case 4:
runFastThenClap();
monitorSensor = true;
break;
case 5:
runOpenCloseThenFast();
monitorSensor = false;
break;
case 6:
runPeekThenFast();
break;
case 7:
runFastWithDelay();
monitorSensor = true;
break;
case 8:
runClap();
monitorSensor = false;
break;
case 9:
runHalf();
break;
default:
break;
}
playCount++;
}
void runSlow() {
openLidSlow();
flipSwitchSlow();
closeLidSlow();
}
void runWaitThenFast() {
delay(5000);
flipSwitchFast();
}
void runFast() {
flipSwitchFast();
}
void runFastThenClap() {
flipSwitchFast();
clapLid();
}
void runOpenCloseThenFast() {
openLidSlow();
delay(2000);
closeLidSlow();
delay(2000);
flipSwitchFast();
}
void runPeekThenFast() {
switchServo.moveSlowTo(SWITCH_HALF_POSITION);
delay(3000);
switchServo.moveFastTo(SWITCH_START_POSITION);
delay(3000);
flipSwitchFast();
}
void runFastWithDelay() {
openLidSlow();
delay(4000);
flipSwitchFast();
closeLidFast();
}
void runClap() {
clapLid();
clapLid();
clapLid();
clapLid();
openLidFast();
flipSwitchFast();
closeLidFast();
}
void runHalf() {
switchServo.moveSlowTo(SWITCH_HALF_POSITION);
delay(3000);
switchServo.moveFastTo(SWITCH_END_POSITION);
switchServo.moveFastTo(SWITCH_START_POSITION);
}
void openLidSlow() {
lidServo.moveSlowTo(LID_END_POSITION);
}
void openLidFast() {
lidServo.moveFastTo(LID_END_POSITION);
}
void closeLidSlow() {
lidServo.moveSlowTo(LID_START_POSITION);
}
void closeLidFast() {
lidServo.moveFastTo(LID_START_POSITION);
}
void clapLid() {
openLidFast();
closeLidFast();
}
void flipSwitchSlow() {
switchServo.moveSlowTo(SWITCH_END_POSITION);
switchServo.moveSlowTo(SWITCH_START_POSITION);
}
void flipSwitchFast() {
switchServo.moveFastTo(SWITCH_END_POSITION);
switchServo.moveFastTo(SWITCH_START_POSITION);
}