-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpixelstick.ino
182 lines (162 loc) · 4.03 KB
/
pixelstick.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
"""
Written by Lucas Berbesson for LA FABRIQUE DIY
LICENSE MIT
"""
#include <Adafruit_NeoPixel.h>
// Neopixel data pin
#define PIN 6
// SD card CS pin
#define SDPin 4
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// * SD card attached as follows:
// ** MOSI - pin 11
// ** MISO - pin 12
// ** CLK - pin 13
// ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
#include <SPI.h>
#include <SD.h>
// Neopixel data pin
#define PIN 6
// SD card CS pin
#define SDPin 4
int buttonPin = 2;
bool playAnimation = true;
int buttonState = 0;
int file_position = 0;
int m_NumberOfFiles = 0;
bool runAnimation = true;
String m_FileNames[10];
File root;
File dataFile;
String m_CurrentFilename = "";
int number_of_leds = 40;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(number_of_leds, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
pinMode(13, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(5,HIGH);
strip.begin();
strip.show();
setupSDcard();
delay(100);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
digitalWrite(13,HIGH);
file_position++;
Serial.println(file_position);
delay(100);
digitalWrite(13,LOW);
}
delay(500);
Serial.print("Sending file");
if ( file_position >= m_NumberOfFiles) {
file_position = 0;
}
SendFile(m_FileNames[file_position]);
ClearStrip();
}
void setupSDcard() {
pinMode(SDPin, OUTPUT);
while (!SD.begin(SDPin)) {
Serial.println("SD init failed!");
delay(500);
}
Serial.println("SD init done");
delay(1000);
root = SD.open("/");
Serial.println("Scanning files");
delay(500);
GetFileNamesFromSD(root);
}
// This function lists files inside the SD card
void GetFileNamesFromSD(File dir) {
int fileCount = 0;
String CurrentFilename = "";
while(1) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
m_NumberOfFiles = fileCount;
Serial.println("total files");
Serial.println(fileCount);
entry.close();
break;
}
else {
CurrentFilename = entry.name();
if (CurrentFilename.endsWith(".TXT")) { //only txt files
if(CurrentFilename.startsWith("_")){ // mac sidecar files start with _ and should not be included, may be on card if written from Mac
}else{
m_FileNames[fileCount] = entry.name();
fileCount++;
}
}
}
entry.close();
}
}
// This function blinks the top led twice and starts the animation
void SendFile(String Filename) {
char temp[14];
runAnimation = true;
Filename.toCharArray(temp,14);
Serial.println(Filename);
dataFile = SD.open(temp);
// if the file is available send it to the LED's
if (dataFile) {
int i = 0;
int red, green, blue;
strip.setPixelColor(1,255,255,255);
strip.show();
delay(500);
strip.setPixelColor(1,0,0,0);
strip.show();
delay(500);
strip.setPixelColor(1,255,255,255);
strip.show();
delay(500);
strip.setPixelColor(1,0,0,0);
strip.show();
delay(2000);
while(dataFile.available() && runAnimation){
if (digitalRead(buttonPin) == HIGH) {
runAnimation = false;
break;
delay(100);
}
if (i == (number_of_leds)) {
i=0;
strip.show();
delay(120);
}
red = dataFile.parseInt();
green = dataFile.parseInt();
blue = dataFile.parseInt();
strip.setPixelColor(i, red, green, blue);
i++;
}
Serial.print("closing file");
dataFile.close();
} else {
Serial.print(" Error reading ");
setupSDcard();
return;
}
}
// Turn all LED off on the pixelstick
void ClearStrip() {
int x;
for(x=0;x<number_of_leds;x++) {
strip.setPixelColor(x, 0);
}
strip.show();
}