The idea was to setup a simple counter.
If there is no more counting pulse after a set time, the counter is reset to 0 at the next counting pulse.
Everything works perfectly, except when the counter is automatically set to 0, there have to be 2 counting pulses before going to state 1, there should be only one because the counter is at 0.
FUNCTION_BLOCKMyCounterVAR_INPUTxIn: BOOL; // Count on rising edge of InputdiDelayMS: DINT; // Delay before reset in milisecondsxReset: BOOL; // Reset the counter to zero on inputEND_VARVAR_OUTPUTdiCount: DINT; // Internal CounterEND_VARVARtDelay: TON; // TimerMyTrig: R_TRIG; // Trigger for inputEND_VAR=============================================//RisingEdgeintheInputMyTrig(CLK:=xIn);IFMyTrig.QTHENIFtDelay.QTHEN//Ifcountisalready0thenwecanmakeit1IFdiCount=0THENdiCount :=1;ELSEdiCount :=0;END_IFELSEdiCount :=diCount+1;END_IFEND_IF//ResetthecountifxResetInputissetIFxresetTHENdiCount :=0;END_IF//TheTimertoforcecountertozero//Thisisatthebottomsothatitdoesn't prevent countingtDelay(IN:=NOTxIn, PT:=DINT_TO_TIME(diDelayMS));
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The idea was to setup a simple counter.
If there is no more counting pulse after a set time, the counter is reset to 0 at the next counting pulse.
Everything works perfectly, except when the counter is automatically set to 0, there have to be 2 counting pulses before going to state 1, there should be only one because the counter is at 0.
Where did I make a mistake?
Try running a trace on it that includes all the variables.
Here's what I came up with: