forked from balassy/useless-box
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseful-box.ino
293 lines (253 loc) · 6.2 KB
/
useful-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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
20240605: this is origial from
https://github.com/balassy/useless-box.git
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
$ arduino-cli version
arduino-cli Version: 0.35.3 Commit: 95cfd654 Date: 2024-02-19T13:24:24Z
arduino-cli Version: 1.0.0 Commit: 05c9852a Date: 2024-06-12T14:13:32Z
Used library Version Path
Servo 1.0.2 /home/qchen/.arduino15/packages/esp8266/hardware/esp8266/3.1.2/libraries/Servo
Wire 1.0 /home/qchen/.arduino15/packages/esp8266/hardware/esp8266/3.1.2/libraries/Wire
Used platform Version Path
esp8266:esp8266 3.1.2 /home/qchen/.arduino15/packages/esp8266/hardware/esp8266/3.1.2
*/
// 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 == LOW);
if (isSwitchTurnedOn) {
led.turnOn();
run();
isLidOpen = false;
led.turnOff();
}
// Check the proximity sensor even if the switch is not turned on.
if (sensor.isInRange()) {
if (!isLidOpen && monitorSensor) {
openLidFast();
isLidOpen = true;
}
} else {
if (isLidOpen) {
closeLidFast();
isLidOpen = false;
}
}
// Update last switch state for the next iteration.
lastSwitchState = switchState;
// Wait 250 ms before next reading (required for the sensor).
delay(250);
}
void run() {
static int runCounter = 0; // Counter to track the number of runs
if (runCounter < 10) { // Follow pattern for the first 10 runs
switch (runCounter % 10) { // Use runCounter to determine the pattern
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;
}
} else { // After 10 runs, switch to random selection
int randomCase = random(10); // Generate a random number between 0 and 9
switch (randomCase) {
case 0:
runSlow();
break;
case 1:
runWaitThenFast();
break;
case 2:
runFast();
break;
case 3:
runFastThenClap();
monitorSensor = true;
break;
case 4:
runOpenCloseThenFast();
monitorSensor = false;
break;
case 5:
runPeekThenFast();
break;
case 6:
runFastWithDelay();
monitorSensor = true;
break;
case 7:
runClap();
monitorSensor = false;
break;
case 8:
runHalf();
break;
case 9:
// Add additional cases here as needed
break;
default:
break;
}
}
runCounter++; // Increment the run counter
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);
}