-
Notifications
You must be signed in to change notification settings - Fork 0
/
fenrir.ino
352 lines (276 loc) · 7.59 KB
/
fenrir.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// FENRiR -- the giant flaming wolf.
// Contact: http://sheetmetalalchemist.com
// --------------------------------------------------------------------
// Note: Any switches higher than POOFERS are SHIFT keys.
#define SWITCHES 6
#define POOFERS 4
// Which relay corresponds to which poofer position:
#define MOUTH 0
#define TAIL 2
#define LEFT 1
#define RIGHT 3
// Joystick directions
#define J_FIRE 0
#define J_UP 1
#define J_DOWN 2
#define J_LEFT 3
#define J_RIGHT 4
// Pin numbers for each switch: count by two because Arduino Mega
// has headers that are two-wide.
int pSwitch[SWITCHES] = {40,42,44,46,48,50};
int pRingLED[SWITCHES] = {41,43,45,47,49,51};
int pRelay[SWITCHES] = {22,24,26,28,30,32};
int pJoystick[5] = {8,9,10,11,12};
// ---- code starts here ----------------------------------------------
// Our brand of relays are on when LOW, off when HIGH.
#define R_ON 0
#define R_OFF 1
// ringState stores the on/off status of the ring LEDs.
// switchState stores the pressed/unpressed status of the switches.
int ringState[SWITCHES];
int switchState[SWITCHES];
int poofSpinner = 0;
// timed effects blackout the LED rings; this variable is used to signal that
// they need to be re-lit after the effect.
boolean ringsNeedResetting = true;
/** Arduino one-time setup call */
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
// Setup each switch and LED:
for (int i=0; i<SWITCHES; i++) {
// setup variables for each switch & LED
pinMode(pRingLED[i], OUTPUT);
ringState[i] = HIGH;
switchState[i] = LOW;
// ensure relays are off at startup: (IMPORTANT! DO NOT CHANGE!)
digitalWrite(pRelay[i], R_OFF);
pinMode(pRelay[i], OUTPUT);
}
initializeRingLEDs();
}
void initializeRingLEDs() {
for (int j=0; j<2; j++) {
for (int i=0; i<SWITCHES; i++) {
digitalWrite(pRingLED[i], HIGH);
}
delay(100);
for (int i=0; i<SWITCHES; i++) {
digitalWrite(pRingLED[i], LOW);
}
delay(100);
}
delay(500);
for (int j=0; j<2; j++) {
for (int i=SWITCHES-1; i>=0; i--) {
digitalWrite(pRingLED[i], HIGH);
delay(25); // - j*5);
digitalWrite(pRingLED[i], LOW);
delay(75); // 150 - j*50);
}
}
delay(1000);
resetRings(HIGH);
}
// Main Arduino Event Loop
void loop() {
updateSwitchStatuses();
// do special stuff if "SHIFT" keys are held
if (processShiftKeys()==true) {
delay(100);
} else if (handleJoystick()==true) {
// don't do anything -- especially don't reset rings!
} else {
// otherwise, just pass the switch state to the poofers.
resetRings(HIGH);
for (int i=0; i<POOFERS; i++) {
digitalWrite(pRelay[i], switchState[i]==HIGH ? R_ON : R_OFF);
digitalWrite(pRingLED[i], 1-switchState[i]);
}
}
}
/** Atari Joysticks require pull-up HIGH value, and are LOW when activated. */
void setupJoystick() {
for (int i=0; i<5; i++) {
pinMode(pJoystick[i],INPUT);
digitalWrite(pJoystick[i],HIGH);
}
}
boolean handleJoystick() {
for (int j=0;j<5;j++) {
digitalWrite(pJoystick[j],HIGH);
}
// only do stuff if fire button is held down
if (digitalRead(pJoystick[J_FIRE])==HIGH) {
resetRings(HIGH);
return false;
}
if (digitalRead(pJoystick[J_LEFT])==LOW) {
ringsNeedResetting = true;
blackout();
poofSpinner++;
poofSpinner %= 4;
setPoofer(poofSpinner,R_ON);
delay(100);
setPoofer(poofSpinner,R_OFF);
delay(50);
return true;
} else if (digitalRead(pJoystick[J_RIGHT])==LOW) {
ringsNeedResetting = true;
blackout();
poofSpinner--;
if (poofSpinner<0) poofSpinner += POOFERS;
setPoofer(poofSpinner,R_ON);
delay(100);
setPoofer(poofSpinner,R_OFF);
delay(50);
return true;
} else if (digitalRead(pJoystick[J_UP])==LOW) {
ringsNeedResetting = true;
blackout();
setPoofer(MOUTH,R_ON);
delay(100);
setPoofer(MOUTH,R_OFF);
delay(50);
return true;
} else if (digitalRead(pJoystick[J_DOWN])==LOW) {
ringsNeedResetting = true;
blackout();
setPoofer(TAIL,R_OFF);
setPoofer(LEFT,R_ON);
setPoofer(RIGHT,R_ON);
delay(100);
setPoofer(TAIL,R_ON);
setPoofer(LEFT,R_OFF);
setPoofer(RIGHT,R_OFF);
delay(100);
setPoofer(TAIL,R_OFF);
return true;
}
}
/** Test each switch twice, to avoid debounce problems */
void updateSwitchStatuses() {
int val[SWITCHES], val2[SWITCHES];
// Get first reading for every switch
for (int s=0; s<SWITCHES; s++) {
val[s] = digitalRead(pSwitch[s]);
}
// A very brief but non-zero delay:
delay(3);
// Get second reading for every switch
for (int s=0; s<SWITCHES; s++) {
val2[s] = digitalRead(pSwitch[s]);
}
// If the readings are the same, switch is "settled". Save it.
for (int s=0; s<SWITCHES; s++) {
if (val[s]==val2[s]) {
switchState[s] = val[s];
}
}
}
/** Test the SHIFT keys and do something if they're being pressed.
* @return true if any SHIFT is pressed or false if no SHIFT is pressed. */
boolean processShiftKeys() {
// just return false if no shift key is pressed:
if (switchState[4]==LOW && switchState[5]==LOW) {
return false;
}
// Both SHIFTS pressed!
if (switchState[4]==HIGH && switchState[5]==HIGH) {
bothShiftsPressed();
return true;
}
// Just one:
if (switchState[4]==HIGH) {
shift1Pressed();
} else {
shift2Pressed();
}
return true;
}
void shift1Pressed() {
Serial.println("shift 1");
counterClockwise();
}
void shift2Pressed() {
Serial.println("shift 2");
clockwise();
}
void bothShiftsPressed() {
Serial.println("both shifts");
performMouthBackSides(1);
}
/** Set poofer *and* ring LED to specified state */
void setPoofer(int which, int state) {
digitalWrite(pRelay[which], state);
digitalWrite(pRingLED[which], state==R_ON ? HIGH : LOW);
}
/** Dim or light up all the rings, if necessary */
void resetRings(int state) {
if (state==LOW) {
for (int i=0; i<SWITCHES; i++) {
digitalWrite(pRingLED[i], LOW);
}
ringsNeedResetting = true;
} else if (ringsNeedResetting) {
delay(400);
for (int i=0; i<SWITCHES; i++) {
digitalWrite(pRingLED[i], HIGH);
delay(80);
}
ringsNeedResetting = false;
}
}
// ############## Fancy Effects ############
void clockwise() {
resetRings(LOW);
setPoofer(MOUTH,R_ON);
delay(200);
setPoofer(MOUTH,R_OFF);
setPoofer(RIGHT,R_ON);
delay(200);
setPoofer(RIGHT,R_OFF);
setPoofer(TAIL,R_ON);
delay(200);
setPoofer(TAIL,R_OFF);
setPoofer(LEFT,R_ON);
delay(200);
setPoofer(LEFT,R_OFF);
}
void counterClockwise() {
resetRings(LOW);
setPoofer(MOUTH,R_ON);
delay(200);
setPoofer(MOUTH,R_OFF);
setPoofer(LEFT,R_ON);
delay(200);
setPoofer(LEFT,R_OFF);
setPoofer(TAIL,R_ON);
delay(200);
setPoofer(TAIL,R_OFF);
setPoofer(RIGHT,R_ON);
delay(200);
setPoofer(RIGHT,R_OFF);
}
void performMouthBackSides(int reps) {
resetRings(LOW);
for (int z=0; z<reps; z++) {
setPoofer(MOUTH,R_ON);
delay(250);
setPoofer(MOUTH,R_OFF);
setPoofer(TAIL,R_ON);
delay(250);
setPoofer(TAIL,R_OFF);
setPoofer(LEFT,R_ON);
setPoofer(RIGHT,R_ON);
delay(400);
setPoofer(LEFT,R_OFF);
setPoofer(RIGHT,R_OFF);
delay(500);
}
}
void blackout() {
for (int i=0; i<POOFERS; i++) {
setPoofer(i, R_OFF);
}
}