We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Creating a timer
Page Index Toggle Pages: 1
Creating a timer (Read 7553 times)
Creating a timer
Nov 13th, 2008, 8:19am
 
I am trying to create a timer that starts from the moment the sketch runs and would display the following:

"This timer has been running for x years x months x days x hours and x seconds"

with x obviously being the relevant times.

from what i understand from the time & date reference section all the statements except millis() refer to processing taking the time from your computers clock.

If this is true, do i need to use millis() to build seconds, hours, days, months and years?

thanks,
TB
Re: Creating a timer
Reply #1 - Nov 13th, 2008, 3:04pm
 
Basically, yes.
The problem is getting beyond days, as the number of months depend on the current date...
Now, it supposes you have a computer with a good uptime... Smiley

Skipping the hard part, I run the following code on click:
Code:
    int m = millis();
   int seconds = m / 1000;
   int minutes = seconds / 60;
   int hours = minutes / 60;
   int days = hours / 24;
seconds -= minutes * 60;
minutes -= hours * 60;
hours -= days * 24;

   String format = "This timer has been running for {0,number,integer} days {1,number,integer} hours {2,number,integer} minutes and {3,number,integer} seconds";
   String message = MessageFormat.format(format,
       days,
       hours,
       minutes,
       seconds
   );
   println(message);

Normally, I would display a plural only if needed...
Re: Creating a timer
Reply #2 - Nov 14th, 2008, 8:28pm
 
Thank you.  I see what you are saying about months.

I was also looking at have the timer reset on key pressed.  Cant work it out.  Any ideas?
Re: Creating a timer
Reply #3 - Nov 15th, 2008, 10:44am
 
Quite easily:
Code:
int startingTime;
final String prefix = "This timer has been running for ";

void setup()
{
 size(1000, 200);
 PFont font= loadFont("AmericanTypewriter-24.vlw");
 textFont(font);

 startingTime = millis();
}

void draw()
{
 background(#99EEAA);

 int seconds = (millis() - startingTime) / 1000;
 int minutes = seconds / 60;
 int hours = minutes / 60;
 int days = hours / 24;
seconds -= minutes * 60;
minutes -= hours * 60;
hours -= days * 24;

 String message = prefix +
     GetPlural(days, "day") + " " +
     GetPlural(hours, "hour") + " " +
     GetPlural(minutes, "minute") + " and " +
     GetPlural(seconds, "second");

 fill(#9977EE);
 stroke(#0000FF);
 text(message, 20, 100);
//  println(message);
}

String GetPlural(int value, String word)
{
 if (value == 1)
   return value + " " + word;
 // Ad hoc for current sketch,
 // I have something more elaborate for irregular plurals
 return value + " " + word + "s";
}

void keyPressed()
{
 startingTime = millis();
}
Re: Creating a timer
Reply #4 - Jan 26th, 2009, 1:13am
 
once you get past 1 hour the seconds will be incorrect.  likewise with getting past 1 day.  you need to switch the order of ops:

Code:

seconds -= minutes * 60;
minutes -= hours * 60;
hours -= days * 24;

Re: Creating a timer
Reply #5 - Jan 26th, 2009, 10:33am
 
You are right, I never let the sketch run long enough to see the issue!
I fixed the code in both messages above to avoid being misleading.

Quick test code:
Code:
int startingTime;

void setup()
{
frameRate(1);
startingTime = millis() - 1000*(60*60*2 + 60*59 + 50);
}

void draw()
{
int seconds = (millis() - startingTime) / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
int days = hours / 24;
println(">H " + hours + " M " + minutes + " S " + seconds);
seconds -= minutes * 60;
minutes -= hours * 60;
hours -= days * 24;
println("=H " + hours + " M " + minutes + " S " + seconds);
}

Still not tested thoroughly... (but OK past midnight).

Many thanks, eforman!
Page Index Toggle Pages: 1