1

For my examination I need a light, which I can flash for min. 40 ms. I can still control the light. The Light has 20 channels with different LEDs. I can control the channel (1-20, integer), the intensity (0-1000, integer) and the time for how long the LEDS should flash onetime (40-.... milliseconds).

Actually I solved it with the Sleep function:

channelID = 5;
intens    = 1000;
time      = 50  //milliseconds

led->setChannelIntensity(channelID, intens);
Sleep(time);
led->setChannelIntensity(channelID, 0);

That works, but it is too vague. There is a deviation from +/- 5 percent. How can I implement a solution, where the flash-time is always the same? I heard about Timer-Events, but I don't know how to do that. My programming knowledge is very bad. May be you can help me and you have a code example for me?

Thank you so much!!!

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • I've removed the `Matlab` tag as it seemed irrelevant. Please edit the question if I'm wrong and add the tag – Luis Mendo Aug 29 '14 at 11:31
  • See here: http://stackoverflow.com/questions/14650885/how-to-create-timer-events-using-c-11 – Horius Aug 29 '14 at 11:34
  • Or here: http://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer – eerorika Aug 29 '14 at 11:35
  • 5
    What kind of system is this for.? . Many microcontrollers have accessible hardware timers that make this simple. Standard PCs, not so much. – AShelly Aug 29 '14 at 11:36
  • matlab tag seemed to be very-very relevant, but I'm not going to edit the question. – Basilevs Aug 29 '14 at 12:02

1 Answers1

0

If this is windows, then the default tick rate is 64 hz or 15.625 ms. The 50 millisecond sleeps are going to alternate to the nearest 64 hz boundary, so they range from 46.875 to 62.5 ms. You can speed up the ticker rate using timeBeginPeriod() . Don't forget to use timeEndPeriod() when done.

If you want a loop that runs at a very precise frequency, usually in it's own thread, here is example C code. This is Windows XP compatible, which can take up to 2ms with a Sleep(1) so max frequency is about 400. For Windows 7 and later, Sleep(1) will be 1ms, so replacing u2ms with a properly calculated u1ms would allow a max frequency about 800.

typedef unsigned long long UI64;        /* unsigned 64 bit int */
#define FREQ    20                      /* frequency */
LARGE_INTEGER liPerfTemp;               /* used for query */
UI64 uFreq = FREQ;                      /* process frequency */
UI64 uOrig;                             /* original tick */
UI64 uWait;                             /* tick rate / freq */
UI64 uRem = 0;                          /* tick rate % freq */
UI64 uPrev;                             /* previous tick based on original tick */
UI64 uDelta;                            /* current tick - previous */
UI64 u2ms;                              /* 2ms of ticks */
UI64 i;

    /* ... */ /* wait for some event to start thread */
    timeBeginPeriod(1);                 /* set period to 1ms */
    Sleep(128);                         /* wait for it to stabilize */

    u2ms = ((UI64)(liPerfFreq.QuadPart)+499) / ((UI64)500);

    QueryPerformanceCounter((PLARGE_INTEGER)&liPerfTemp);
    uOrig = uPrev = liPerfTemp.QuadPart;

    for(i = 0; i < (uFreq*30); i++){
        /* update uWait and uRem based on uRem */
        uWait = ((UI64)(liPerfFreq.QuadPart) + uRem) / uFreq;
        uRem  = ((UI64)(liPerfFreq.QuadPart) + uRem) % uFreq;
        /* wait for uWait ticks */
        while(1){
            QueryPerformanceCounter((PLARGE_INTEGER)&liPerfTemp);
            uDelta = (UI64)(liPerfTemp.QuadPart - uPrev);
            if(uDelta >= uWait)
                break;
            if((uWait - uDelta) > u2ms)
                Sleep(1);
        }
        if(uDelta >= (uWait*2))
            dwLateStep += 1;
        uPrev += uWait;
        /* fixed frequency code goes here */
        /*  along with some type of break when done */
    }

    timeEndPeriod(1);                   /* restore period */
rcgldr
  • 27,407
  • 3
  • 36
  • 61