Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. /* three_sin_pal_demo
  2. *
  3. * By: Andrew Tuline
  4. *
  5. * Date: March, 2015
  6. *
  7. * 3 sine waves, one for each colour. I didn't take this far, but you could change the beat frequency and so on. . .
  8. *
  9. */
  10.  
  11.  
  12. #include "FastLED.h" // FastLED library. Preferably the latest copy of FastLED 2.1.
  13.  
  14. #if FASTLED_VERSION < 3001000
  15. #error "Requires FastLED 3.1 or later; check github for latest code."
  16. #endif
  17.  
  18. // Fixed definitions cannot change on the fly.
  19. #define LED_DT 12 // Data pin to connect to the strip.
  20. #define LED_CK 11 // Clock pin for WS2801 or APA102.
  21. #define COLOR_ORDER BGR // It's GRB for WS2812 and BGR for APA102.
  22. #define LED_TYPE APA102 // Using APA102, WS2812, WS2801. Don't forget to modify LEDS.addLeds to suit.
  23. #define NUM_LEDS 60 // Number of LED's.
  24.  
  25. // Initialize changeable global variables.
  26. uint8_t max_bright = 128; // Overall brightness definition. It can be changed on the fly.
  27.  
  28. struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
  29.  
  30.  
  31. // Initialize global variables for sequences
  32. uint8_t thisdelay = 20; // A delay value for the sequence(s)
  33.  
  34. int wave1=0; // Current phase is calculated.
  35. int wave2=0;
  36. int wave3=0;
  37.  
  38. uint8_t lvl1 = 80; // Any result below this value is displayed as 0.
  39. uint8_t lvl2 = 80;
  40. uint8_t lvl3 = 80;
  41.  
  42. uint8_t mul1 = 7; // Frequency, thus the distance between waves
  43. uint8_t mul2 = 6;
  44. uint8_t mul3 = 5;
  45.  
  46.  
  47. CRGBPalette16 currentPalette(CRGB::Black);
  48. CRGBPalette16 targetPalette(PartyColors_p);
  49.  
  50.  
  51.  
  52. void setup() {
  53.  
  54. Serial.begin(115200); // Initialize serial port for debugging.
  55. delay(1000); // Soft startup to ease the flow of electrons.
  56.  
  57. // FastLED.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812B
  58. FastLED.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2801 or APA102
  59.  
  60. FastLED.setBrightness(max_bright);
  61. set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA
  62.  
  63. random16_set_seed(4832); // Awesome randomizer (which we don't yet need here.)
  64. random16_add_entropy(analogRead(2));
  65.  
  66. } // setup()
  67.  
  68.  
  69.  
  70. void loop () {
  71.  
  72. ChangeMe();
  73.  
  74. uint8_t maxChanges = 24;
  75.  
  76. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
  77. nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
  78. three_sin(); // Improved method of using non-blocking delay
  79. }
  80.  
  81. FastLED.show();
  82.  
  83. } // loop()
  84.  
  85.  
  86.  
  87. void three_sin() {
  88.  
  89. wave1 += beatsin8(10,-4,4);
  90. wave2 += beatsin8(15,-2,2);
  91. wave3 += beatsin8(12,-3, 3);
  92.  
  93. for (int k=0; k<NUM_LEDS; k++) {
  94. uint8_t tmp = sin8(mul1*k + wave1) + sin8(mul1*k + wave2) + sin8(mul1*k + wave3);
  95. leds[k] = ColorFromPalette(currentPalette, tmp, 255);
  96. }
  97.  
  98. } // three_sin()
  99.  
  100.  
  101.  
  102. void ChangeMe() {
  103.  
  104. uint8_t secondHand = (millis() / 1000) % 60;
  105. static uint8_t lastSecond = 99;
  106.  
  107. if( lastSecond != secondHand) {
  108. lastSecond = secondHand;
  109. CRGB p = CHSV( HUE_PURPLE, 255, 255);
  110. CRGB g = CHSV( HUE_GREEN, 255, 255);
  111. CRGB u = CHSV( HUE_BLUE, 255, 255);
  112. CRGB b = CRGB::Black;
  113. CRGB w = CRGB::White;
  114.  
  115. switch(secondHand) {
  116. case 0: targetPalette = RainbowColors_p; break;
  117. case 5: targetPalette = CRGBPalette16( u,u,b,b, p,p,b,b, u,u,b,b, p,p,b,b); break;
  118. case 10: targetPalette = OceanColors_p; break;
  119. case 15: targetPalette = CloudColors_p; break;
  120. case 20: targetPalette = LavaColors_p; break;
  121. case 25: targetPalette = ForestColors_p; break;
  122. case 30: targetPalette = PartyColors_p; break;
  123. case 35: targetPalette = CRGBPalette16( b,b,b,w, b,b,b,w, b,b,b,w, b,b,b,w); break;
  124. case 40: targetPalette = CRGBPalette16( u,u,u,w, u,u,u,w, u,u,u,w, u,u,u,w); break;
  125. case 45: targetPalette = CRGBPalette16( u,p,u,w, p,u,u,w, u,g,u,w, u,p,u,w); break;
  126. case 50: targetPalette = CloudColors_p; break;
  127. case 55: targetPalette = CRGBPalette16( u,u,u,w, u,u,p,p, u,p,p,p, u,p,p,w); break;
  128. case 60: break;
  129. }
  130. }
  131.  
  132. } // ChangeMe()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement