-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.ino
306 lines (284 loc) · 6.34 KB
/
project.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
294
295
296
297
298
299
300
301
302
303
304
305
306
/* Purpose: Useless box -- A machine that turns itself off after being turned on
* Author: Tyler Fretz
* Date: 12/12/2019
* Citation: Inspiration for arm/lid movement style from reddit user u/ahhTheSnorch. URL --> https://www.youtube.com/watch?time_continue=53&v=kproPsch7i0&feature=emb_logo
* link to u/ahhTheSnorch's code --> https://codebender.cc/sketch:273092#Useless_Machine_Final.ino
*/
#include <Servo.h>
Servo lidservo;
Servo armservo;
const int buzzer = 5;
const int knockSensor = A0;
const int switchpin = 4;
//max arm & lid servo positions
const int armClosed = 15;
const int armOpen = 147;
const int lidClosed = 128;
const int lidOpen = 90;
//changing variables
int action = 1;
int pos = 0;
int knockVal = 0;
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(switchpin,INPUT);
armservo.attach(3);
//starting position for arm
armservo.write(armClosed);
}
void loop() {
knockVal = analogRead(knockSensor); //get integer value from the lid knock sensor
if (digitalRead(switchpin) == LOW) //if the switch has been pushed, preform an action
{
if (action > 8) { //reset back to action #1 after whole cycle has been preformed
action = 1;
}
switch(action){
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
case 5:
action5();
break;
case 6:
action6();
break;
case 7:
action7();
break;
case 8:
action8();
break;
}
action += 1; //increment the actions
}
if (digitalRead(switchpin) == HIGH) //check for knock and respond only while switch has not been pushed
{
if (knockVal >= 225){
delay(1500);
knockBuzz();
delay(200);
}
}
}
//methods
void buzz(){ //buzzer tone that plays during action #8
tone(buzzer, 1700, 150);
delay(300);
tone(buzzer, 1700, 150);
delay(300);
tone(buzzer, 1700, 150);
delay(300);
tone(buzzer, 1700, 150);
delay(300);
tone(buzzer, 1700, 150);
delay(300);
tone(buzzer, 1700, 300);
delay(100);
tone(buzzer, 1700, 2000);
delay(200);
tone(buzzer, 1500, 1000);
delay(200);
tone(buzzer, 1900, 2000);
delay(200);
}
void knockBuzz(){ //buzzer tone that plays after a knock on lid
tone(buzzer, 1900, 200);
delay(300);
tone(buzzer, 1500, 200);
delay(300);
tone(buzzer, 1300, 200);
delay(100);
}
// methods for arm/lid movement based on number of switch flips
void action1() { //standard open close
lidservo.attach(2); //attaching lid during action 1 to avoid servo hum on start up
delay(100);
lidservo.write(lidOpen);
delay(100);
lidservo.detach(); //need to detach lid servo from pwm while it is under load to avoid servo hum
delay(1000);
armservo.write(armOpen);
delay(500);
armservo.write(armClosed);
delay(900);
lidservo.attach(2);
delay(100); //delay after reattach to ensure proper connection
lidservo.write(lidClosed);
delay(1000);
lidservo.detach();
}
void action2() { //quick open close
lidservo.attach(2);
delay(100);
lidservo.write(lidOpen);
delay(100);
armservo.write(armOpen);
delay(250);
armservo.write(armClosed);
delay(100);
lidservo.write(lidClosed);
delay(100);
lidservo.detach();
}
void action3() { //slow open
lidservo.attach(2);
delay(100);
for(pos = 128; pos > 90; pos -=1)
{
lidservo.write(pos);
delay(50);
}
lidservo.detach();
delay(100);
armservo.write(armOpen);
delay(500);
armservo.write(armClosed);
lidservo.attach(2);
delay(100);
lidservo.write(lidClosed);
delay(200);
lidservo.detach();
}
void action4() { //hold arm on switch
lidservo.attach(2);
delay(250);
lidservo.write(lidOpen);
delay(100);
lidservo.detach();
armservo.write(armOpen);
delay(2500);
for(pos = 147; pos > 15; pos -=2)
{
armservo.write(pos);
delay(50);
}
lidservo.attach(2);
delay(100);
lidservo.write(lidClosed);
delay(100);
lidservo.detach();
}
void action5() { //fast repeat open/close lid
lidservo.attach(2);
delay(100);
lidservo.write(lidOpen);
delay(500);
lidservo.write(lidClosed);
delay(500);
lidservo.write(lidOpen);
delay(500);
lidservo.write(lidClosed);
delay(500);
lidservo.write(lidOpen);
delay(500);
lidservo.write(lidClosed);
delay(2500);
lidservo.write(lidOpen);
delay(100);
armservo.write(armOpen);
delay(250);
armservo.write(armClosed);
delay(100);
lidservo.write(lidClosed);
delay(100);
lidservo.detach();
}
void action6() { //fast arm psyche out touch
lidservo.attach(2);
delay(100);
lidservo.write(lidOpen);
delay(100);
armservo.write(128);
delay(250);
armservo.write(armClosed);
delay(100);
lidservo.write(lidClosed);
delay(250);
lidservo.write(lidOpen);
delay(100);
armservo.write(128);
delay(250);
armservo.write(armClosed);
delay(100);
lidservo.write(lidClosed);
delay(250);
lidservo.write(lidOpen);
delay(100);
armservo.write(128);
delay(250);
armservo.write(armClosed);
delay(100);
lidservo.write(lidClosed);
delay(250);
lidservo.write(lidOpen);
delay(100);
armservo.write(armOpen);
delay(250);
armservo.write(armClosed);
delay(100);
lidservo.write(lidClosed);
delay(100);
lidservo.detach();
}
void action7() { //slow open, slow arm
lidservo.attach(2);
delay(100);
for(pos = 128; pos > 80; pos -=2)
{
lidservo.write(pos);
delay(50);
}
lidservo.detach();
for(pos = 15; pos < 81; pos +=2)
{
armservo.write(pos);
delay(50);
}
delay(500);
armservo.write(armOpen);
delay(250);
for(pos = 147; pos > 15; pos -=2)
{
armservo.write(pos);
delay(50);
}
lidservo.attach(2);
delay(100);
for(pos = 80; pos < 128; pos +=2)
{
lidservo.write(pos);
delay(50);
}
lidservo.detach();
}
void action8() { //vocal annoyed arm
lidservo.attach(2);
delay(100);
lidservo.write(lidOpen);
delay(200);
lidservo.detach();
armservo.write(105);
delay(300);
buzz();
delay(1000);
armservo.write(armClosed);
delay(1000);
armservo.write(armOpen);
delay(300);
armservo.write(armClosed);
lidservo.attach(2);
delay(100);
lidservo.write(lidClosed);
delay(200);
lidservo.detach();
}