-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eye_Of_Agamotto.ino
402 lines (331 loc) · 12.3 KB
/
Eye_Of_Agamotto.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
MIT License
Copyright (c) 2022 Crash Works 3D
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
DESCRIPTION
====================
The purpose of this code is to automate the servo and pixels for the Eye of Agamotto
ORIGINAL CREATOR
====================
MSquared94
3D Model: https://www.thingiverse.com/thing:4970358
DEVELOPED BY
====================
Crash Works 3D: Cranshark, Dropwire
Link Tree: https://linktr.ee/crashworks3d
WORKING DEMO
====================
https://www.youtube.com/watch?v=XaKzuAUb2fQ
*/
// Version. Don't change unless authorized by Cranshark
#define VERSION "0.0.1.1"
// Referenced libraries
// For installation instructions see https://github.com/adafruit/Adafruit_SoftServo
#include "Adafruit_SoftServo.h"
// For installation instructions see: https://github.com/mathertel/OneButton
#include <OneButton.h>
// For installation instructions see https://github.com/cpldcpu/light_ws2812
#include "WS2812.h"
#ifndef __AVR_ATtiny85__
#error "Code designed for ATTiny85 chipset only."
#endif
#define SERVO_PIN 0 // Signal pin driving the servo
#define PIXELS_PIN 1 // Signal pin controlling the pixels
#define BUTTON_PIN 2 // Pin for switch
#define OPEN_POS 180 // Degrees (0-180) of the servo in the open position
#define CLOSE_POS 1 // Degrees (0-180) of the servo in the closed position
// Declare values for servo control
#define SERVO_CLOSE_SPEED 3000 // set the speed of the servo close function (0 = Max Speed / 10000 = Slowest Speed)
#define SERVO_OPEN_SPEED 3000 // set the speed of the servo opening recommend set to max speed to aid in lift (0 = Max Speed / 10000 = Slowest Speed)
// Declare values for pixel control
#define PIXEL_NUM 7 // Number of bits/LEDs in pixel (default is NeoPixel Jewel)
#define PIXEL_INCREMENT 1 // Number to increase/decrease RGB value when changing color
#define PIXEL_DELAY 20 // Time in milli-seconds between changing pixel color
#define PIXEL_OFF (cRGB){ 0, 0, 0 } // RGB value (black) for "off"
#define PIXEL_COLOR_1 (cRGB){ 0, 100, 0 } // RGB value for first color (default green)
#define PIXEL_COLOR_2 (cRGB){ 100, 0, 0 } // RGB value for second color (red)
#define PIXEL_COLOR_3 (cRGB){ 0, 0, 100 } // RGB value for third color (blue)
#define PIXEL_COLOR_4 (cRGB){ 100, 100, 100 } // RGB value for fourth color (white)
cRGB curColor = PIXEL_COLOR_1; // Keeps track of the current pixels color
cRGB targetColor = PIXEL_OFF; // Keeps track of the target pixels color
int curColorState = 1; // Keeps track of the current state of the pixels color; 0 = Off
int prevColorState = 1; // Keeps track of the previous state of the pixels color
long curMillis = millis(); // Keeps track of the current number of milli-seconds that have passed since the code started running
bool isOpen = 0; // Keeps track whether or not the lid is open or closed
// Create object to control servo
Adafruit_SoftServo servo;
// Create object for WS2812 pixel LEDs
WS2812 pixels(PIXEL_NUM);
// Create object for primary button to handle
OneButton primaryButton = OneButton(BUTTON_PIN, true, true);
/**
* Helper Method
* Simulate a delay (milli-seconds) in processing without disabling the processor completely
*
* @param[out] period - the amount of time in milliseconds to delay
*
* See: https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/
*/
void simDelayMillis(unsigned long period){
unsigned long delayMillis = millis() + period;
while (millis() <= delayMillis)
{
byte x = 0; // dummy variable, does nothing
}
}
/**
* @brief Helper Method
* Simulate a delay (micro-seconds) in processing without disabling the processor completely
*
* @param period
*/
void simDelayMicros(unsigned long period){
unsigned long delayMicros = micros() + period;
while (micros() <= delayMicros)
{
byte x = 0;
}
}
/**
* @brief Initializes the primary button
*
*/
void initPrimaryButton(){
primaryButton.attachClick(handlePrimaryButtonSingleTap);
primaryButton.attachDoubleClick(handlePrimaryButtonDoubleTap);
primaryButton.attachLongPressStart(handlePrimaryButtonDoubleTap);
}
/**
* @brief Initializes the WS2812 LED pixels
*
*/
void initPixels(){
pixels.setOutput(PIXELS_PIN);
pixels.setColorOrderRGB();
}
/**
* @brief Method to activate the servo and move the eye lid to the open position
*
*/
void openEye(){
servo.attach(SERVO_PIN); // Attaches the servo to the pin for use
// Instead of immediately moving to the target position, use a
// loop function with a delay to control the speed of movement
for(int i = CLOSE_POS; i <= OPEN_POS; i++){
servo.write(i); // Send position value signal to servo
simDelayMicros(SERVO_OPEN_SPEED); // Controls the speed at which the servo moves to the next position
}
simDelayMillis(500); // Allow enough time for the servo to complete movement
servo.detach(); // Stop sending signals to the servo and save power
}
/**
* @brief Method to activate the servo and move the eye lid to the closed position
*
*/
void closeEye(){
servo.attach(SERVO_PIN); // Attaches the servo to the pin for use
// Instead of immediately moving to the target position, use a
// loop function with a delay to control the speed of movement
for(int i = OPEN_POS; i >= CLOSE_POS; i--){
servo.write(i); // Send position value signal to servo
simDelayMicros(SERVO_OPEN_SPEED); // Controls the speed at which the servo moves to the next position
}
simDelayMillis(500); // Allow enough time for the servo to complete movement
servo.detach(); // Stop sending signals to the servo and save power
}
/**
* @brief Sets the target color of the pixels object
*
*/
void setPixels(){
switch (curColorState){
case 0:
targetColor = PIXEL_OFF;
break;
case 1:
targetColor = PIXEL_COLOR_1;
break;
case 2:
targetColor = PIXEL_COLOR_2;
break;
case 3:
targetColor = PIXEL_COLOR_3;
break;
case 4:
targetColor = PIXEL_COLOR_4;
break;
default:
break;
}
}
/**
* @brief Turns the pixels on
*
*/
void pixelsOn(){
// Checks if the pixels were off and sets to the previous color
if(curColorState == 0){
curColorState = prevColorState;
}
setPixels(); // Calls the method to set the target pixels value
}
/**
* @brief Turns the pixels off
*
*/
void pixelsOff(){
prevColorState = curColorState; // Stores the previous color state
curColorState = 0; // Sets the current color state to "off"
setPixels(); // Calls the method to set the target pixels value
}
/**
* Method to turn the NEO LEDs on
*/
void setPixels(int r, int g, int b){
cRGB color; // Required color object to pass to pixels object
color.r = r; color.g = g; color.b = b; // Sets the RGB values of the color object
// Loop through the bits/pixels
for(uint8_t i = 0; i < PIXEL_NUM; i++){
pixels.set_crgb_at(i, color); // Set the color value of each bit/pixel
}
pixels.sync(); // Activate the bits/pixels in the array of pixels
}
/**
* @brief Combine functions to create special effects when opening the eye
*
*/
void openEyeFx(){
isOpen = true; // Set that the eye is open
pixelsOn(); // Turn on the pixels
openEye(); // Move the servo to the open position
}
/**
* @brief Combine functions to create special effects when closing the eye
*
*/
void closeEyeFx(){
isOpen = false; // Set that the eye is closed
closeEye(); // Move the servo to the closed positon
pixelsOff(); // Turn off the pixels
}
/**
* @brief Handles what to do when the switch is activated once
*
*/
void handlePrimaryButtonSingleTap(){
if(isOpen){
closeEyeFx(); // If the eye is open, run the close special effects
} else {
openEyeFx(); // If the eye is closed, run the open special effects
}
}
/**
* @brief Handles what to do when the switch is pressed twice in rapid sequence
*
*/
void handlePrimaryButtonDoubleTap(){
if(!isOpen){ // Don't do anything, the pixels should be off
return; // Exits the method without running any code below this line
}
prevColorState = curColorState; // Keep track of the color state
// Drop through the color state and set to the next state
switch (curColorState){
case 0:
curColorState = 1;
break;
case 1:
curColorState = 2;
break;
case 2:
curColorState = 3;
break;
case 3:
curColorState = 4;
break;
case 4:
default:
curColorState = 1;
break;
}
pixelsOn(); // Turn on the pixels
}
/**
* @brief Handles what to do when the button is pressed for a long time
*
*/
void handlePrimaryButtonLongPress(){
}
/**
* @brief Monitors the state/color of the pixels asynchronously
* Other code can be running and the pixels can change color at the same time ;D
*/
void monitorPixels(){
if((millis() - curMillis) > PIXEL_DELAY){ // When the timer interval is met, run this code
curMillis = millis(); // Reset the timer
// Begin transistioning the red color to the target red color
if (curColor.r < targetColor.r){
curColor.r = curColor.r + PIXEL_INCREMENT;
} else if (curColor.r > targetColor.r) {
curColor.r = curColor.r - PIXEL_INCREMENT;
}
// Begin transitioning the green color to the target green color
if (curColor.g < targetColor.g){
curColor.g = curColor.g + PIXEL_INCREMENT;
} else if (curColor.g > targetColor.g){
curColor.g = curColor.g - PIXEL_INCREMENT;
}
// Begin transitioning the blue color to the target blue color
if (curColor.b < targetColor.b){
curColor.b = curColor.b + PIXEL_INCREMENT;
} else if (curColor.b > targetColor.b){
curColor.b = curColor.b + PIXEL_INCREMENT;
}
setPixels(curColor.r, curColor.g, curColor.b); // Send the latest RGB colors to the pixels
}
}
/**
* @brief Code that runs when the board first powers on
*
*/
void setup(){
// Turn on the timer register
OCR0A = 0x00; // any number is OK (Configuration for ATtiny85)
TIMSK |= _BV(TOIE0); // (Configuration for ATtiny85)
initPrimaryButton(); // Initialize the switch
initPixels(); // Initialize the pixels
closeEyeFx(); // Start by having the eye closed and the pixels off
}
/**
* @brief Code that runs continuously on the board
*
*/
void loop(){
primaryButton.tick(); // Monitors for button presses
monitorPixels(); // Monitors pixels state
}
// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
volatile uint8_t counter = 0;
ISR(TIMER0_OVF_vect) {
// this gets called every 2 milliseconds
counter += 2;
// every 20 milliseconds, refresh the servos!
if (counter >= 20) {
counter = 0;
servo.refresh();
}
}