How to create a simple timer?

Hello,
I think this is a rather simple question, but how do I create a timer(or something like that), that tells the programm to do thing 1, and then, after half a second, do thing 2? I tried using the frameRate() command(do you call stuff like that commands i don’t know), but it did not really work out.

Hi Schred,

Check out the millis() function.

You can find plenty of example on the forum on how to use it :slight_smile:

Thanks, I already have a little experience with that, but I heared that it’s “exhausting” for the processor, to run this command that often. Is there an easier way? (Or is that simply not true?)

I can’t say for sure but I don’t think it is heavy on the processor and if you really just want to make a simple timer, it will definitly not be too much for the processor to handle.

1 Like

Okay thanks, I’ll try it then! :smile:

2 Likes

Thanks dude, I’ll take a look at it :smile:

For something super simple you could just increment an int. If your sketch is running at 30 fps then every 30 increments is 1 second.

@ScottGrogin Well I tried that before with “something = 1/frameRate;” and testing for a value that should only appear once during the 60 frames but NOPE it did not work out (with “if(blabla == something)”) … XD :wink:

Be careful when checking for true equality when trying to time something. It will almost never match the exact time you want.

The best you can do is to check if the elapsed time is higher than the time you wanted and then perform the action you want in this case.

Libraries also includes TimedEvents, Timing Utilities, and Countdown Timer.

== will not work, because the precise millisecond a frame occurs on is not reliable.

void draw(){
  print(millis(), ' ');
}

Run it three times:

513 531 546 562 579 595 615 629 648 662 681 695 713 729 746 764 782 795

533 550 566 585 600 617 632 651 666 700 719 734 750 767 785 801 817 835

485 504 522 537 554 571 585 602 619 639 652 672 686 704 719 737 751 772

…it will print different values every time you run it. Use >.

@jb4x I might try that next time! :smile: (For now, I’ve got a solution :P)

@jeremydouglass That’s what kept me from doing it that way, I just didn’t know how to fix it. :smile: Next time I’ll try it :slight_smile:

text ((frameCount/frameRate),width/2,height/2);
Displays the exact amount of time the program has been running for

or use

text (millis(),width/2,height/2);

Haha, thanks for your replies. I’m done with that project by now though. ;D

1 Like