Advertisement
joymonkey

Logic Engine 7d

May 13th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.62 KB | None | 0 0
  1. //
  2. //  RSeries Logic Engine AVR : Basic Sketch by Paul Murphy
  3. //  ======================================================
  4. //  This sketch provides basic functionality for the RSeries Logic Engine hardware.
  5. //  Does not include microSD reading or I2C communication.
  6. //
  7. //  Uses the FastLED library : https://github.com/FastLED/FastLED/archive/3.0.2.zip
  8. //
  9. //   revision history...
  10. //   2014-12-19 : Added mapping for V2 LED boards (40 LED "3PO" fronts and single board "Kenny" RLD)
  11. //                Added option to skip startup sequence (don't do this if your brightness is set to 100%).
  12. //                Switched to newer FastLED library.
  13. //   2014-03-09 : Added code for Teeces PSI V2
  14. //   2014-01-15 : Optimizing memory to make way for the SD card's wretched 512 byte buffer.
  15. //                Moved rldMap, fldMap, rldColors & fldColors out of SRAM and into flash memory.
  16. //                Eliminated oColors array (adjusted hues are now calculated individually by updateLED instead of all at once).
  17. //                Added debug option to report available SRAM.
  18. //                Need to write the makeColors function and a few small helpers for updateLED.    
  19. //                Need to reduce LEDstat size (direction can be combined with color number to save 64 bytes)
  20. //   2014-01-12 : Fixed prototype FLD bug
  21. //   2014-01-08 : Removed debug options to free up some RAM. Fixed rldMap. Fixed fldMap.
  22. //   2014-01-07 : Added 'Testpattern Mode' to aid assembly of the Rear Logic
  23. //
  24.  
  25. /*
  26.  
  27.   Dr.Paul explains Colors, LED Direction and (if you're lucky) Midichlorians:
  28.  
  29.   We give the sketch a selection of 'Key' colors. For the Rear Logic we use 5 colors; black, dark green, green, yellow, red.
  30.   The sketch takes those Key colors and generates several 'Tween' colors that fade between each Key color. These colors are
  31.   all put in order in a big array called AllColors. The color elements themselves are HSV values (Hue, Saturation, Value).
  32.  
  33.   During the sketches main loop(), each pixel is cycled through all these colors, so thanks to the tween colors they appear to
  34.   fade from color to color.  As each Key color is reached the pixel is paused for a slightly random number of loops; this
  35.   randomness causes the color patterns to constantly evolve, so over time you will see different patterns emerge.
  36.  
  37.   Say we have 3 key colors and 1 tween color. This creates an AllColors array of 5 unique colors...
  38.     0: Black
  39.     1: Tween
  40.     2: Red
  41.     3: Tween
  42.     4: Yellow
  43.   After the 5th color (Yellow) is reached, we want to switch direction...
  44.     5: Tween (3)
  45.     6: Red   (2)
  46.     7: Tween (1)
  47.   So even though we only use 5 unique colors, each pixel loops through 8 color changes.
  48.   Sometimes we might refer to Color 6; there is no Color 6 in our AllColors array, but we can figure it out...
  49.   if (colorNum>=numColors) actualColorNum=numColors-(colorNum-numColors)-2;
  50.   using the above example numbers...
  51.   if (6>=5) colorNum=5-(6-5)-2; // so color 6 is really 2 (Red) when looping through the array in reverse
  52.  
  53.   To put it simply, if colorNum >= numColors then the pixel is moving in reverse through the color array.
  54. */
  55.  
  56. #define skipStartup 0  // 0 to start with a simple LED animation, 1 to skip
  57.  
  58. #define PSIbright 12
  59. int psiRed=2500;    //how long front PSI stays red  (or yellow)
  60. int psiBlue=1700;   //how long front PSI stays blue (or green)
  61. #define rbSlide 125 // mts - time to transition between red and blue
  62.  
  63. //  the TOGGLEPIN is used if there's no SD card. if this pin is jumped to +5V,
  64. //  then we'll assume this is the RLD, otherwise we'll assume this is the FLD
  65. #define TOGGLEPIN 4
  66. //  the SPEEDPIN enables speed adjustment via the pots via the pause & delay
  67. #define SPEEDPIN 3
  68.  
  69. //debug will print a bunch of stuff to serial. useful, but takes up valuable memory and may slow down routines a little
  70. #define DEBUG 0 //0=off, 1=only report available memory, 2=report lots of stuff, 3=slow the main loop down
  71.  
  72. #define PCBVERSION 2 // 1 if your LED boards are from the first run (48 LEDs per PCB), otherwise set this to 2
  73.  
  74. #define LOOPCHECK 20 //number of loops after which we'll check the pot values
  75. //                                                                                           Variable Sizes in SRAM:
  76. #include "FastLED.h"
  77. #define DATA_PIN 6
  78. CRGB leds[96];      // structure used by the FastSPI_LED library                                    288 bytes
  79.  
  80. byte Tweens=6;     // number of tween colors to generate                                              1 byte
  81. byte tweenPause=7; // time to delay each tween color (aka fadeyness)                                  1 byte
  82. int keyPause=350; // time to delay each key color (aka pauseyness)                                    2 bytes
  83. byte maxBrightness=200; // 0-255, no need to ever go over 128                                         1 byte
  84.  
  85. int keyPin = A0; //analog pin to read keyPause value                                                  2 bytes
  86. int tweenPin = A1; //analog pin to read tweenPause value                                              2 bytes
  87. int briPin = A2; //analog pin to read Brightness value                                                2 bytes
  88. int huePin = A3; //analog pin to read Color/Hue shift value                                           2 bytes
  89.  
  90. byte totalColors,Keys; //                                                                             2 bytes
  91.  
  92. byte AllColors[45][3]; // a big array to hold all original KeyColors and all Tween colors           135 bytes
  93. byte LEDstat[96][3]; // an array holding the current color number of each LED                       288 bytes
  94. unsigned int LEDpause[96]; // holds pausetime for each LED (how many loops before changing colors)  192 bytes
  95. boolean speeds=0; //0 for preset, 1 for tweakable (depends on speedpin)                               1 byte
  96. boolean logic=0; //0 for fld, 1 for rld (depends on togglepin)                                        1 byte
  97.  
  98. unsigned int loopCount; // used during main loop                                                      2 bytes
  99. byte briVal,prevBri,briDiff,hueVal; // global variables used during main loop and functions           4 bytes
  100. #if (DEBUG>1)
  101. unsigned long time; //used to calculate loops-per-second in debug mode
  102. #endif
  103.  
  104. //our LEDs aren't in numeric order (FLD is wired serpentine and RLD is all over the place!)
  105. //so we can address specific LEDs more easily, we'll map them out here...
  106. #include <avr/pgmspace.h> //to save SRAM, this stuff all gets stored in flash memory
  107.  
  108. #if PCBVERSION<2
  109.  
  110.   //mapping for first RLD (two PCBs soldered together)
  111.   const byte rldMap[]PROGMEM = {
  112.    0, 1, 2, 3, 4, 5, 6, 7,48,49,50,51,52,53,54,55,
  113.   15,14,13,12,11,10, 9, 8,63,62,61,60,59,58,57,56,
  114.   16,17,18,19,20,21,22,23,64,65,66,67,68,69,70,71,
  115.   31,30,29,28,27,26,25,24,79,78,77,76,75,74,73,72,
  116.   32,33,34,35,36,37,38,39,80,81,82,83,84,85,86,87,
  117.   47,46,45,44,43,42,41,40,95,94,93,92,91,90,89,88};
  118.   //mapping for FLD boards from first run (with 48 LEDs per PCB)
  119.   const byte fldMap[]PROGMEM = {
  120.   15,14,13,12,11,10, 9, 8,
  121.   16,17,18,19,20,21,22,23,
  122.   31,30,29,28,27,26,25,24,
  123.   32,33,34,35,36,37,38,39,
  124.   47,46,45,44,43,42,41,40, //
  125.   88,89,90,91,92,93,94,95, //
  126.   87,86,85,84,83,82,81,80,
  127.   72,73,74,75,76,77,78,79,
  128.   71,70,69,68,67,66,65,64,
  129.   56,57,58,59,60,61,62,63};
  130.  
  131. #else
  132.  
  133.   //mapping for single RLD PCB (second parts run on)...
  134.   const byte rldMap[]PROGMEM = {
  135.    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
  136.   31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,
  137.   32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  138.   63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,
  139.   64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  140.   95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80};
  141.   //mapping for newer FLD PCBs (40 LEDs per PCB)...
  142.   const byte fldMap[]PROGMEM = {
  143.    0, 1, 2, 3, 4, 5, 6, 7,
  144.   15,14,13,12,11,10, 9, 8,
  145.   16,17,18,19,20,21,22,23,
  146.   31,30,29,28,27,26,25,24,
  147.   32,33,34,35,36,37,38,39,
  148.   40,41,42,43,44,45,46,47,
  149.   55,54,53,52,51,50,49,48,
  150.   56,57,58,59,60,61,62,63,
  151.   71,70,69,68,67,66,65,64,
  152.   72,73,74,75,76,77,78,79};
  153.  
  154. #endif
  155.  
  156. //default colors also get stored in flash memory...
  157. const byte fldColors[6][3]PROGMEM = { {170,0,0} , {170,255,54} , {170,255,120} , {166,255,200} , {154,84,150} , {174,0,200} };
  158. const byte rldColors[5][3]PROGMEM = { {87,0,0} , {87,206,105} , {79,255,184} , {18,255,250} , {0,255,214} };
  159.  
  160. //experimental microSD stuff
  161. /*#include <SD.h>
  162. const int chipSelect = 10; //digital pin used for microSD
  163. File myFile;*/
  164.  
  165. // TEECES PSI CODE...
  166. #include <LedControl.h>
  167. LedControl lcPSI=LedControl(7,8,9,1); //Data,Clock,Load,DevNum  (pins go GND,+5V,L,C,D)
  168. #define HPROW 5
  169.   class PSI {
  170.   int stage; //0 thru 6
  171.   int inc;
  172.   int stageDelay[7];
  173.   int cols[7][5];
  174.   int randNumber; //a random number to decide the fate of the last stage
  175.  
  176.   unsigned long timeLast;
  177.   int device;
  178.  
  179.   public:
  180.  
  181.   PSI(int _delay1, int _delay2, int _delay3, int _device)
  182.   {
  183.     device=_device;
  184.    
  185.     stage=0;
  186.     timeLast=0;
  187.     inc=1;
  188.    
  189.     cols[0][0] = B10101000;
  190.     cols[0][1] = B01010100;
  191.     cols[0][2] = B10101000;
  192.     cols[0][3] = B01010100;
  193.     cols[0][4] = B10101000;
  194.    
  195.     cols[1][0] = B00101000; //R B R B R B
  196.     cols[1][1] = B11010100; //B R B R B R
  197.     cols[1][2] = B00101000; //R B R B R B
  198.     cols[1][3] = B11010100; //B R B R B R
  199.     cols[1][4] = B00101000; //R B R B R B
  200.  
  201.     cols[2][0] = B01101000;
  202.     cols[2][1] = B10010100;
  203.     cols[2][2] = B01101000;
  204.     cols[2][3] = B10010100;
  205.     cols[2][4] = B01101000;
  206.    
  207.     cols[3][0] = B01001000;
  208.     cols[3][1] = B10110100;
  209.     cols[3][2] = B01001000;
  210.     cols[3][3] = B10110100;
  211.     cols[3][4] = B01001000;
  212.    
  213.     cols[4][0] = B01011000;
  214.     cols[4][1] = B10100100;
  215.     cols[4][2] = B01011000;
  216.     cols[4][3] = B10100100;
  217.     cols[4][4] = B01011000;
  218.    
  219.     cols[5][0] = B01010000;
  220.     cols[5][1] = B10101100;
  221.     cols[5][2] = B01010000;
  222.     cols[5][3] = B10101100;
  223.     cols[5][4] = B01010000;
  224.    
  225.     cols[6][0] = B01010100;
  226.     cols[6][1] = B10101000;
  227.     cols[6][2] = B01010100;
  228.     cols[6][3] = B10101000;
  229.     cols[6][4] = B01010100;
  230.    
  231.     stageDelay[0] = _delay1 - _delay3;
  232.     stageDelay[1] = _delay3/5;
  233.     stageDelay[2] = _delay3/5;
  234.     stageDelay[3] = _delay3/5;
  235.     stageDelay[4] = _delay3/5;
  236.     stageDelay[5] = _delay3/5;
  237.     stageDelay[6] = _delay2 - _delay3;
  238.   }
  239.  
  240.   void Animate(unsigned long elapsed, LedControl control)
  241.   {
  242.     if ((elapsed - timeLast) < stageDelay[stage]) return;
  243.    
  244.     timeLast = elapsed;
  245.     stage+=inc;
  246.  
  247.     if (stage>6 || stage<0 )
  248.     {
  249.       inc *= -1;
  250.       stage+=inc*2;
  251.     }
  252.    
  253.     if (stage==6) //randomly choose whether or not to go 'stuck'
  254.       {
  255.         randNumber = random(9);
  256.         if (randNumber<5) { //set the last stage to 'stuck'
  257.           cols[6][0] = B01010000;
  258.           cols[6][1] = B10101100;
  259.           cols[6][2] = B01010000;
  260.           cols[6][3] = B10101100;
  261.           cols[6][4] = B01010000;
  262.         }
  263.         else //reset the last stage to a solid color
  264.         {
  265.           cols[6][0] = B01010100;
  266.           cols[6][1] = B10101000;
  267.           cols[6][2] = B01010100;
  268.           cols[6][3] = B10101000;
  269.           cols[6][4] = B01010100;
  270.         }
  271.       }
  272.      if (stage==0) //randomly choose whether or not to go 'stuck'
  273.       {
  274.         randNumber = random(9);
  275.         if (randNumber<5) { //set the first stage to 'stuck'
  276.           cols[0][0] = B00101000; //R B R B R B
  277.           cols[0][1] = B11010100; //B R B R B R
  278.           cols[0][2] = B00101000; //R B R B R B
  279.           cols[0][3] = B11010100; //B R B R B R
  280.           cols[0][4] = B00101000; //R B R B R B
  281.         }
  282.         else //reset the first stage to a solid color
  283.         {
  284.           cols[0][0] = B10101000;
  285.           cols[0][1] = B01010100;
  286.           cols[0][2] = B10101000;
  287.           cols[0][3] = B01010100;
  288.           cols[0][4] = B10101000;
  289.         }
  290.       }
  291.  
  292.     for (int row=0; row<5; row++)
  293.       control.setRow(device,row,cols[stage][row]);
  294.   }
  295. };
  296. PSI psiFront=PSI(psiRed, psiBlue, rbSlide, 0);
  297.  
  298.  
  299. void setup() {      
  300.      
  301.       delay(50); // sanity check delay
  302.       randomSeed(analogRead(0)); //helps keep random numbers more random  
  303.       #if (DEBUG>0)
  304.       Serial.begin(9600);        
  305.       #endif    
  306.      
  307.       lcPSI.shutdown(0, false); //take the device out of shutdown (power save) mode
  308.       lcPSI.clearDisplay(0);
  309.       lcPSI.setIntensity(0,PSIbright);
  310.      
  311.       pinMode(TOGGLEPIN, INPUT);
  312.       //digitalWrite(TOGGLEPIN, HIGH); //used for dipswitch prototype
  313.       logic=digitalRead(TOGGLEPIN);      
  314.       if (logic==1) {
  315.         #if (DEBUG>1)
  316.         Serial.println("RLD");
  317.         #endif        
  318.         //logic=1;
  319.         //numLeds=96;
  320.         //slow default speeds down for the RLD...
  321.         tweenPause=40;
  322.         keyPause=1200;
  323.         Keys=sizeof(rldColors)/3;
  324.         #if (DEBUG>1)
  325.         Serial.println(String(Keys)+" Keys");
  326.         #endif
  327.       }  
  328.       else {
  329.         #if (DEBUG>1)
  330.         Serial.println("FLD");
  331.         #endif
  332.         //numLeds=80;
  333.         Keys=sizeof(fldColors)/3;        
  334.       }    
  335.  
  336.       totalColors=Keys*Tweens;
  337.       #if (DEBUG>1)
  338.       Serial.println(String(Keys)+" Keys\n"+String(Tweens)+" Tweens\n"+String(totalColors)+" Total");
  339.       #endif
  340.      
  341.       pinMode(SPEEDPIN, INPUT);
  342.       //digitalWrite(SPEEDPIN, HIGH); //used for dipswitch prototype
  343.       if (digitalRead(SPEEDPIN)==HIGH) {
  344.         speeds=1;  
  345.         #if (DEBUG>1)
  346.         Serial.println("SPD");
  347.         #endif
  348.       }  
  349.      
  350.       //make a giant array of all colors tweened...
  351.       byte el,val,kcol,perStep,totalColorCount; //
  352.       for(kcol=0;kcol<(Keys);kcol++) { //go through each Key color
  353.         for(el=0;el<3;el++) { //loop through H, S and V
  354.           if (logic==0) perStep=int(pgm_read_byte(&fldColors[kcol+1][el])-pgm_read_byte(&fldColors[kcol][el]))/(Tweens+1);
  355.           else perStep=int(pgm_read_byte(&rldColors[kcol+1][el])-pgm_read_byte(&rldColors[kcol][el]))/(Tweens+1);
  356.           byte tweenCount=0;
  357.           if (logic==0) val=pgm_read_byte(&fldColors[kcol][el]);
  358.           else val=pgm_read_byte(&rldColors[kcol][el]);
  359.           while (tweenCount<=Tweens) {
  360.             if (tweenCount==0) totalColorCount=kcol*(Tweens+1); //this is the first transition, between these 2 colors, make sure the total count is right
  361.             AllColors[totalColorCount][el]=val; //set the actual color element (H, S or V)
  362.             tweenCount++;
  363.             totalColorCount++;
  364.             val=byte(val+perStep);
  365.           }  
  366.         }  
  367.       }
  368.       /*#if (DEBUG>1)
  369.       // print all the colors
  370.       for(byte x=0;x<totalColors;x++) {
  371.         Serial.println(String(x)+" : "+String(AllColors[x][0])+","+String(AllColors[x][1])+","+String(AllColors[x][2])+"  o  "+String(oColors[x][0])+","+String(oColors[x][1])+","+String(oColors[x][2]));
  372.       }  
  373.       #endif */
  374.      
  375.       FastLED.setBrightness(maxBrightness); //sets the overall brightness to the maximum
  376.       FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, 96);
  377.      
  378.       for(byte x=0;x<96;x++) leds[x] = CRGB::Black; //sets all possible LEDs to black
  379.       FastLED.show();  
  380.      
  381.       //to get the RLD test mode, S4 should have jumper, S3 no jumper, Delay trimmer should be turned completely counter-clockwise
  382.       int delayVal = analogRead(keyPin);      
  383.       if (logic==1 && speeds==0 && delayVal<10) {
  384.         //testpattern for RLD
  385.         byte testColor[96] = {
  386.         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  387.         2,2,2,3,3,3,3,2,2,3,3,3,3,2,2,2,
  388.         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  389.         5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  390.         2,2,2,3,3,3,3,2,2,3,3,3,3,2,2,2,
  391.         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  392.         //go through all our LEDs, setting them to appropriate colors
  393.         for(byte x=0;x<96;x++) {
  394.            if (testColor[x]==1) leds[pgm_read_byte(&rldMap[x])] = CRGB::DarkRed;
  395.            if (testColor[x]==2) leds[pgm_read_byte(&rldMap[x])] = CRGB::DarkOrange;
  396.            if (testColor[x]==3) leds[pgm_read_byte(&rldMap[x])] = CRGB::DarkGreen;
  397.            if (testColor[x]==4) leds[pgm_read_byte(&rldMap[x])] = CRGB::DeepSkyBlue;
  398.            if (testColor[x]==5) leds[pgm_read_byte(&rldMap[x])] = CRGB::Orchid;
  399.            FastLED.setBrightness(10);  
  400.            FastLED.show();  
  401.            delay(10);        
  402.         }
  403.         delay(999999);
  404.         FastLED.setBrightness(maxBrightness);
  405.       }
  406.      
  407.       //configure each LED's status (color number, direction, pause time)
  408.       for(byte x=0;x<96;x++) {
  409.         LEDstat[x][0]=byte(random(totalColors)); //choose a random color number to start this LED at
  410.         LEDstat[x][1]=random(2); //choose a random direction for this LED (0 up or 1 down)
  411.         if (LEDstat[x][0]%(Tweens+1)==0) LEDstat[x][2]=random(keyPause); //color is a key, set its pause time for longer than tweens
  412.         else LEDstat[x][2]=random(tweenPause);
  413.       }
  414.       //now set the LEDs to their initial colors...
  415.       for(byte x=0;x<96;x++) {          
  416.         if (logic==1)  leds[pgm_read_byte(&rldMap[x])].setHSV(AllColors[LEDstat[x][0]][0],AllColors[LEDstat[x][0]][1],AllColors[LEDstat[x][0]][2]);
  417.         else {
  418.           if (x<80) leds[pgm_read_byte(&fldMap[x])].setHSV(AllColors[LEDstat[x][0]][0],AllColors[LEDstat[x][0]][1],AllColors[LEDstat[x][0]][2]) ;
  419.         }  
  420.         #if (skipStartup==0)
  421.         FastLED.show();
  422.         delay(15);
  423.         #endif
  424.       }
  425.       FastLED.show();
  426.        
  427.       #if (DEBUG>0)
  428.       Serial.println(memoryFree()); // print the free memory
  429.       delay(250);
  430.       #endif
  431. }
  432.  
  433. #if (DEBUG>0)
  434. // variables created by the build process when compiling the sketch (used for the memoryFree function)
  435. extern int __bss_end;
  436. extern void *__brkval;
  437. // function to return the amount of free RAM
  438. int memoryFree() {
  439.   int freeValue;
  440.   if((int)__brkval == 0) freeValue = ((int)&freeValue) - ((int)&__bss_end);
  441.   else freeValue = ((int)&freeValue) - ((int)__brkval);
  442.   return freeValue;
  443. }
  444. #endif
  445.  
  446.  
  447. void updateLED(byte LEDnum, byte hueVal) {
  448.     //this will take an LED number and adjust its status in the LEDstat array
  449.     //check the current color this LED is set to...
  450.     //unsigned int currentColor=LEDstat[LEDnum];  
  451.     if (LEDstat[LEDnum][2]!=0) {
  452.       //LED is paused
  453.       LEDstat[LEDnum][2]=LEDstat[LEDnum][2]-1; //reduce the LEDs pause number and check back next loop
  454.     }
  455.     else {
  456.         //LED had 0 pause time, let's change things around...
  457.         if (LEDstat[LEDnum][1]==0 && LEDstat[LEDnum][0]<(totalColors-1)) {
  458.             LEDstat[LEDnum][0]=LEDstat[LEDnum][0]+1; //change it to next color
  459.             leds[LEDnum].setHSV(AllColors[LEDstat[LEDnum][0]][0]+hueVal,AllColors[LEDstat[LEDnum][0]][1],AllColors[LEDstat[LEDnum][0]][2]);
  460.             if (LEDstat[LEDnum][0]%(Keys+1)==0) LEDstat[LEDnum][2]=random(keyPause); //color is a key, set its pause time for longer than tweens
  461.             else LEDstat[LEDnum][2]=random(tweenPause);
  462.         }
  463.         else if (LEDstat[LEDnum][1]==0 && LEDstat[LEDnum][0]==(totalColors-1)) {
  464.             LEDstat[LEDnum][1]=1; //LED is at the final color, leave color but change direction to down
  465.         }
  466.         else if (LEDstat[LEDnum][1]==1 && LEDstat[LEDnum][0]>0) {
  467.             LEDstat[LEDnum][0]=LEDstat[LEDnum][0]-1; //change it to previous color
  468.             leds[LEDnum].setHSV(AllColors[LEDstat[LEDnum][0]][0]+hueVal,AllColors[LEDstat[LEDnum][0]][1],AllColors[LEDstat[LEDnum][0]][2]);
  469.             if (LEDstat[LEDnum][0]%(Keys+1)==0) {
  470.               LEDstat[LEDnum][2]=random(keyPause); //new color is a key, set LED's pause time for longer than tweens
  471.             }
  472.             else LEDstat[LEDnum][2]=tweenPause;
  473.         }
  474.         else if (LEDstat[LEDnum][1]==1 && LEDstat[LEDnum][0]==0) {
  475.             LEDstat[LEDnum][1]=0; //LED is at the first color (0), leave color but change direction to up
  476.         }
  477.     }  
  478. }
  479.  
  480.  
  481. void loop() {
  482.  
  483.     /*#if (DEBUG>1)
  484.     if (loopCount==1) time=millis();
  485.     #endif*/
  486.  
  487.     if (loopCount==LOOPCHECK) { //only check this stuff every 100 or so loops
  488.        
  489.        #if (DEBUG>0)
  490.        Serial.println(String(memoryFree())); // print the free memory  
  491.        #endif
  492.        #if (DEBUG>1)
  493.        time=(micros()-time)/(LOOPCHECK-1);
  494.        Serial.println("lps "+String(1000000/time)+"\n");
  495.        time=micros();
  496.        #endif
  497.              
  498.        if (speeds==1) {
  499.          /*#if (DEBUG>1)
  500.          Serial.println("checking key and tween pots\n");
  501.          #endif*/
  502.          keyPause = analogRead(keyPin);  
  503.          tweenPause = round(analogRead(tweenPin)/10);
  504.        }
  505.        
  506.        // LET THE USER ADJUST GLOBAL BRIGHTNESS...
  507.        briVal = (round(analogRead(briPin)/4)*maxBrightness)/255; //the Bright trimpot has a value between 0 and 1024, we divide this down to between 0 and our maxBrightness
  508.        if (briVal!=prevBri) {
  509.          briDiff=(max(briVal,prevBri)-min(briVal,prevBri));
  510.          if (briDiff>=2) {
  511.              /*#if (DEBUG>1)
  512.              Serial.println("bri "+String(briVal));
  513.              //delay(100);
  514.              #endif*/
  515.              FastLED.setBrightness(briVal); //sets the overall brightness
  516.          }
  517.          prevBri=briVal;
  518.        }
  519.        loopCount=0;      
  520.     }
  521.    
  522.     unsigned long timeNew= millis();
  523.     psiFront.Animate(timeNew, lcPSI);
  524.    
  525.     loopCount++;    
  526.    
  527.     hueVal = round(analogRead(huePin)/4); //read the Color trimpot (gets passed to updateLED for each LED)
  528.     //go through each LED and update it
  529.     for(byte LEDnum=0;LEDnum<96;LEDnum++) {
  530.       if (logic==1) updateLED(pgm_read_byte(&rldMap[LEDnum]),hueVal);
  531.       else if (LEDnum<80) updateLED(pgm_read_byte(&fldMap[LEDnum]),hueVal);
  532.     }  
  533.     FastLED.show();
  534.    
  535.     #if (DEBUG>2)
  536.     delay(10); //slow things down to a crawl
  537.     #endif
  538. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement