shdowmyst

Untitled

Dec 2nd, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.17 KB | None | 0 0
  1.  
  2. // included libraries
  3. #include <FastLED.h>
  4.  
  5. #define LED_PIN    6
  6. #define NUM_LEDS    144
  7. #define BRIGHTNESS  255
  8. #define UPDATES_PER_SECOND 60  //setting anything lower will result in horrible flickering, no idea why.
  9.  
  10.  
  11. CRGB leds[NUM_LEDS];
  12.  
  13. uint8_t Reverse = 0;
  14. uint8_t RandomReverse = 0;
  15.  
  16. //freshly added settings
  17. uint8_t BrightnessAnim = 0;    //should we animate brightness?
  18.  
  19. uint8_t BeatSineMax = 10;
  20. uint8_t BeatSineBPM = 4;
  21. uint8_t SpeedMethod = 0;      //0 static, (1), 1 is beatsine.
  22. const uint8_t StaticScale = (256 + (NUM_LEDS - 1)) / NUM_LEDS;
  23.  
  24. // POST PROCESS
  25.  
  26. // PaletteBrightness
  27. uint8_t PaletteBrightness = 125;
  28.  
  29.  
  30. // Fill settings
  31. uint8_t StartIndex = 0;   //starting position of the gradient between 0-255, incrase it by 255 / LED_NUM to step one led.
  32. uint8_t SpeedIndex = 0;   //Speed starting value. Eeach animation method works with different speed. Not linear, 0 means stop, 1-120 slowly incrase above 120 its speed up rapidly.
  33. uint8_t FillScale = StaticScale;
  34. // Fill methods
  35. uint8_t FillMethod = 1;  /* 0 = same color fill, all leds are same color selected from palette, Startindex will be color of the leds!  
  36.               1 = Gradient fill, maps the gradient on the leds.
  37.               2 = Sine wave fill.
  38.               3 = Random Noise based fill.
  39.              */
  40.  
  41. uint8_t AnimationMethod = 0;  /* 0 = no animation
  42.                    1 = Noise anim
  43.                    2 = sine wave anim
  44.                 */
  45.  
  46. // noise settings
  47. static uint16_t NoiseDistanceJump = random16(12345);       // A random number for our noise generator.
  48. uint8_t AnimScale = 30;
  49.  
  50. //Sine settings this settings needs to be reviewed!
  51. uint8_t SineOnePhase = 0;
  52. uint8_t SineOneWaveLenght = 20;
  53. uint8_t SineOneAmplitude = 10;
  54. uint8_t SineTwoPhase = 0;
  55. uint8_t SineTwoWaveLenght = 30;
  56. uint8_t SineTwoAmplitude = 10;
  57.  
  58. DEFINE_GRADIENT_PALETTE( test_pl_bw ) {
  59.     0,  0,   0,   0,
  60.   255, 255, 255 ,255};
  61.  
  62.  DEFINE_GRADIENT_PALETTE( test_pl_r ) {
  63.     0, 255, 0,0,
  64.   255, 0, 0,255};
  65.  
  66. DEFINE_GRADIENT_PALETTE( test_pl_g ) {
  67.     0,0,255,0,
  68.   255,255,0,0};
  69.  
  70. DEFINE_GRADIENT_PALETTE( test_pl_b ) {
  71.     0,0, 0,255,
  72.   255,0, 255,0};
  73.  
  74. DEFINE_GRADIENT_PALETTE( test_pl_y) {
  75.     0, 255, 255,0,
  76.   255, 0,255,255};
  77.  
  78. DEFINE_GRADIENT_PALETTE( test_pl_c ) {
  79.     0,0,255,255,
  80.   255,255,0,255};
  81.  
  82. DEFINE_GRADIENT_PALETTE( test_pl_m ) {
  83.     0,255, 0,255,
  84.   255,255,255,0};
  85.  
  86. //defining starting palettes
  87. CRGBPalette16 currentPalette( test_pl_r ); // starting palette, fade from black
  88. CRGBPalette16 BwPalette ( test_pl_bw ); // starting palette, fade from black
  89.  
  90. // fill palettes.
  91. void AnimateAndFill() {
  92.   static uint8_t FillIndex;
  93.   static uint8_t ReversTimer = 8;
  94.   uint8_t AnimationIndex;
  95.   uint8_t AnimationAndSpeed;
  96.    // Effect speed global, nudges everything from left to right!
  97.   static int effectSpeed = 0;  //animation speed, This is not the speed you want to set. use SpeedIndex!
  98.  
  99.  
  100.   if (Reverse) {
  101.     effectSpeed = +effectSpeed; }
  102.   else {
  103.    effectSpeed = -effectSpeed; //making it go backwards.
  104.    }
  105.  
  106.  
  107.   for( int i = 0; i < NUM_LEDS; i++) {
  108.  
  109.   switch (FillMethod) {
  110.   case 0: FillIndex = StartIndex; break;
  111.   case 1: FillIndex = i * FillScale + StartIndex; break;
  112.   case 2: FillIndex = sin8(i*FillScale) + StartIndex; break;
  113.   case 3: FillIndex = inoise8(i* FillScale) + StartIndex; break;
  114.   // case 3: FillIndex =  inoise8(i*FillScale, NoiseDistanceJump+i*FillScale) % 255; break;
  115.   }
  116.  
  117.   switch (AnimationMethod) {
  118.   case 0: AnimationIndex = 0; break;
  119.   case 1: AnimationIndex = inoise8(i*AnimScale, NoiseDistanceJump+i*AnimScale); break;
  120.   case 2: AnimationIndex = sin8((i+SineOnePhase)*SineOneWaveLenght)*SineOneAmplitude; break;
  121.   }
  122.          
  123.  
  124.        if (BrightnessAnim) {
  125.        leds[i] = ColorFromPalette(currentPalette, FillIndex + AnimationIndex + effectSpeed, PaletteBrightness);  
  126.        leds[i] -= ColorFromPalette(BwPalette, i*2 +  AnimationIndex + effectSpeed, PaletteBrightness); }
  127.        /* This part, is inteded to separately animate brightness, by substracting a full black and white palette from the original palette.
  128.           It works, but i'm not entirely happy with this solution. However, any way I tried to animate PaletteBrightness the animation comes
  129.           out very blocky.
  130.           you might want to change the i*2 to 256 / NUM_LEDS or: uint16_t StaticScale = (256 + (NUM_LEDS - 1)) / NUM_LEDS;
  131.        */
  132.        else {
  133.        leds[i] = ColorFromPalette(currentPalette, FillIndex + AnimationIndex + effectSpeed, PaletteBrightness); break;
  134.      }
  135.   }
  136.  
  137.  //Speed is set by waiting a bit after every frame.
  138.   if (SpeedIndex) {
  139.    
  140.   uint8_t BeatSine = beatsin8(BeatSineBPM,1,BeatSineMax);
  141.   effectSpeed = abs(effectSpeed); //get the absolute value of effectspeed, in case its negative, cause we going backwards.
  142.  
  143.   switch (SpeedMethod) {
  144.     case 0: effectSpeed += 1; break;
  145.     case 1: effectSpeed += BeatSine; break;
  146.     }
  147.  
  148.   NoiseDistanceJump += BeatSine;
  149.   uint8_t Delay = 100 - SpeedIndex;
  150.   unsigned long Speed = millis();
  151.   while ( Speed + Delay > millis() ) {
  152.   yield(); //pondering existence.
  153.     }
  154.   }
  155.  
  156. }
  157.  
  158. void Control()
  159. {
  160. String inData;
  161.     while (Serial.available() > 0)
  162.     {
  163.         char recieved = Serial.read();
  164.         inData += recieved;
  165.  
  166.  
  167.        // Process message when new line character is recieved
  168.         if (recieved == '\n')
  169.         {
  170.             Serial.println("");
  171.       Serial.print("Arduino Received: ");
  172.             Serial.print(inData);
  173.  
  174.        // whole word commands
  175.  
  176.  
  177.         if(inData == "banim\n"){ // DON'T forget to add "\n" at the end of the string.
  178.           if (BrightnessAnim) {
  179.         BrightnessAnim = 0;}
  180.       else {
  181.         BrightnessAnim = 1;
  182.       }  
  183.           Serial.print("BrightnessAnim: " );
  184.           Serial.println( BrightnessAnim );
  185.         }  
  186.  
  187.             if(inData == "rand\n"){ // DON'T forget to add "\n" at the end of the string.
  188.           if (RandomReverse) {
  189.         RandomReverse = 0;}
  190.       else {
  191.         RandomReverse = 1;
  192.       }  
  193.           Serial.print("RandomReverse: " );
  194.           Serial.println( RandomReverse );
  195.         }
  196.          
  197.  
  198.       // commands with numbers
  199.    
  200.    
  201.         if(inData.startsWith("br")) {  
  202.       inData.remove(0,2);    
  203.       PaletteBrightness = inData.toInt();  
  204.       Serial.print("PaletteBrightness: ");
  205.       Serial.println( PaletteBrightness );
  206.       }
  207.    
  208.    
  209.         if(inData.startsWith("sp")) {  
  210.       inData.remove(0,2);  
  211.       SpeedIndex  = inData.toInt();
  212.       Serial.print("SpeedIndex : ");
  213.       Serial.println( SpeedIndex );
  214.       }
  215.    
  216.         if(inData.startsWith("fs")) {
  217.       inData.remove(0,2);  
  218.       FillScale   = inData.toInt();
  219.       Serial.print("FillScale  : ");
  220.       Serial.println( FillScale );
  221.       }
  222.    
  223.         if(inData.startsWith("fm")) {  
  224.       inData.remove(0,2);  
  225.       FillMethod   = inData.toInt();  
  226.       Serial.print("FillMethod  : ");
  227.       Serial.println( FillMethod );
  228.       }
  229.    
  230.         if(inData.startsWith("am")) {  
  231.       inData.remove(0,2);      
  232.       AnimationMethod  = inData.toInt();  
  233.       Serial.print("AnimationMethod : ");
  234.       Serial.println( AnimationMethod );
  235.       }
  236.  
  237.         if(inData.startsWith("as")) {  
  238.       inData.remove(0,2);    
  239.       AnimScale   = inData.toInt();  
  240.       Serial.print("AnimScale  : ");
  241.       Serial.println( AnimScale  );
  242.       }  
  243.  
  244.         if(inData.startsWith("bm")) {  
  245.       inData.remove(0,2);      
  246.       BeatSineMax   = inData.toInt();  
  247.       Serial.print("BeatSineMax  : ");
  248.       Serial.println( BeatSineMax  );
  249.       }
  250.  
  251.         if(inData.startsWith("bp")) {  
  252.       inData.remove(0,2);    
  253.       BeatSineBPM   = inData.toInt();  
  254.       Serial.print("BeatSineBPM  : ");
  255.       Serial.println( BeatSineBPM  );
  256.       }  
  257.  
  258.         if(inData.startsWith("sm")) {  
  259.       inData.remove(0,2);      
  260.       SpeedMethod   = inData.toInt();
  261.       Serial.print("SpeedMethod  : ");
  262.       Serial.println( SpeedMethod  );
  263.       }
  264.    
  265.        if(inData.startsWith("rv")) {  
  266.       inData.remove(0,2);      
  267.       Reverse   = inData.toInt();  
  268.       Serial.print("Reverse  : ");
  269.       Serial.println( Reverse  );
  270.       }
  271.    
  272.      if(inData.startsWith("np")) {  
  273.       inData.remove(0,2);    
  274.       uint8_t NextPalette   = inData.toInt();  
  275.       Serial.print("Next Palette : ");
  276.       Serial.println( NextPalette  );
  277.       switch (NextPalette) {
  278.     case 0: currentPalette = test_pl_r;
  279.     case 1: currentPalette = test_pl_g;
  280.     case 2: currentPalette = test_pl_b;
  281.     case 3: currentPalette = test_pl_c;
  282.     case 4: currentPalette = test_pl_m;
  283.     case 5: currentPalette = test_pl_y;
  284.     case 6: currentPalette = test_pl_bw;
  285.         }
  286.         }
  287.      
  288.          
  289.     inData = ""; // Clear recieved buffer
  290.         }
  291.     }  
  292. }
  293.  
  294. void setup() {
  295.   Serial.begin(9600);
  296.   FastLED.addLeds<WS2811, LED_PIN, BRG>(leds, NUM_LEDS);
  297.   FastLED.setCorrection( TypicalSMD5050 );
  298.   FastLED.setBrightness( 255 );
  299.   delay( 3000 ); // power-up safety delay
  300.    
  301.   } //end of setup
  302.  
  303. void loop()
  304. {
  305.   Control();
  306.    AnimateAndFill();
  307.       // FastLED.show(); fastled.delay already calling show.
  308.    FastLED.delay(1000 / UPDATES_PER_SECOND);  
  309. }
Advertisement
Add Comment
Please, Sign In to add comment