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 & HelpPrograms › How can i make a stopwatch
Page Index Toggle Pages: 1
How can i make a stopwatch? (Read 3757 times)
How can i make a stopwatch?
Jun 15th, 2009, 3:30pm
 
Hello everybody,

i am a beginner.
i habe now a problem, how can ich make a stopwatch with processing.
ich have browsed the whole forum. But i have not found the answer.
Undecided Undecided
Re: How can i make a stopwatch?
Reply #1 - Jun 15th, 2009, 4:14pm
 
Always good to look in the forums, but don't forget about the reference Wink

How about:

http://processing.org/reference/millis_.html

At a simple level you can just count these, adjust some variables appropriately, and display them with text.  In the short term you could start and stop with mouse clicks or key presses, rather than worrying about buttons...

I'd imagine that would be enough rto get started...  I do wonder just how accurate the value returned by millis is though  Huh
Re: How can i make a stopwatch?
Reply #2 - Jun 16th, 2009, 2:35am
 
Perhaps Creating a timer thread might get you started.

Remember to set the "This post was made in the last..." field in the Search page to a large time span (but that's slower)... and to vary the search strings.
Re: How can i make a stopwatch?
Reply #3 - Jun 16th, 2009, 2:17pm
 
thanks for all your help

i have managed to write a stopwatch.
the watch can beginn, stop or continue. Cheesy Cheesy


int ss;
int ii=1;
int mill;
int a;
int b;
int m;
int s;

void setup(){

 PFont font;
 font = loadFont("ArialNarrow-12.vlw");
 textFont(font);
 
 size(600,600);
 ss=0;

}


void draw(){
 background(255);
 if(ii==0){
   mill=millis()/1000;
   m=mill/5;
   s=mill%5;
   a=m;
   b=s;
 }
 if(ii==2){
   mill=(millis()-ss)/1000;
   m=mill/5;
   s=mill%5;
   a=m;
   b=s;
 }

 fill(0,0,0);

 text(a,50,100); // a is minute
 text(b,50,120); // b is second

}

void keyPressed(){ //watch stop
 if(key=='s'){
   ii=1;
 }
 if(key=='b'){ //watch beginn or reneu
   ii=2;
   ss=millis();
 }
 if(key=='c'){ //watch continue
   ii=2;
   ss=0;
 }
}
Re: How can i make a stopwatch?
Reply #4 - Jun 17th, 2009, 2:45pm
 
Hmmm...  That's a slightly unconventional stopwatch, but I guess you may have a reason to count in fives...  

Anyway - it's not a bad first attempt, but you do have a slight problem: the top counter continues to increase when the stopwatch is 'paused'.  Pause and wait a few seconds then restart and you'll see what I mean.
Re: How can i make a stopwatch?
Reply #5 - Jun 21st, 2009, 11:17pm
 
blindfish wrote on Jun 17th, 2009, 2:45pm:
Hmmm...  That's a slightly unconventional stopwatch, but I guess you may have a reason to count in fives...  

Anyway - it's not a bad first attempt, but you do have a slight problem: the top counter continues to increase when the stopwatch is 'paused'.  Pause and wait a few seconds then restart and you'll see what I mean.



thank you for you advice.
i know what you mean.
so i have changed code:

int ss;
int ii=1;
int mill;
int a;
int b;
int m;
int s;

int u;
int v;

void setup(){

PFont font;
font = loadFont("ArialNarrow-12.vlw");
textFont(font);

size(600,600);
ss=0;

}


void draw(){
background(255);
if(ii==0){
 
  mill=(millis()-ss)/1000;
  m=mill/5+u;
  s=mill%5+v;
  a=m;
  b=s;
    //p=0;
   // println(a);
    //println(b);
   
 
 
}
if(ii==2){
  mill=(millis()-ss)/1000;
  m=mill/5;
  s=mill%5;
  a=m;
  b=s;
}

fill(0,0,0);

text(a,50,100); // a is minute
text("m",60,100);
text(b,50,120); // b is second
text("s",60,120);
text("key b for begin,s for stop, c for continue",50,140);

}

void keyPressed(){ //watch stop
if(key=='s'){
  ii=1;
}
if(key=='b'){ //watch beginn or reneu
  ii=2;
  ss=millis();
}
if(key=='c'){ //watch continue
  ii=0;
  ss=millis();
 
  u=a;
  v=b;
}
}
Re: How can i make a stopwatch?
Reply #6 - May 18th, 2010, 9:22am
 
Thanks for sharing, this is MUCH simpler than what I had attempted on my own. It took me a while to digest exactly what you'd done, so here's a version I did that I think is slightly more human-readable (for us dummies!).

Adam

**update: cleaned up code:

Code:
int timerStart = 0;
int offset;

int mill;
int minutes;
int seconds;
int hundredths;

boolean stopped = false;
boolean continued = false;

void setup(){
 PFont font;
 font = loadFont("ArialNarrow-12.vlw");
 textFont(font);
 
 size(600,600);
}


void draw(){
 background(255);
 
if(!stopped) {
mill=(millis()-timerStart);
if(continued) mill += offset;

seconds = mill / 1000;
minutes = seconds / 60;
seconds = seconds % 60;
hundredths = mill / 10 % 100;
}
 
 fill(0,0,0);
 text(nf(minutes,2,0)+":"+nf(seconds,2,0)+":"+nf(hundredths,2,0),50,100);
 text("key b for begin, s for stop, c for continue",50,140);
}

void keyPressed(){ // pause
 if(key=='s'){
   stopped = true;
 }
 if(key=='b'){ // reset
   stopped = false;
   continued = false;
   timerStart = millis();
 }
 if(key=='c'){ // continue
   stopped = false;
   continued = true;
   timerStart = millis();
   
   offset = mill;
 }
}

Re: How can i make a stopwatch?
Reply #7 - May 18th, 2010, 2:03pm
 
I'm actually using it as a class, so here it is as an object:

Code:
class Timer {
int timerStart = 0;
int offset;

int mill;
int minutes;
int seconds;
int hundredths;

boolean stopped = false;
boolean continued = false;

Timer() {

}

void update() {
if(!stopped) {
mill=(millis()-timerStart);
if(continued) mill += offset;

seconds = mill / 1000;
minutes = seconds / 60;
seconds = seconds % 60;
hundredths = mill / 10 % 100;
}
}

void pause() { stopped = true; }

void unpause() {
stopped = false;
continued = true;
timerStart = millis();

offset = mill;
}

void reset() {
stopped = false;
continued = true;
timerStart = millis();
}

int minutes() { return minutes; }
int seconds() { return seconds; }
int hundredths() { return hundredths; }

boolean isPaused() { if (stopped) return true; else return false; }
}

Re: How can i make a stopwatch?
Reply #8 - May 19th, 2010, 2:29am
 
Suggestion:
boolean isPaused() { return stopped; }
Cool
Re: How can i make a stopwatch?
Reply #9 - May 19th, 2010, 7:26am
 
PhiLho  wrote on May 19th, 2010, 2:29am:
Suggestion:
boolean isPaused() { return stopped; }
Cool


That would have been much too easy. I was just testing to see if you'd catch it. Good job! (forehead smack)
Page Index Toggle Pages: 1