Lineary animates an internal value toward target value `TARG` with a rate `RATE`. Use the node to smoothen LED switching, motor starting, or servo angular position update.
fade
@/fade
Lineary animates an internal value toward target value `TARG` with a rate `RATE`. Use the node to smoothen LED switching, motor starting, or servo angular position update.
TARGnumber
The target value to strive for.
RATEnumber
Speed rate in units per second.
UPDpulse
Triggers an update (i.e. recalculation) of `OUT` value. Keep the value set to `Continuously` to achieve the most smooth animation possible.
OUTnumber
The current animation value.
C++ implementation
#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_UPD
node {
TimeMs lastUpdateTime;
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx)) {
return;
}
TimeMs now = transactionTime();
Number target = getValue<input_TARG>(ctx);
Number position = getValue<output_OUT>(ctx);
if (target == position) {
// Already done. Store timestamp anyway so that an animation to a new
// value would not jump at the first update
lastUpdateTime = now;
return;
}
Number rate = getValue<input_RATE>(ctx);
TimeMs dtMs = now - lastUpdateTime;
Number step = (Number)dtMs / 1000. * rate;
if (target > position) {
position = min(target, position + step);
} else {
position = max(target, position - step);
}
emitValue<output_OUT>(ctx, position);
lastUpdateTime = now;
}
}