Easy and Efficient Debounce Timer?
As part of my code, I monitor PLC tags to generate alerts. To limit the number of alerts logged while a sensor reading is on the edge of generating an alert, I wrote a some code that act like a debounce timer. The timer starts when the value either hits LoLo or HiHi alert states. If the value leaves that state before the timer times out, then the timer is reset and doesn't start until it hits the alert state again. Below is the code I use for this.
My question is: Is there a better way of doing this in ThingWorx? Perhaps one that is more efficient than what I wrote?
var time = me.AlertDebounce * 10; /* AlertDebounce is in seconds */
if ((me.Device_Value < me.LoLo_Alarm_SP) || (me.Device_Value > me.HiHi_Alarm_SP)) {
var trigger = true;
/* If trigger remains true after for loop completes, then generate alarm. */
/* If Device_Value leaves alarm state setpoint, trigger goes false and */
/* time is copied to x, causing the for loop to finish. */
for (var x = 0; x < time; x++) {
pause(100); /* for loop paused for 100 milliseconds (0.1 seconds) */
if ((me.Device_Value < me.LoLo_Alarm_SP) || (me.Device_Value > me.HiHi_Alarm_SP)) {
trigger = true;
} else {
trigger = false;
x = time;
}
}
}

