Debounces a number value. The `OUT` value would change only after a period of at least `Ts` seconds while which the input state `ST` has not changed.
debounce(number)
@/debounce(number)
Debounces a number value. The `OUT` value would change only after a period of at least `Ts` seconds while which the input state `ST` has not changed.
node {
Number state = 0;
void evaluate(Context ctx) {
Number x = getValue<input_ST>(ctx);
if (x != state) {
state = x;
TimeMs dt = getValue<input_Ts>(ctx) * 1000;
setTimeout(ctx, dt);
}
if (isTimedOut(ctx)) {
emitValue<output_OUT>(ctx, x);
}
}
}
__time(ms) | ST | Ts | OUT |
0 | 0 | 1 | 0 |
1000 | 1 | 1 | 0 |
2001 | 1 | 1 | 1 |
2002 | 2 | 1 | 1 |
3003 | 2 | 1 | 2 |
5000 | 2 | 2 | 2 |
5001 | 3 | 2 | 2 |
6000 | 3 | 0.5 | 2 |
7002 | 3 | 0.5 | 3 |
8000 | 5 | 0.1 | 3 |
8101 | 5 | 0.1 | 5 |