#include <FastLED.h>
#include <SPI.h>
#include <SD.h>
#include <EEPROM.h>
#define NUM_LEDS 144 // LED number
#define DATA_PIN 15 // your data arduino pin
#define CHIPSET WS2812 // your LED chip type
#define CMD_NEW_DATA 1
//#define BAUD_RATE 500000 //if using Glediator via serial
unsigned char x = 12; // matrix x size
unsigned char y = 12; // matrix y size
const int buttonPin = 2;
int buttonState = 0;
int file=0;
File fxdata;
CRGB leds[NUM_LEDS];
void setup()
{
FastLED.addLeds<CHIPSET, DATA_PIN, GRB>(leds, NUM_LEDS); //see doc for different LED strips
// Serial.begin(BAUD_RATE); // when using Glediator via usb
// FastLED.setMaxPowerInVoltsAndMilliamps(5,1500);
FastLED.setBrightness(150);
Serial.begin(115200);
pinMode(buttonPin, INPUT);
attachInterrupt(1, pin_ISR, RISING);
for(int y = 0 ; y < NUM_LEDS ; y++)
{
leds[y] = CRGB::Black; // set all leds to black during setup
}
FastLED.show();
pinMode(8, OUTPUT); // CS/SS pin as output for SD library to work.
digitalWrite(8, HIGH); // workaround for sdcard failed error...
delay(200);
if (!SD.begin(8))
{
Serial.println("sdcard initialization failed!");
return;
}
Serial.println("sdcard initialization done.");
// test file open
fxdata = SD.open("1.dat"); // read only
if (fxdata)
{
Serial.println("file open ok");
fxdata.close();
}
}
void pin_ISR() {
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 200)
{
buttonState = digitalRead(buttonPin);
fxdata.close();
file++;
Serial.println("interrupt");
delay(20);
if(file==4) file=1;
EEPROM.write(0,file);
Serial.println(file);
}
last_interrupt_time = interrupt_time;
}
int serialGlediator ()
{
while (!Serial.available()) {}
return Serial.read();
}
void loop()
{
// uncomment for Glediator
//while (fileGlediator () != CMD_NEW_DATA) {}
//Serial.readBytes((char*)leds, NUM_LEDS*3);
switch (EEPROM.read(0))
{
case 0:
fxdata = SD.open("0.dat"); // read only
if (fxdata)
{
Serial.println("file 0 open ok");
}
break;
case 1:
fxdata = SD.open("1.dat"); // read only
if (fxdata)
{
Serial.println("file 1 open ok");
}
break;
case 2:
fxdata = SD.open("2.dat"); // read only
if (fxdata)
{
Serial.println("file 2 open ok");
}
break;
case 3:
fxdata = SD.open("3.dat"); // read only
if (fxdata)
{
Serial.println("file 3 open ok");
}
break;
case 4:
fxdata = SD.open("4.dat"); // read only
if (fxdata)
{
Serial.println("file 4 open ok");
}
break;
case 5:
fxdata = SD.open("5.dat"); // read only
if (fxdata)
{
Serial.println("file 5 open ok");
}
break;
case 6:
fxdata = SD.open("6.dat"); // read only
if (fxdata)
{
Serial.println("file 6 open ok");
}
break;
}
while (fxdata.available())
{
fxdata.readBytes((char*)leds, NUM_LEDS*3);
ledSort(2); //1-4 possible, set your first LED's position. Change the number: 1=TL(top left),2=TR(top right),3=BL(bottom left),4=BR(bottom right)
FastLED.show();
delay(20); // set the speed of the animation. 20 is appx ~ 500k bauds
}
// close the file in order to prevent hanging IO or similar throughout time
fxdata.close();
}
int ledSort (int modus) { //1=TL,2=TR,3=BL,4=BR, this function will rearrange the led array ;-)
CRGB tmp[x];
if(modus == 3 || modus == 4) {
for(int i = 0; i < (y / 2);i++) {
for(int j = 0; j < x;j++) {
tmp[j] = leds[i * x + j];
leds[i * x] = leds[(y - i - 1) * x];
leds[(y - i - 1) * x] = tmp[j];
}
}
}
if(modus == 1 || modus == 3) {
for(int i = 1; i < y; i = i + 2) {
for(int j = x; j > 0;j--) {
tmp[(x - j)] = leds[i * x + j - 1];
}
for(int j = 0; j < x;j++) {
leds[i * x + j] = tmp[j];
}
}
}
if(modus == 2 | modus == 4) {
for(int i = 0; i < y; i = i + 2) {
for(int j = x; j > 0;j--) {
tmp[(x - j)] = leds[i * x + j - 1];
}
for(int j = 0; j < x;j++) {
leds[i * x + j] = tmp[j];
}
}
}
return 1;
}