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.
Page Index Toggle Pages: 1
Timer problem (Read 203 times)
Timer problem
Feb 16th, 2009, 3:45am
 
I am using the following code to create a timer that counts up on screen and resets when a key is pressed.

However, when the timer passes the 1 hour mark, the seconds start to accumulate beyond '60'.  They still add to the minutes but don't start again from '0'.  e.g. http://uoregon.edu/~josh/odd_count.jpg

//
// This Button
//

int startingTime;
final String prefix = "This button was last pressed ";

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

 startingTime = millis();
 

}

void draw()
{
 background(#ffffff);

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

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

 fill(#000000);
 stroke(#0000FF);
 text(message, 200, 400);
//  println(message);



}

String GetPlural(int value, String word)
{
 if (value == 1)
   return value + " " + word;
   return value + " " + word + "s";
 
 
}

void keyPressed(){
    startingTime = millis();
 }
Re: Timer problem
Reply #1 - Feb 16th, 2009, 9:49am
 
You got the code from Creating a timer and somebody pointed out it was buggy. The thread shows the right code now.
Re: Timer problem
Reply #2 - Feb 16th, 2009, 6:10pm
 
thank you
Page Index Toggle Pages: 1