-
Notifications
You must be signed in to change notification settings - Fork 9
/
ILTMS_lightsaber_v1.ino
369 lines (313 loc) · 9.37 KB
/
ILTMS_lightsaber_v1.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
/* Presented without warranty, guarantee and support.
* By Bob Clagett - I Like To Make Stuff
*
* This is the code to accompany the lightsaber project at http://www.iliketomakestuff.com/make-lightsaber/
* Expected hardware is listed in post (Teensy 3.2, Prop Shield, DotStar LEDs)
*
* This is functional v1 of this code. Lots more I want to add to it including crash detection/sounds.
* This code is dirty, bulky, poorly documented and ugly but I finally got it all functional.
* Cleanup to come later in v2.
*/
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioPlaySerialflashRaw playFlashRaw1; //xy=149,388
AudioPlaySerialflashRaw playHUMraw; //xy=149,388
AudioPlaySerialflashRaw playSwingRaw; //xy=149,388
AudioMixer4 mixer1; //xy=445,386
AudioOutputAnalog dac1; //xy=591,379
AudioConnection patchCord4(playSwingRaw, 0, mixer1, 2);
AudioConnection patchCord1(playFlashRaw1, 0, mixer1, 1);
AudioConnection patchCord3(playHUMraw, 0, mixer1, 0);
AudioConnection patchCord2(mixer1, dac1);
// GUItool: end automatically generated code
#include <Adafruit_DotStar.h>
#include <NXPMotionSense.h>
NXPMotionSense imu;
NXPSensorFusion filter;
int motionThreshold = 2;
float lastPitch = 0, lastRoll = 0, lastHeading = 0;
int buttonState = HIGH;
int bladeState = 0; //0 off, 1, fading up, 2 on, 3 fading down
int fadeStep = 0;
int fadeStepSize = 6;
int selectedColor = 0;
int lastSelectedColor = 0;
bool isAnimating = 0;
bool bladeOn = 0;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50;
int lastButtonState = HIGH;
int pendingPress = 0;
//BC = Blade Color
int BCbuttonState = HIGH;
long BClastDebounceTime = 0; // the last time the output pin was toggled
long BCdebounceDelay = 50;
int BClastButtonState = HIGH;
int BCpendingPress = 0;
int pendingColorPress = 0;
#define COLOR_BUTTON_PIN 1
#define POWER_BUTTON_PIN 0
#define COLOR_ORDER BGR
#define CHIPSET APA102
#define NUMPIXELS 252
#define BRIGHTNESS 100
#define FRAMES_PER_SECOND 70
#define PROP_AMP_ENABLE 5
#define FLASH_CHIP_SELECT 6
#define LED_BUFFER_SELECT 7
#define VOLUME_POT 15
#define LED_PIN 7
Adafruit_DotStar strip = Adafruit_DotStar(
NUMPIXELS, DOTSTAR_BRG);
int outputValue = 0;
int presetColors[] = {0x0000FF,0x00FF00,0xFF0000,0xFFFFFF};
int totalPresetColors = (sizeof(presetColors)/sizeof(int));
int selectedColorIndex = 0;
int swingSounds = 4;
int lastSwingSound = swingSounds;
int clashSounds = 0;
void setup() {
delay(500); // sanity delay
imu.begin();
filter.begin(100);
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000L)
clock_prescale_set(clock_div_1); // Enable 16 MHz on Trinket
#endif
strip.begin(); // Initialize pins for output
strip.show(); // Turn all LEDs off ASAP
pinMode(POWER_BUTTON_PIN, INPUT);
pinMode(COLOR_BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(POWER_BUTTON_PIN, HIGH);
digitalWrite(COLOR_BUTTON_PIN, HIGH);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
// wait up to 3 seconds for the Serial device to become available
long unsigned debug_start = millis ();
while (!Serial && ((millis () - debug_start) <= 3000))
;
Serial.println ("Start prop shield RAW player");
SPI.begin();
// Enable the amplifier on the prop shield
pinMode(PROP_AMP_ENABLE, OUTPUT);
digitalWrite(PROP_AMP_ENABLE, HIGH);
pinMode(LED_BUFFER_SELECT, OUTPUT);
digitalWrite(LED_BUFFER_SELECT, HIGH);
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(8);
// Set initial volume
mixer1.gain(1, 1.5f); //other sounds
mixer1.gain(0, 0.8f); //hum
// Start SerialFlash
if (!SerialFlash.begin(FLASH_CHIP_SELECT)) {
while (1)
{
Serial.println ("Cannot access SPI Flash chip");
delay (3000);
}
}
triggerSound("saberswing1.raw");
}
void powerUpBlade(){
isAnimating = 1;
bladeState = 1;
Serial.println("Turn on blade");
bladeOn = 1;
triggerSound("saberon.raw");
}
void powerDownBlade(){
//animate DOWN
triggerSound("saberoff.raw");
bladeState = 3;
fadeStep = NUMPIXELS;
isAnimating = 1;
Serial.println("Turn off blade");
stopHum();
bladeOn = 0;
}
void bladeIsOn(){
if(!playHUMraw.isPlaying()){
startHum();
}
//nothing needed here unless adding animation
}
void bladeIsAnimatingUp(){
//Serial.println("bladeIsAnimatingUp");
int midpoint = NUMPIXELS/2;
int newSection = fadeStep+fadeStepSize;
for( int j = fadeStep; j < newSection; j++) {
strip.setPixelColor(j, selectedColor);
strip.setPixelColor(NUMPIXELS-j, selectedColor);
}
Serial.println(newSection);
fadeStep = newSection;
if (fadeStep >= midpoint+fadeStepSize){
fadeStep = NUMPIXELS;
isAnimating=0;
bladeState = 2;
// delay(200);
Serial.println("blade up complete");
startHum();
}
}
void bladeIsAnimatingDown(){
//Serial.println("bladeIsAnimatingDown");
int midpoint = NUMPIXELS/2;
int newSection = fadeStep-fadeStepSize;
for( int j = fadeStep; j > newSection; j--) {
strip.setPixelColor(j-midpoint, 0x000000);
strip.setPixelColor(midpoint+NUMPIXELS-j, 0x000000);
}
//Serial.println(fadeStep);
fadeStep = newSection;
if (fadeStep <=midpoint-fadeStepSize){
fadeStep = 0;
isAnimating=0;
bladeState = 0;
}
}
void detectMotion() {
float ax, ay, az;
float gx, gy, gz;
float mx, my, mz;
float roll, pitch, heading;
if (imu.available()) {
// Read the motion sensors
imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
// Update the SensorFusion filter
filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
// does it need a time threshold too?
float headingDiff = abs(lastHeading - heading);
float pitchDiff = abs(lastPitch - pitch);
if(lastHeading != 0){
if(pitchDiff > motionThreshold || headingDiff > motionThreshold){
//cyle through swing sounds
lastSwingSound++;
if(lastSwingSound>swingSounds){
lastSwingSound=1;
}
String swingFile = "saberswingX.raw";
swingFile.replace("X",lastSwingSound);
char charBuf[50];
swingFile.toCharArray(charBuf, 50);
triggerSwing(charBuf); // needs sequence to iterate through
}
}
lastHeading = heading;
lastPitch = pitch;
}
}
void loop(){
// Add entropy to random number generator; we use a lot of it.
// random16_add_entropy( random() );
//handle color selector button
int BCreading = digitalRead(COLOR_BUTTON_PIN);
if (BCreading != BClastButtonState) {
// reset the debouncing timer
BClastDebounceTime = millis();
}
if ((millis() - BClastDebounceTime) > BCdebounceDelay) {
if (BCreading != BCbuttonState) {
BCbuttonState = BCreading;
if(BCbuttonState == HIGH){
nextColor();
}
}
}
BClastButtonState = BCreading;
selectedColor = presetColors[selectedColorIndex];
if(selectedColor != lastSelectedColor && bladeState != 0){
Serial.print("COLOR CHANGE");
Serial.println(selectedColorIndex);
for( int j = 0; j < NUMPIXELS; j++) {
strip.setPixelColor(j,selectedColor);
}
}
lastSelectedColor = selectedColor;
//handle blade on/off
int reading = digitalRead(POWER_BUTTON_PIN);
//debounce
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
}
}
lastButtonState = reading;
if( pendingPress == 1 && buttonState != LOW){
buttonState = HIGH;
pendingPress = 0;
Serial.print("execute pending blade press");
if(bladeOn){
powerDownBlade();
} else {
powerUpBlade();
}
}
if (buttonState == LOW && !isAnimating) {
Serial.println("BLADE BUTTON IS PRESSED");
pendingPress = 1;
}
switch(bladeState){
case 0:
//blade is off
break;
case 1:
//blade is animating up
bladeIsAnimatingUp();
break;
case 2:
//blade is on
bladeIsOn();
break;
case 3:
//blade is animating down
bladeIsAnimatingDown();
break;
}
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
digitalWrite(LED_PIN, HIGH); // enable access to LEDs
strip.show(); // Refresh strip
digitalWrite(LED_PIN, LOW);
SPI.endTransaction(); // allow other libs to use SPI again
delay(10);
if(bladeState == 2){
detectMotion();
}
}
void nextColor(){
selectedColorIndex++;
if(selectedColorIndex >= totalPresetColors){
selectedColorIndex = 0;
}
}
void triggerSound(const char *filename){
playFlashRaw1.play(filename);
}
void triggerSwing(const char *filename){
if(playSwingRaw.isPlaying()==0){
playSwingRaw.play(filename);
} else {
Serial.println("already swinging");
}
}
void startHum(){
//Serial.println("startHum");
playHUMraw.play("HUM2.RAW");
Serial.println(playHUMraw.isPlaying());
}
void stopHum(){
playHUMraw.stop();
}