Advertisement
atuline

three_sin_demo

Jan 30th, 2015
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1.  
  2. /* three_sin demo for FastLED 2.1 or greater
  3.  
  4. By: Andrew Tuline
  5.  
  6. Date: Oct, 2014
  7.  
  8. 3 sine waves, one for each colour. I'm already doing a lot with 2 sine waves, so I didn't take this far.
  9.  
  10. FastLED is available at https://github.com/FastLED/FastLED
  11.  
  12. Note: If you receive compile errors (as I have in the Stino add-on for Sublime Text), set the compiler to 'Full Compilation'.
  13.  
  14. */
  15.  
  16.  
  17. #include "FastLED.h"                                          // FastLED library. Preferably the latest copy of FastLED 2.1.
  18.  
  19. // Fixed definitions cannot change on the fly.
  20. #define LED_DT 12                                             // Serial data pin for WS2812B or WS2801
  21. #define LED_CK 11
  22. #define COLOR_ORDER BGR                                       // Are they RGB, GRB or what??
  23. #define LED_TYPE APA102                                       // What kind of strip are you using?
  24. #define NUM_LEDS  12                                           // Number of LED's
  25.  
  26. // Initialize changeable global variables.
  27. uint8_t max_bright = 64;                                      // Overall brightness definition. It can be changed on the fly.
  28.  
  29. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  30.  
  31.  
  32. // Initialize global variables for sequences
  33. uint8_t thisdelay = 20;                                       // A delay value for the sequence(s)
  34.  
  35. int wave1=0;                                            // Current phase is calculated.
  36. int wave2=0;
  37. int wave3=0;
  38.  
  39. uint8_t inc1 = 2;                                       // Phase shift. Keep it low, as this is the speed at which the waves move.
  40. uint8_t inc2 = 1;
  41. uint8_t inc3 = -2;
  42.  
  43. uint8_t lvl1 = 80;                                      // Any result below this value is displayed as 0.
  44. uint8_t lvl2 = 80;
  45. uint8_t lvl3 = 80;
  46.  
  47. uint8_t mul1 = 20;                                      // Frequency, thus the distance between waves
  48. uint8_t mul2 = 25;
  49. uint8_t mul3 = 22;
  50.  
  51.  
  52.  
  53. void setup() {
  54.   Serial.begin(57600);
  55.   LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);
  56.   FastLED.setBrightness(max_bright);
  57.   set_max_power_in_volts_and_milliamps(5, 500);               // FastLED 2.1 Power management set at 5V, 500mA
  58.  
  59.   random16_set_seed(4832);                                     // Awesome randomizer (which we don't yet need here.)
  60.   random16_add_entropy(analogRead(2));
  61.  
  62. } // setup()
  63.  
  64.  
  65.  
  66. void loop () {
  67.   ChangeMe();
  68.   three_sin();                                                  // Improved method of using non-blocking delay
  69.   show_at_max_brightness_for_power();                         // Power managed display of LED's
  70.   delay_at_max_brightness_for_power(thisdelay*2.5);
  71.   Serial.println(LEDS.getFPS());
  72. } // loop()
  73.  
  74.  
  75.  
  76. void three_sin() {
  77.   wave1 += inc1;
  78.   wave2 += inc2;
  79.   wave3 += inc3;
  80.   for (int k=0; k<NUM_LEDS; k++) {
  81.     leds[k].r = qsub8(sin8(mul1*k + wave1/128), lvl1);         // Another fixed frequency, variable phase sine wave with lowered level
  82.     leds[k].g = qsub8(sin8(mul2*k + wave2/128), lvl2);         // A fixed frequency, variable phase sine wave with lowered level
  83.     leds[k].b = qsub8(sin8(mul3*k + wave3/128), lvl3);         // A fixed frequency, variable phase sine wave with lowered level
  84.   }
  85. } // three_sin()
  86.  
  87.  
  88.  
  89. void ChangeMe()
  90. {
  91.   uint8_t secondHand = (millis() / 1000) % 60;                // Increase this if you want a longer demo.
  92.   static uint8_t lastSecond = 99;                             // Static variable, means it's only defined once. This is our 'debounce' variable.
  93.  
  94.   // You can change variables, but remember to set them back in the next demo, or they will stay as is.
  95.   if( lastSecond != secondHand) {
  96.     lastSecond = secondHand;
  97.     if( secondHand ==  0)  {thisdelay = 8; mul1 = 20; mul2 = 25; mul3 = 22; lvl1 = 80; lvl2 = 80; lvl3 = 80; inc1 = 1; inc2 = 1; inc3 = -1;}
  98. //  if( secondHand == 5)   {mul1 = 5; mul2 = 8; mul3 = 7;}
  99. //  if( secondHand == 10)  {thisdelay = 20; lvl1 = 220; lvl2 = 220; lvl3 = 220;}
  100.     if( secondHand == 15)  {}
  101.     if( secondHand == 20)  {}
  102.     if( secondHand == 25)  {}
  103.     if( secondHand == 30)  {}
  104.     if( secondHand == 35)  {}
  105.     if( secondHand == 40)  {}
  106.     if( secondHand == 45)  {}
  107.     if( secondHand == 50)  {}
  108.     if( secondHand == 55)  {}
  109.   } // if lastSecond
  110. } // ChangeMe()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement