Problem with timer delay in C

L

lynology

My program takes in a key pressed value from the main routine and
based on the key pressed, it selects the command to be executed. The
problem I have is in creating a delay timer so that a message appears
on my screen for only one second. The condensed code is as follows:

#include <sys\timeb.h>
#define DELAY_1SEC 1000 // in millisec

void Scan_Menu_Keys(int key_press)
{ switch(key_press){
case 1: Execute1stCommand(); break;
case 2: Start_Timer();
while (!Timer_Expired(DELAY_1SEC))
strcpy(CG_ScreenKeyboard.Screen, "Display Message");
Clear_Screen();
break;
default: break;
}
}

void Start_Timer(){
ftime(&start_time); // ftime is function defined in sys\timeb
}

int Timer_Expired(){
ftime(&current_time);
time_diff = (int) (1000.0*(current_time.time - start_time.time) +
(current_time.millitm - start_time.millitm));
return (time_diff >= DELAY_1SEC);
}

The problem I ran into was that the timer would wait for one second
before displaying the message "Display Message". Effectively, that
meant the message never displayed because when it got out of the 1
second delay loop, the screen was cleared. I would love to get
suggestions on why this delay loop is not be executing the command
within the loop and any alternative methods to achieving the same
results.

Thanks in advance!

LYN
 
M

Mark A. Odell

(e-mail address removed) (lynology) wrote in

My program takes in a key pressed value from the main routine and
based on the key pressed, it selects the command to be executed. The
problem I have is in creating a delay timer so that a message appears
on my screen for only one second. The condensed code is as follows:

#include <sys\timeb.h>
#define DELAY_1SEC 1000 // in millisec

void Scan_Menu_Keys(int key_press)
{ switch(key_press){
case 1: Execute1stCommand(); break;
case 2: Start_Timer();
while (!Timer_Expired(DELAY_1SEC))
strcpy(CG_ScreenKeyboard.Screen, "Display Message");
Clear_Screen();
break;
default: break;
}
}

Ignoring all the non-C stuff and reformatting your function for human
eyes:

void Scan_Menu_Keys(int key_press)
{
switch (key_press)
{
case 1: Execute1stCommand(); break;

case 2: Start_Timer();
/* maybe never do it */
while (!Timer_Expired(DELAY_1SEC))
{
strcpy(CG_ScreenKeyboard.Screen, "Display Message");
}

Clear_Screen();
break;

default: break;
}
}

We can now clearly see that you do, indeed, wait first then copy "Display
Message" to some buffer somewhere that we'll assume eventually makes it to
a "screen". Re-writing the while loop as a do-while may give you the
desired operation:

void Scan_Menu_Keys(int key_press)
{
switch (key_press)
{
case 1: Execute1stCommand(); break;

case 2: Start_Timer();
do /* Do it at least once */
{
strcpy(CG_ScreenKeyboard.Screen, "Display Message");
} while (!Timer_Expired(DELAY_1SEC))

Clear_Screen();
break;

default: break;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top