LDmicro0.1 CYCLE=10000 CRYSTAL=4000000 BAUD=2400 IO LIST Yout at 0 Yout_0 at 0 Ain at 0 END PROGRAM RUNG COMMENT The parameter alpha determines the time constant of a low-pass filter. It\r\ngoes from 1 to 20, where 1 is the slowest response, and 20 is the fastest. END RUNG MOVE alpha 2 END RUNG READ_ADC Ain END RUNG GRT Ain 500 COIL Yout_0 0 0 0 END RUNG COMMENT Based on the analog input Ain, we compute a low-pass filtered version of\r\nthat signal, in alp. END RUNG PARALLEL SUB onemalpha 20 alpha MUL a Ain alpha MUL b alp onemalpha ADD c a b ADD d c 10 DIV alp d 20 END END RUNG COMMENT We take the absolute value of the difference between Ain and alp; so we\r\nrespond equally to increasing or decreasing changes. END RUNG PARALLEL SERIES GRT alp Ain SUB diff alp Ain END SERIES LEQ alp Ain SUB diff Ain alp END END END RUNG COMMENT We then compare that difference against our threshold, with some hysteresis.\r\nSmaller numbers here would require the value to settle to a narrower range. END RUNG PARALLEL SERIES CONTACTS Yout_0 0 GRT diff 100 COIL Rsteady 0 0 1 END SERIES CONTACTS Yout_0 0 LES diff 50 COIL Rsteady 0 1 0 END END END RUNG COMMENT And finally, we put a turn-on delay on our output, to avoid e.g. glitching\r\nwhen the input changes sign, but still doesn't settle. END RUNG CONTACTS Rsteady 0 TON Tnew 100000 COIL Yout 0 0 0 END