Advertisement
atuline

fill_grad_array

Sep 12th, 2019
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.42 KB | None | 0 0
  1. /* fill_grad_array
  2.  *
  3.  * By: Andrew Tuline
  4.  *
  5.  * Date: Sept, 2019
  6.  *
  7.  * This has two gradient fills.
  8.  *
  9.  * 1) fill_gradient_array moves the array in a circular pattern around the strip in a single direction continuously.
  10.  *
  11.  * 2) fill_gradient fills the gradient across the entire strip with varying hues.
  12.  *
  13.  * It uses FastLED's:
  14.  *
  15.  * - fill_gradient() function to blend between two colours and to vary the colours.
  16.  * - beatsin8() to provide a time based smooth varying input to blend().
  17.  * - fill_pal to provide a time based smooth varying palette around the strip.
  18.  *
  19.  * An issue when using the fill_gradient is which way you want the gradient to sweep around the colour wheel. Options are:
  20.  *
  21.  * FORWARD_HUES: hue always goes clockwise
  22.  * BACKWARD_HUES: hue always goes counter-clockwise
  23.  * SHORTEST_HUES: hue goes whichever way is shortest and is the DEFAULT
  24.  * LONGEST_HUES: hue goes whichever way is longest
  25.  *
  26.  * When the start and end colours are animated, the sweep will flip in the other direction at certain points, thus causing a disruption of the smooth gradient.
  27.  *
  28.  * This routine demonstrates one method to animate the start/end hues and to ensure the fill_gradient remains consistently smooth.
  29.  *
  30.  */
  31.  
  32. #include "FastLED.h"                                          // FastLED library. Preferably the latest copy of FastLED 2.1.
  33.  
  34. #if FASTLED_VERSION < 3001000
  35. #error "Requires FastLED 3.1 or later; check github for latest code."
  36. #endif
  37.  
  38. // Fixed global definitions.
  39. #define LED_DT 12                                             // Data pin to connect to the strip.
  40. #define LED_CK 11
  41. #define COLOR_ORDER GRB                                       // Are they RGB, GRB or what??
  42. #define LED_TYPE WS2812                                       // Don't forget to change LEDS.addLeds
  43. #define NUM_LEDS 30                                           // Number of LED's.
  44.  
  45. // Initialize changeable global variables.
  46. uint8_t max_bright = 128;                                      // Overall brightness definition. It can be changed on the fly.
  47.  
  48. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  49. struct CRGB grad[NUM_LEDS];
  50.  
  51. CRGBPalette16 palette = PartyColors_p;
  52.  
  53. void setup() {
  54.  
  55.   Serial.begin(115200);                                        // Initialize serial port for debugging.
  56.   delay(1000);                                                // Soft startup to ease the flow of electrons.
  57.  
  58. //  LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2801 or APA102
  59.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2801 or APA102
  60.  
  61.   set_max_power_in_volts_and_milliamps(5, 500);               // This is used by the power management functionality and is currently set at 5V, 500mA.
  62.   FastLED.setBrightness(max_bright);
  63.  
  64.   TBlendType currentBlending = LINEARBLEND;
  65.  
  66.   fill_gradient(grad, NUM_LEDS/3, CHSV(0,255,255), CHSV(128,255,255));
  67.  
  68. } // setup()
  69.  
  70.  
  71.  
  72. void loop () {
  73.  
  74.   fill_pal();                             // A palette version, which I prefer.
  75. //  fill_grad();                          // A gradient that doesn't span between NUM_LEDS and 0.
  76. //  fill_grad_array();                    // A gradient that uses a 2nd array to be able to span anywhere.
  77.  
  78.   FastLED.show();                         // Power managed display of LED's.
  79.  
  80. } // loop()
  81.  
  82.  
  83.  
  84. void fill_pal() {
  85.  
  86.   const int len = NUM_LEDS/4;
  87.  
  88.   uint8_t offset = millis()/50%256;
  89.   fadeToBlackBy(leds,NUM_LEDS,4);
  90.   for (uint16_t i = 0; i < len; i++) {
  91.       uint8_t locn = (millis()/50 + i)%NUM_LEDS;
  92.       leds[locn] = ColorFromPalette(palette, (i*255)/NUM_LEDS + offset);
  93.   }
  94. } // fill_pal()
  95.  
  96.  
  97.  
  98. void fill_grad_array() {
  99.  
  100.   fadeToBlackBy(leds, NUM_LEDS,4);
  101.  
  102.   uint8_t posn = millis()/50;
  103.   posn = posn % NUM_LEDS;
  104.   Serial.println(posn);
  105.  
  106.   for (uint8_t i = 0; i < NUM_LEDS/3; i++) {
  107.     uint8_t locn = (posn + i) %NUM_LEDS;
  108.     leds[locn] = grad[i];    
  109.   }
  110. } // fill_grad_array()
  111.  
  112.  
  113.  
  114. void fill_grad() {
  115.  
  116.   uint8_t starthue = beatsin8(5, 0, 255);
  117.   uint8_t endhue = beatsin8(7, 0, 255);
  118.  
  119.   if (starthue < endhue) {
  120.     fill_gradient(leds, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), FORWARD_HUES);    // If we don't have this, the colour fill will flip around.
  121.   } else {
  122.     fill_gradient(leds, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), BACKWARD_HUES);
  123.   }
  124.  
  125. } // fill_grad()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement