atuline

two_sin for Arduino and FastLED 2.1

Sep 23rd, 2014
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.49 KB | None | 0 0
  1. /*
  2. #
  3. # two_sin
  4. #
  5. # By: Andrew Tuline
  6. # www.tuline.com
  7. #
  8. # Date: Sep, 2014
  9. #
  10. # A demo showing the flexibility of the two_sin routine. Lots of variables can be changed for some awesome effects. A little code can go a long way.
  11. #
  12. # Oh, and those twinkles for Mark Kriegsman are still there.
  13. #
  14. #
  15. # FastLED 2.1 is available at https://github.com/FastLED/FastLED/tree/FastLED2.1
  16. #
  17. # Note: If you receive compile errors (as I have in the Stino add-on for Sublime Text), set the compiler to 'Full Compilation'.
  18. #
  19. # There are a number of things to update, such as removing the speed code, as I'm now using a signed variable, so you're seeing a preliminary version.
  20. */
  21.  
  22. #define qsub(x, b)  ((x>b)?wavebright:0)                      // A digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
  23. // #define qsub(x, b)  ((x>b)?x-b:0)                          // Unsigned subtraction macro. if result <0, then => 0
  24.  
  25.  
  26. #include <FastLED.h>
  27.  
  28. #define LED_PIN     13
  29. #define NUM_LEDS    24
  30. #define BRIGHTNESS  255                                       // This is the overall brightness
  31. #define LED_TYPE    WS2811
  32. #define COLOR_ORDER GRB
  33. CRGB leds[NUM_LEDS];
  34.  
  35.  
  36. // Most of these variables can be mucked around with. Better yet, add some form of variable input or routine to change them on the fly. 1970's here I come. .
  37. //
  38.  
  39. // Don't forget to update resetvar() definitions if you change these.
  40. uint8_t wavebright = 128;                                     // You can change the brightness of the waves/bars rolling across the screen. Best to make them not as bright as the sparkles.
  41. uint8_t thishue = 0;                                          // You can change the starting hue value for the first wave.
  42. uint8_t thathue = 140;                                        // You can change the starting hue for other wave.
  43. uint8_t thisrot = 0;                                          // You can change how quickly the hue rotates for this wave. Currently 0.
  44. uint8_t thatrot = 0;                                          // You can change how quickly the hue rotates for the other wave. Currently 0.
  45. uint8_t allsat = 255;                                         // I like 'em fully saturated with colour.
  46. uint8_t alldir = 0;                                           // You can change direction.
  47. int8_t thisspeed = 4;                                         // You can change the speed, and use negative values.
  48. int8_t thatspeed = 4;                                         // You can change the speed, and use negative values.
  49. uint8_t allfreq = 32;                                         // You can change the frequency, thus overall width of bars.
  50. int thisphase = 0;                                            // Phase change value gets calculated.
  51. int thatphase = 0;                                            // Phase change value gets calculated.
  52. uint8_t thiscutoff = 192;                                     // You can change the cutoff value to display this wave. Lower value = longer wave.
  53. uint8_t thatcutoff = 192;                                     // You can change the cutoff value to display that wave. Lower value = longer wave.
  54. int loopdelay = 10;                                           // You can change the delay. Also you can change the allspeed variable above.
  55. uint8_t twinkrun = 1;                                         // Enable/disable twinkles.
  56. // End of resetvar() redefinitions.
  57.  
  58.  
  59. typedef struct {                                              // Define a structure for the twinkles that get overlaid on the moving waves.
  60.       int twinkled;                                           // Supports a long strand of LED's.
  61.       int twinkbright;                                        // Defined as 'int', so that we can trigger change on a negative brightness value.
  62.   }  twinks;
  63.  
  64. #define numtwinks 4                                           // You can change the number of twinkles.
  65. twinks mytwinks[numtwinks];                                   // The structure is called mytwinks.
  66.  
  67.  
  68.  
  69. void setup() {
  70.   delay(1000);                                                 // Power-up safety delay or something like that.
  71.   Serial.begin(57600);
  72.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  73.   set_max_power_in_volts_and_milliamps(5, 500);               // FastLED 2.1 Power management set at 5V, 500mA
  74.   FastLED.setBrightness( BRIGHTNESS );
  75. } // setup()
  76.  
  77.  
  78.  
  79. void loop()
  80. {
  81.   ChangeMe();                                                 // Muck those variable around.
  82.   two_sin();                                                 // Simple call to the routine.
  83.   if(twinkrun == 1) twinkle();                                // You can keep or lose the twinkles.
  84.   show_at_max_brightness_for_power();
  85.   delay_at_max_brightness_for_power(loopdelay*2.5);
  86.   LEDS.countFPS();
  87. } // loop()
  88.  
  89.  
  90.  
  91. void ChangeMe()
  92. {
  93.   uint8_t secondHand = (millis() / 1000) % 60;                // Increase this if you want a longer demo.
  94.   static uint8_t lastSecond = 99;                             // Static variable, means it's only defined once. This is our 'debounce' variable.
  95.  
  96.   // You can change variables, but remember to set them back in the next demo, or they will stay as is.
  97.   if( lastSecond != secondHand) {
  98.     lastSecond = secondHand;
  99.     if( secondHand ==  0) {thisrot = 1; thatrot = 1;}                                             // Both rotating hues
  100.     if( secondHand == 5)  {thisrot = 0;}                                                          // Just 1 rotating hue
  101.     if( secondHand == 10)  {thatrot = 0; thishue = 255; thathue = 255;}                           // No rotating hues, all red.
  102.     if( secondHand == 15)  {twinkrun = 0;}                                                        // Enough with the damn twinkles.
  103.     if( secondHand == 20)  {allfreq = 16; thathue = 128;}                                         // Time to make a wider bar.
  104.     if( secondHand == 25)  {thiscutoff = 96; thatcutoff = 240;}                                   // Change width of bars.
  105.     if( secondHand == 30)  {thiscutoff = 96; thatcutoff = 96; thisrot = 1;}                       // Make those bars overlap, and rotate a hue
  106.     if( secondHand == 35)  {alldir = 1;}                                                          // Change the direction.
  107.     if( secondHand == 40)  {thiscutoff = 128; thatcutoff = 128; wavebright = 64; twinkrun = 1;}   // Yet more changes
  108.     if( secondHand == 45)  {wavebright = 128; twinkrun = 0; thisspeed = 3;}                       // Now, we change speeds.
  109.     if( secondHand == 50)  {alldir = 0; twinkrun = 0; thatspeed = -3;}                            // Opposite directions
  110.     if( secondHand == 55)  {resetvar(); }                                                         // Getting complicated, let's reset the variables.
  111.   }
  112. } // ChangeMe()
  113.  
  114.  
  115.  
  116. void two_sin() {                                                               // This is the heart of this program. Sure is short.
  117.  
  118.   if (alldir == 0)
  119.     {thisphase+=thisspeed; thatphase+=thatspeed;}                               // You can change direction and speed individually.
  120.   else
  121.     {thisphase-=thisspeed; thatphase-=thatspeed;}  
  122.  
  123.     thishue = thishue + thisrot;                                                // Hue rotation is fun for thiswave.
  124.     thathue = thathue + thatrot;                                                // It's also fun for thatwave.
  125.  
  126.   for (int k=0; k<NUM_LEDS-1; k++) {
  127.     int thisbright = qsub(cubicwave8((k*allfreq)+thisphase), thiscutoff);       // qsub sets a minimum value called thiscutoff. If < thiscutoff, then bright = 0. Otherwise, bright = 128 (as defined in qsub)..
  128.     int thatbright = qsub(cubicwave8((k*allfreq)+128+thatphase), thatcutoff);   // This wave is 180 degrees out of phase (with the value of 128).
  129.  
  130.     leds[k] = CHSV(thishue, allsat, thisbright);                                // Assigning hues and brightness to the led array.
  131.     leds[k] += CHSV(thathue, allsat, thatbright);                      
  132.   }
  133. } // two_sin()
  134.  
  135.  
  136.  
  137.  
  138. void twinkle() {                                                            // This has been added for Mark Kriegsman.
  139.   for (int i = 0; i < numtwinks; i++) {
  140.     if (mytwinks[i].twinkbright <0) {
  141.       mytwinks[i].twinkled = random8(0, NUM_LEDS-1);
  142.       mytwinks[i].twinkbright = random8(220, 255);
  143.     }
  144.     leds[mytwinks[i].twinkled] = CHSV(80, 120, mytwinks[i].twinkbright);    // Trying to make a soft white twinkle
  145.     mytwinks[i].twinkbright -= 8;
  146.   }
  147. } // twinkle()
  148.  
  149.  
  150. void resetvar() {                                               // Reset the variable back to the beginning.
  151.   wavebright = 128;                                     // You can change the brightness of the waves/bars rolling across the screen. Best to make them not as bright as the sparkles.
  152.   thishue = 0;                                          // You can change the starting hue value for the first wave.
  153.   thathue = 140;                                        // You can change the starting hue for other wave.
  154.   thisrot = 0;                                          // You can change how quickly the hue rotates for this wave. Currently 0.
  155.   thatrot = 0;                                          // You can change how quickly the hue rotates for the other wave. Currently 0.
  156.   allsat = 255;                                         // I like 'em fully saturated with colour.
  157.   alldir = 0;                                           // You can change direction.
  158.   thisspeed = 4;                                         // You can change the speed, and use negative values.
  159.   thatspeed = 4;                                         // You can change the speed, and use negative values.
  160.   allfreq = 32;                                         // You can change the frequency, thus overall width of bars.
  161.   thisphase = 0;                                            // Phase change value gets calculated.
  162.   thatphase = 0;                                            // Phase change value gets calculated.
  163.   thiscutoff = 192;                                     // You can change the cutoff value to display this wave. Lower value = longer wave.
  164.   thatcutoff = 192;                                     // You can change the cutoff value to display that wave. Lower value = longer wave.
  165.   loopdelay = 10;                                           // You can change the delay. Also you can change the allspeed variable above.
  166.   twinkrun = 1;                                         // Enable/disable twinkles.
  167. } // resetvar()
Advertisement
Add Comment
Please, Sign In to add comment