Millisecond counter

Using a millisecond counter to trigger a function

Page 1 of 1

1 Replies - 10173 Views - Last Post: 12 October 2008 - 02:16 PM Rate Topic: -----

#1 Cotillion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-October 08

Millisecond counter

Posted 12 October 2008 - 01:42 PM

Hi all,

I'm trying to create a timer which will count milliseconds, assign them to a running variable, and trigger a function every x milliseconds until a set amount of time has passed. Thus far, I haven't even been able to get this to compile, and my design probably has definite flaws in how it accounts for differences in the time it takes to execute the loop and the fact that the loop may not execute fast enough to catch every point it should call the "Awesome()" function. Also, I think it would consume processor power like mad when run. Ideally, I'm looking for some way to do this without those flaws and any others you can spot. Anyways, here's the code, and below that I'll have the errors my compiler (Visual C++ 2008 Express Edition) is giving me.

/*File:  Test1.cpp
  The goal is to trigger a function every x
  milliseconds while keeping a running counter
  of the number of milliseconds which have

#include <iostream>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int main() {
	long timeCounter = 0;
	int x = 500;	// Increment every 500 milliseconds
	int programLength = 10;	/* How many seconds will
							the program last?*/
	long milliLength;	//Program length in milliseconds
	bool stillCounting = true;

	//function declarations
	long HowManyMilliseconds(int);
	long Count();
	const void TriggerFunction(long, int);
	void TestTime(& bool, long, long);
	const void Awesome();

	//How long will the program last in milliseconds?
	milliLength = HowManyMilliseconds(programLength);
	/*Run a loop to count the time, test to see if x many
	  milliseconds have passed, and trigger the function
	  if that is the case.  After the test, another check
	  is made to see if the loop should end.  */
	while (stillCounting) {
		timeCounter = Count();
		TriggerFunction(timeCounter, x);
		TestTime(& stillCounting, milliLength, timeCounter);

	char letter;
	cout << "Enter a letter to exit./t";
	cin >> letter;
	return 0;


//Finds how many milliseconds the program will run for
long HowManyMilliseconds(int x){
	return 1000 * x;

//Keeps the timeCounter updated
long Count() {
	return clock();

//Checks to see if it is time to trigger the function,
const void TriggerFunction(long timeCounter, int x) {
	/* Divide timeCounter by 10 to reduce the error
	   I think would be intrinsic by way of it taking
	   time to execute the loop.  Also divide x by
	   the same amount to keep everything consistent
	   internally.  */
	long roundedCounter = timeCounter / 10;
	int y = x / 10;
	if (timeCounter % y == 0) {
	else {

//Checks to see if it is time to exit the loop.
void TestTime(& bool stillCounting, long milliLength, long timeCounter)
	if (milliLength >= timeCounter) {
		stillCounting = false;
	else {

//Prints a message saying it actually worked! :)
const void Awesome() {
	cout << "Whoot!  It worked!" << endl;

As I mentioned, this will not compile (for me at least). My compiler gives me six errors and zero warnings. The errors are as follows:
-test1.cpp(29) : error C2062: type 'bool' unexpected
-test1.cpp(42) : error C3861: 'TestTime': identifier not found
-test1.cpp(75) : error C3861: 'Awesome': identifier not found
-test1.cpp(82) : error C2062: type 'bool' unexpected
-test1.cpp(83) : error C2143: syntax error : missing ';' before '{'
-test1.cpp(83) : error C2447: '{' : missing function header (old-style formal list?)

Thank you so much for any help you can give me,

This post has been edited by Cotillion: 12 October 2008 - 01:46 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Millisecond counter

#2 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 611
  • Joined: 24-July 08

Re: Millisecond counter

Posted 12 October 2008 - 02:16 PM

A couple of those errors are because you are calling to funtions that dont exist, such as Awsome() and TestTime(). You need at least a prototype before the main() function.

as far as the bool goes, idk but there is a way around that.
but you also need something that will break from that loop

	//code here
	if (done == 1)
		 //or return false;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1