counter(for)

koadrobot/aritmatics/counter(for)

Count from start to stop with step at each INC pulse. When RST pulse is received counting start (again). If use START is greater than STOP for decresing. STEP is converted to pozitive always. At end of the counting loop (to reach STOP), LOOP pulse transmit. If Last Inc/Dec bigger/smaller then STOP, STOP value transmit.
counter(for)
@/counter(for)
Count from start to stop with step at each INC pulse. When RST pulse is received counting start (again). If use START is greater than STOP for decresing. STEP is converted to pozitive always. At end of the counting loop (to reach STOP), LOOP pulse transmit. If Last Inc/Dec bigger/smaller then STOP, STOP value transmit.
STEPnumber
Value to increment or decrement on each INC. Step is absolute. If (Stop > Start) then increment. If (Stop < Start) then decrement.
STARTnumber
Start Value to count.
STOPnumber
Stop Value to count.
INCpulse
Triggers a single step.
RSTpulse
Resets the counting value to start.
counter(for)
SUM
TICK
LOOP
STEP
START
STOP
INC
RST
LOOPpulse
Fires on each for-count complete.
TICKpulse
Fires on each for-count.
SUMnumber
The counting value.
To use the node in your project you should have the koadrobot/aritmatics library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
    bool isStarted = false;
    bool isStoped = false;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx)
{
    Number sum = getValue<output_SUM>(ctx);
    Number start = getValue<input_START>(ctx);
    Number stop = getValue<input_STOP>(ctx);
    Number step =  getValue<input_STEP>(ctx);
    step = abs(step);
    State* state = getState(ctx);

    if (isInputDirty<input_RST>(ctx))
    {
        state->isStarted = true;
        state->isStoped = false;
        emitValue<output_SUM>(ctx, start);
        emitValue<output_TICK>(ctx, 1);
    }
    else if (isInputDirty<input_INC>(ctx))
    {
        if (state->isStoped) { return; }

        if (state->isStarted)
        {
            sum = (stop>start) ? sum + step : sum - step;

        }
        else
        {
            state->isStarted = true;
            state->isStoped = false;
            sum=start;
        }

        if (((stop>=start) && (sum>stop)) || ((stop<=start) && (sum<stop)))
        {
            state->isStoped = true;
            state->isStarted = false;
            emitValue<output_SUM>(ctx, sum);
            emitValue<output_LOOP>(ctx, 1);
        }
        else
        {
            emitValue<output_TICK>(ctx, 1);
            emitValue<output_SUM>(ctx, sum);
        }
    }

}

Tabular tests

INCRSTSTARTSTOPSTEPSUMTICKLOOP
pulseno-pulse0310pulseno-pulse
pulseno-pulse0311pulseno-pulse
pulseno-pulse0312pulseno-pulse
pulseno-pulse0313no-pulsepulse
pulseno-pulse0313no-pulseno-pulse
no-pulsepulse0310pulseno-pulse