In examples of FastLED library, there's the animation of a dot sweeping back and forth, with fading trails (file "DemoReel100", animation "sinelon"). How can I alter the code so instead of moving back, the dot begins at LED #1 again; so when using a LED ring, then the movement is continuous and circular. Since the code will be part of a more complex project with reading of button states etc, the animation code has to be non-blocking, i.e. delay() is not an option.

void sinelon()
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16( 13, 0, NUM_LEDS-1 );
leds[pos] += CHSV( gHue, 255, 192);
  • Can't follow you. I think there's no explicit code for back movement, it's just the natural thing of a sine wave: it has a sign reversal in it.
    – Madamadam
    Jan 6, 2020 at 1:31
  • Oh, missunderstanding, my example animation is called "sinelon" and resides in example file named "DemoReel100".
    – Madamadam
    Jan 6, 2020 at 1:59
  • replace the beatsin16( 13, 0, NUM_LEDS-1 ); with a function that returns a value that increments on successive calls to the function ... starts at 0 and resets to 0 when it reaches NUM_LEDS
    – jsotola
    Jan 6, 2020 at 2:41

1 Answer 1


If you look at the surrounding methods, you'll see that 'fadeToBlackBy' will give a fading effect on anything you feed in. You'll want to locate method 'beatsin16' and then edit it yourself so that it doesn't bounce back but restarts the counter at the first LED. (hint: phase_offset)

You'll find the code snippet you want to copy in lib8tion.h

/// beatsin16 generates a 16-bit sine wave at a given BPM,
///           that oscillates within a given range.
LIB8STATIC uint16_t beatsin16( accum88 beats_per_minute, uint16_t lowest = 0, uint16_t highest = 65535,
                               uint32_t timebase = 0, uint16_t phase_offset = 0)
    uint16_t beat = beat16( beats_per_minute, timebase);
    uint16_t beatsin = (sin16( beat + phase_offset) + 32768);
    uint16_t rangewidth = highest - lowest;
    uint16_t scaledbeat = scale16( beatsin, rangewidth);
    uint16_t result = lowest + scaledbeat;
    return result;
  • Thank you, altering the beatsin16 method is a good idea, but fiddeling with phase didn't work. I am not very savvy concerning the math behind sine function. Could you please give more information?
    – Madamadam
    Jan 6, 2020 at 11:10
  • You shouldn't be altering the function as much as duplicating it and calling it 'beatshoot16' or similar The sine function is just what gives the light the 'acceleration' and 'deceleration' motion. if you don't need that it might be simpler for you to rewrite the function to just have a light beeping on incrementally through the strip With regards to phase offset, google images for "sine wave phase offset". Changing the offset value just changes the timing, what you want is to restart the sine wave if the offset is in a certain range (when it is pi to 2pi, change it to zero to pi).
    – What The J
    Jan 7, 2020 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.