The complete code is:
Code:
/* Programme to mimic Grenwich time signal (BBC pips) © Fat Agnus, all rights reserved */
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#define shortsleep 600
#define shortbeep 100
#define longbeep 500
WORD minutes, seconds, milliseconds, minutesuntiltoh, secondsuntiltom;
LONG millisecondsuntiltoh, millisecondsuntiltom;
void main()
{
SYSTEMTIME str_t;
/* GetSystemTime(&str_t);
minutes = str_t.wMinute;
seconds = str_t.wSecond;
milliseconds = str_t.wMilliseconds;
printf("Min:%d\nSecond:%d\nMilliseconds:%d\n"
,minutes,seconds,milliseconds);
*/
while(TRUE)
{
/* Get system time */
GetSystemTime(&str_t);
minutes = str_t.wMinute;
seconds = str_t.wSecond;
milliseconds = str_t.wMilliseconds;
/* calculate milliseconds until top of minute */
/* millisecondsuntiltom = 60000-(seconds*1000+milliseconds);
printf("milliseconds until top of minute:%d\n", millisecondsuntiltom); */
/* calculate milliseconds until top of hour */
/* calculate milliseconds until top of the hour ^/
millisecondsuntiltoh = 3600000-(minutes*60000+seconds*1000+milliseconds);
/* printf("milliseconds until top of hour:%d\n", millisecondsuntiltoh); */
/* Sleep until just before 55 seconds of minute */
Sleep(millisecondsuntiltoh - 5050);
/* printf("Sleep finished\n"); */
/* GetSystemTime(&str_t);
seconds = 54; */
/* Wait until seconds reach 55 */
while(seconds!=55)
{
GetSystemTime(&str_t);
seconds = str_t.wSecond;
}
Beep(1000, shortbeep);
/* Sleep until just before 56 seconds of minute */
Sleep(shortsleep);
/* Wait until seconds reach 56 */
while(seconds!=56)
{
GetSystemTime(&str_t);
seconds = str_t.wSecond;
}
Beep(1000, shortbeep);
/* Sleep until just before 57 seconds of minute */
Sleep(shortsleep);
/* Wait until seconds reach 57 */
while(seconds!=57)
{
GetSystemTime(&str_t);
seconds = str_t.wSecond;
}
Beep(1000, shortbeep);
/* Sleep until just before 58 seconds of minute */
Sleep(shortsleep);
/* Wait until seconds reach 58 */
while(seconds!=58)
{
GetSystemTime(&str_t);
seconds = str_t.wSecond;
}
Beep(1000, shortbeep);
/* Sleep until just before 59 seconds of minute */
Sleep(shortsleep);
/* Wait until seconds reach 59 */
while(seconds!=59)
{
GetSystemTime(&str_t);
seconds = str_t.wSecond;
}
Beep(1000, shortbeep);
/* Sleep until just before 0 seconds of minute */
Sleep(shortsleep);
/* Wait until seconds reach 0 */
while(seconds!=0)
{
GetSystemTime(&str_t);
seconds = str_t.wSecond;
}
Beep(1000, longbeep);
}
}
The
Code:
Sleep(millisecondsuntiltoh - 5050)
will cause the program to sleep until just before 5 seconds before 55 seconds to the hour. The 5050 figure has been 6000 during development but the problem persists. In fact if I reduce it to very close to 5000 the problem goes away leaving me to think it's a problem to do with a multitasking operating system.
Thank you.