Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // the TOGGLEPIN is used if there's no SD card. if this pin is jumped to +5V, then we'll assume this is the RLD, otherwise we'll assume this is the FLD
- #define TOGGLEPIN 9
- #define NUM_LEDS 40
- #define DATA_PIN 6
- const int chipSelect = 10; //pin used for sd card
- /* also uses these pins for the sd card...
- ** MOSI = pin 11
- ** MISO = pin 12
- ** CLK = pin 13
- */
- #include <SD.h>
- //SdCard sd;
- File myFile;
- #define FORCE_SOFTWARE_SPI
- #define FORCE_SOFTWARE_PINS
- #include "FastSPI_LED2.h" // https://code.google.com/p/fastspi/downloads/detail?name=FastSPI_LED2.RC4.zip
- CRGB leds[100];
- void setup() {
- Serial.begin(9600);
- randomSeed(analogRead(0)); //helps keep random numbers more random
- //default settings for FLD...
- byte numColors=6;
- int settings[6]={80,8,500,128,0,5};
- byte colors[6][3] = { {170,0,0} , {170,255,54} , {170,255,120} , {166,255,200} , {154,84,150} , {174,0,200} };
- Serial.print("Pin D"+String(TOGGLEPIN)+" is");
- if (digitalRead(TOGGLEPIN)==HIGH) {
- Serial.println(" jumpered, so this is the RLD (maybe)");
- int settings[6]={100,8,1000,128,0,10};
- byte colors[6][3] = { {170,0,0} , {170,255,54} , {170,255,120} , {166,255,200} , {154,84,150} , {174,0,200} };
- }
- else {
- Serial.println("n't jumpered, so this is the FLD (maybe)");
- }
- //int settings[6]; //array to hold values from settings .txt
- const char* setnames[]={"LEDs","fade","pause","maxbri","speeds","tweens"};
- //byte colors[20][3];
- //byte numColors=0;
- boolean sdvalid=true;
- if (!SD.begin(chipSelect)) {
- Serial.println("SD card initialization failed!");
- sdvalid=false;
- }
- // open the file for reading:
- if (!SD.exists("settings.txt")) {
- Serial.println("failed to open settings.txt");
- sdvalid=false;
- }
- else {
- myFile = SD.open("settings.txt");
- byte colors[20][3]; //ignore previously set colors
- byte numColors=0;
- Serial.println("settings.txt opened.\nOverwriting defaults with custom settings.");
- /*int settings[5]; //array to hold values from settings .txt
- char* setnames[]={"LEDs","fade","pause","maxbri","speeds"};
- byte colors[20][3];
- byte numColors=0;*/
- char character;
- String desc = "";
- String value = "";
- //boolean valid = true;
- // read from the file until there's nothing else in it:
- while (myFile.available()) {
- character = myFile.read();
- if(character == '/') {
- // Comment - ignore this line
- while(character != '\n'){
- character = myFile.read();
- };
- }
- else if(isalnum(character)) { // Add a character to the description
- desc.concat(character);
- }
- else if(character =='=') { // start checking the value for possible results
- // First going to trim out all trailing white spaces
- do {
- character = myFile.read();
- }
- while(character == ' ');
- if(desc == "colors") {
- //Serial.println("reading colors...");
- value = "";
- byte tempcolor[3]; //temporary array to hold values of each hsv color
- byte numcount=0; //count the number of numbers (3 numbers = 1 color)
- while(character != '\n') {
- if (character!=',') value.concat(character);
- character = myFile.read();
- if (character==',' || character=='\n') {
- //value is almost ready to be added to our colors array, but must be converted to an int first
- //Serial.println("adding value ("+String(value)+") to colors array at position ["+String(numColors)+"]["+String(numcount)+"]");
- tempcolor[numcount]=value.toInt();
- numcount++;
- if (numcount%3==0) {
- //Serial.println("setting colors["+String(numColors)+"]={"+String(tempcolor[0])+","+String(tempcolor[1])+","+String(tempcolor[2])+"}");
- for (int i = 0; i < 3; i++) colors[numColors][i] = tempcolor[i];
- //Serial.println("-- next color --");
- numcount=0;
- numColors++;
- }
- value = ""; //start value fresh
- }
- }
- }
- else if(desc=="LEDs" || desc=="fade" || desc=="pause" || desc=="maxbri" || desc=="speeds" || desc=="tweens") {
- value = "";
- do {
- value.concat(character);
- character = myFile.read();
- }
- while(character != '\n');
- for (int i = 0; i < 6; i++) {
- if(desc==setnames[i]) settings[i] = value.toInt();
- }
- }
- else { // unknown parameter
- while(character != '\n')
- character = myFile.read();
- }
- desc = "";
- }
- else {
- // Ignore this character (could be space, tab, newline, carriage return or something else)
- }
- }
- // close the file:
- myFile.close();
- }
- if (sdvalid==false) {
- Serial.println("Unable to get custom settings from SD card.");
- }
- for (int i = 0; i < 6; i++) Serial.println(String(setnames[i])+" = "+String(settings[i]));
- for (int i = 0; i < numColors; i++) Serial.println("Clr "+String(i)+" = "+String(colors[i][0])+","+String(colors[i][1])+","+String(colors[i][2]));
- int LEDcolor[numColors][3]; // an array holding the current color number of each LED, its direction and pausetime
- //
- //create a big array with the KeyColors and every tween color in sequence
- const int totalColors=numColors*settings[5]+1;
- byte AllColors[numColors][3];
- byte TweakedColors[numColors][3];
- int primary,value,kcolor,changePerStep,tempLEDnum;
- for(kcolor=0;kcolor<(numColors-1);kcolor++) {
- int totalColorCount=0;
- for(primary=0;primary<3;primary++) {
- /*if (primary==0) Serial.print("Hue : ");
- if (primary==1) Serial.print("Sat : ");
- if (primary==2) Serial.print("Val : "); */
- changePerStep=int(colors[kcolor+1][primary]-colors[kcolor][primary])/(settings[5]+1);
- //Serial.print("primary"+String(primary)+" : change per tween for this primary color is "+String(changePerStep)+"\n");
- int tranCount=0;
- unsigned int value=colors[kcolor][primary];
- while (tranCount<=(settings[5])) {
- if (tranCount==0) {
- totalColorCount=kcolor*(settings[5]+1); //this is the first transition, between these 2 colors, make sure the total count is right
- }
- AllColors[totalColorCount][primary]=value; //set the actual color
- TweakedColors[totalColorCount][primary]=value; //set the same color in our Tweaked array
- tranCount++;
- totalColorCount++;
- value=int(value+changePerStep);
- }
- //Serial.print("\nDone setting all tweens\n");
- }
- }
- //don't forget the very final colors...
- AllColors[totalColors-1][0]=colors[numColors-1][0];
- AllColors[totalColors-1][1]=colors[numColors-1][1];
- AllColors[totalColors-1][2]=colors[numColors-1][2];
- TweakedColors[totalColors-1][0]=colors[numColors-1][0];
- TweakedColors[totalColors-1][1]=colors[numColors-1][1];
- TweakedColors[totalColors-1][2]=colors[numColors-1][2];
- Serial.print("\nDone making our big array of all "+String(totalColors)+" colors\n");
- for (int i = 0; i < totalColors; i++) {
- Serial.println(String(AllColors[i][0])+","+String(AllColors[i][1])+","+String(AllColors[i][2]));
- }
- Serial.println("starting FastLED. LEDs = "+String(settings[0])+" , maxbri = "+String(settings[3]));
- //CRGB leds[100]; // This is an array of leds. One item for each led in your strip.
- //FastLED.setBrightness(settings[3]); //sets the overall brightness to the maximum
- FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, settings[0]);
- for(int LEDnumber = 0; LEDnumber < settings[0]; LEDnumber++ ) leds[LEDnumber] = CRGB::Black; //set all the LEDs to black
- FastLED.show();
- }
- void loop() {
- // nothing happens after setup
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement