Motor Encoder - Program an action for every loop

code for output in post #16

#include <stdio.h>

const int   PulsePerRot = 408;
const float OffsetInc   = 0.168;

long  enc;

void
gatling (void)
{
    static float offset;
    static int   cnt;
    int          tic = 0;

    cnt++;

    if (PulsePerRot < cnt)  {
        cnt    -= PulsePerRot;
        offset += OffsetInc;
        if (1 <= offset)  {
            offset -= 1;
            cnt--;
            tic++;
        }
    }

    if (! (cnt % 68))
        printf (" %8d %6d %8.3f %3s  barrel\n",
            enc, cnt, offset, tic ? "*" : " ");
}

int
main ()
{
    for (enc = 1 ; 5000 > enc; enc++)
        gatling();

    return 0;
}
1 Like