 # Hi there!  I am in the middle of making a project with this fantastic

(Javier Soto Morras) #1

Hi there!
I am in the middle of making a project with this fantastic library, and I got caught in a few details. I am reading several inputs, which move around a 2 dimensional strip. These inputs sometimes make big gaps, going from 20 to 100.
At the moment, I am doing a funcions like this:

if (oldVal>Val) { val++ }
if (oldVal<Val) { val-- }

This doesnt´t work at the moment very well, I wonder if there is any math function already implemented in the library that I could use to make changes progressive?

(Mark Kriegsman) #2

Hi, and that’s a great question – a need for that sort of ‘damped following’ shows up more often than you might expect!

There’s no function like this built into FastLED at the moment. I usually use something like this as my helper function:

uint8_t moveToward8( uint8_t current, uint8_t target, uint8_t maxmove)
{
// If current is already at target, just return it.
if( current == target) {
return current;
}

uint8_t delta;
uint8_t newvalue;

if( current < target) {

``````// current is less than target,
// so compute amount to add;
delta = target - current;
delta = scale8( delta, maxmove);
delta = delta + 1; // move by at least 1
newvalue = current + delta;
``````

} else {

``````// current is greater than target,
// so computer amount to subtract
delta = current - target;
delta = scale8( delta, maxmove);
delta = delta + 1; // move by at least 1
newvalue = current - delta;
``````

}

return newvalue;
}

Then to use it, do this:

val = moveToward8( val, targetValue, 64);

If val is close to targetValue, it will move slowly. If val is far away targetValue, it will move more quickly. The “64” number there controls the maximum speed; I’d try different values between maybe 10-100, and see what you like.

Let us know if this helps, or what you wind up using instead!

(UPDATE: fixed a typo - oops!)

(Peter Buelow) #3

Hey, cool. My non twinkle Christmas lights might be able to use this. Great idea. Thanks.