Advertisement
joymonkey

LogicEngineSD2

Nov 17th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.11 KB | None | 0 0
  1. // 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
  2. #define TOGGLEPIN 9
  3. #define NUM_LEDS 40
  4. #define DATA_PIN 6
  5. const int chipSelect = 10; //pin used for sd card
  6. /* also uses these pins for the sd card...
  7.  ** MOSI = pin 11
  8.  ** MISO = pin 12
  9.  ** CLK  = pin 13
  10. */
  11. #include <SD.h>
  12. //SdCard sd;
  13. File myFile;
  14.  
  15. #define FORCE_SOFTWARE_SPI
  16. #define FORCE_SOFTWARE_PINS
  17. #include "FastSPI_LED2.h" // https://code.google.com/p/fastspi/downloads/detail?name=FastSPI_LED2.RC4.zip
  18. CRGB leds[100];
  19.  
  20. void setup() {
  21.   Serial.begin(9600);
  22.   randomSeed(analogRead(0)); //helps keep random numbers more random
  23.  
  24.   //default settings for FLD...
  25.   byte numColors=6;
  26.   int settings[6]={80,8,500,128,0,5};
  27.   byte colors[6][3] = { {170,0,0} , {170,255,54} , {170,255,120} , {166,255,200} , {154,84,150} , {174,0,200} };
  28.  
  29.   Serial.print("Pin D"+String(TOGGLEPIN)+" is");
  30.   if (digitalRead(TOGGLEPIN)==HIGH) {
  31.     Serial.println(" jumpered, so this is the RLD (maybe)");
  32.     int settings[6]={100,8,1000,128,0,10};
  33.     byte colors[6][3] = { {170,0,0} , {170,255,54} , {170,255,120} , {166,255,200} , {154,84,150} , {174,0,200} };    
  34.   }
  35.   else {
  36.     Serial.println("n't jumpered, so this is the FLD (maybe)");
  37.   }
  38.  
  39.   //int settings[6]; //array to hold values from settings .txt
  40.   const char* setnames[]={"LEDs","fade","pause","maxbri","speeds","tweens"};
  41.   //byte colors[20][3];
  42.   //byte numColors=0;
  43.   boolean sdvalid=true;
  44.  
  45.   if (!SD.begin(chipSelect)) {
  46.     Serial.println("SD card initialization failed!");
  47.     sdvalid=false;
  48.   }
  49.   // open the file for reading:
  50.   if (!SD.exists("settings.txt")) {
  51.     Serial.println("failed to open settings.txt");
  52.     sdvalid=false;
  53.   }
  54.   else {
  55.     myFile = SD.open("settings.txt");
  56.     byte colors[20][3]; //ignore previously set colors
  57.     byte numColors=0;
  58.     Serial.println("settings.txt opened.\nOverwriting defaults with custom settings.");
  59.    
  60.     /*int settings[5]; //array to hold values from settings .txt
  61.     char* setnames[]={"LEDs","fade","pause","maxbri","speeds"};
  62.     byte colors[20][3];
  63.     byte numColors=0;*/
  64.     char character;
  65.     String desc = "";
  66.     String value = "";
  67.     //boolean valid = true;
  68.    
  69.     // read from the file until there's nothing else in it:
  70.     while (myFile.available()) {
  71.         character = myFile.read();
  72.         if(character == '/')         {
  73.           // Comment - ignore this line
  74.           while(character != '\n'){
  75.             character = myFile.read();
  76.           };
  77.         }
  78.         else if(isalnum(character))      {  // Add a character to the description
  79.           desc.concat(character);
  80.         }
  81.         else if(character =='=')         {  // start checking the value for possible results
  82.           // First going to trim out all trailing white spaces
  83.           do {
  84.             character = myFile.read();
  85.           }
  86.           while(character == ' ');      
  87.          
  88.           if(desc == "colors") {
  89.             //Serial.println("reading colors...");
  90.             value = "";
  91.             byte tempcolor[3]; //temporary array to hold values of each hsv color
  92.             byte numcount=0;   //count the number of numbers (3 numbers = 1 color)
  93.             while(character != '\n') {
  94.               if (character!=',') value.concat(character);
  95.               character = myFile.read();
  96.               if (character==',' || character=='\n') {
  97.                  //value is almost ready to be added to our colors array, but must be converted to an int first
  98.                  //Serial.println("adding value ("+String(value)+") to colors array at position ["+String(numColors)+"]["+String(numcount)+"]");
  99.                  tempcolor[numcount]=value.toInt();
  100.                  numcount++;
  101.                  if (numcount%3==0) {
  102.                    //Serial.println("setting colors["+String(numColors)+"]={"+String(tempcolor[0])+","+String(tempcolor[1])+","+String(tempcolor[2])+"}");
  103.                    for (int i = 0; i < 3; i++) colors[numColors][i] = tempcolor[i];                
  104.                    //Serial.println("-- next color --");
  105.                    numcount=0;
  106.                    numColors++;
  107.                  }  
  108.                  value = ""; //start value fresh
  109.               }  
  110.             }
  111.          }
  112.          else if(desc=="LEDs" || desc=="fade" || desc=="pause" || desc=="maxbri" || desc=="speeds" || desc=="tweens") {
  113.             value = "";
  114.             do {
  115.               value.concat(character);
  116.               character = myFile.read();
  117.             }
  118.             while(character != '\n');
  119.             for (int i = 0; i < 6; i++) {
  120.               if(desc==setnames[i]) settings[i] = value.toInt();
  121.             }  
  122.            
  123.           }  
  124.           else { // unknown parameter
  125.             while(character != '\n')
  126.               character = myFile.read();
  127.           }
  128.           desc = "";
  129.         }
  130.         else {
  131.           // Ignore this character (could be space, tab, newline, carriage return or something else)
  132.         }    
  133.     }  
  134.     // close the file:
  135.     myFile.close();  
  136.   }
  137.   if (sdvalid==false) {
  138.     Serial.println("Unable to get custom settings from SD card.");
  139.   }  
  140.   for (int i = 0; i < 6; i++) Serial.println(String(setnames[i])+" = "+String(settings[i]));
  141.   for (int i = 0; i < numColors; i++) Serial.println("Clr "+String(i)+" = "+String(colors[i][0])+","+String(colors[i][1])+","+String(colors[i][2]));
  142.  
  143.   int LEDcolor[numColors][3]; // an array holding the current color number of each LED, its direction and pausetime
  144.  
  145.   //
  146.   //create a big array with the KeyColors and every tween color in sequence
  147.   const int totalColors=numColors*settings[5]+1;
  148.   byte AllColors[numColors][3];
  149.   byte TweakedColors[numColors][3];
  150.   int primary,value,kcolor,changePerStep,tempLEDnum;    
  151.   for(kcolor=0;kcolor<(numColors-1);kcolor++) {
  152.     int totalColorCount=0;  
  153.     for(primary=0;primary<3;primary++) {  
  154.        /*if (primary==0) Serial.print("Hue : ");
  155.        if (primary==1) Serial.print("Sat : ");
  156.        if (primary==2) Serial.print("Val : ");  */
  157.        changePerStep=int(colors[kcolor+1][primary]-colors[kcolor][primary])/(settings[5]+1);
  158.        
  159.        //Serial.print("primary"+String(primary)+" : change per tween for this primary color is "+String(changePerStep)+"\n");
  160.        int tranCount=0;
  161.        unsigned int value=colors[kcolor][primary];
  162.        while (tranCount<=(settings[5])) {
  163.          if (tranCount==0) {
  164.            totalColorCount=kcolor*(settings[5]+1); //this is the first transition, between these 2 colors, make sure the total count is right
  165.          }
  166.          AllColors[totalColorCount][primary]=value; //set the actual color
  167.          TweakedColors[totalColorCount][primary]=value; //set the same color in our Tweaked array
  168.          
  169.          tranCount++;
  170.          totalColorCount++;
  171.          value=int(value+changePerStep);
  172.        }  
  173.        //Serial.print("\nDone setting all tweens\n");
  174.     }    
  175.   }
  176.   //don't forget the very final colors...
  177.   AllColors[totalColors-1][0]=colors[numColors-1][0];
  178.   AllColors[totalColors-1][1]=colors[numColors-1][1];
  179.   AllColors[totalColors-1][2]=colors[numColors-1][2];
  180.   TweakedColors[totalColors-1][0]=colors[numColors-1][0];
  181.   TweakedColors[totalColors-1][1]=colors[numColors-1][1];
  182.   TweakedColors[totalColors-1][2]=colors[numColors-1][2];
  183.  
  184.   Serial.print("\nDone making our big array of all "+String(totalColors)+" colors\n");
  185.  
  186.   for (int i = 0; i < totalColors; i++) {
  187.     Serial.println(String(AllColors[i][0])+","+String(AllColors[i][1])+","+String(AllColors[i][2]));
  188.   }
  189.  
  190.   Serial.println("starting FastLED. LEDs = "+String(settings[0])+" , maxbri = "+String(settings[3]));
  191.  
  192.   //CRGB leds[100]; // This is an array of leds.  One item for each led in your strip.  
  193.   //FastLED.setBrightness(settings[3]); //sets the overall brightness to the maximum
  194.   FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, settings[0]);
  195.   for(int LEDnumber = 0; LEDnumber < settings[0]; LEDnumber++ ) leds[LEDnumber] = CRGB::Black; //set all the LEDs to black
  196.   FastLED.show();  
  197.  
  198.  
  199. }
  200.  
  201. void loop() {
  202.   // nothing happens after setup
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement