Advertisement
atuline

two_sin for Arduino and FastLED 2.1

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