Advertisement
atuline

sinelon_subpixel.ino

Oct 31st, 2021
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. /* Poor man's 1D subpixel rendering.
  2.  
  3.    By: Andrew Tuline
  4.  
  5.    Date: Oct, 2021
  6.  
  7.   Supports partial brightness both before and after the 'current' led.
  8.  
  9. */
  10.  
  11. #include <FastLED.h>
  12.  
  13. #define LED_DT 12
  14. #define NUM_LEDS 60
  15.  
  16. struct CRGB leds[NUM_LEDS];
  17.  
  18. void setup() {
  19.   Serial.begin(115200);
  20.   LEDS.addLeds<WS2812, LED_DT, GRB>(leds, NUM_LEDS);
  21. } // setup()
  22.  
  23.  
  24. void loop () {
  25.  
  26.   float t = (sin((float)millis() / 1000.) + 1.0) / 2.;            // Make a slow sine wave and convert output range from -1.0 and 1.0 to between 0 and 1.0.
  27.   t = t * (float)NUM_LEDS;                                        // Now map to the length of the strand.
  28.  
  29.   for (int i = 0; i < NUM_LEDS; i++)
  30.   {
  31.     float diff = abs(t - (float)i);                               // Get difference between t and current location. Greater distance = lower brightness.
  32.     if (diff > 2.0) diff = 2.0;                                   // Let's not overflow.
  33.     float bri = 256 - diff * 128;                                 // Scale the brightness to up to 255. Closer = brighter.
  34.     leds[i] = CHSV(0, 255, (uint8_t)bri);
  35.   }
  36.  
  37.   FastLED.show();
  38. } // loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement