Adding a stopwatch to a clock

Hi!
I’m trying to add a stopwatch to my clock. When I start the program the clock shows the current time and I want to add a stopwatch that starts when the mouse is clicked. The clock itself works but I can’t get my stopwatch to work, I want the clockhands to go to 12 and that the digital clock resets to 0 and starts counting until i click again and the current time is displayed again. Here’s what I have so far:

int i = 0;
int h = hour();
int m = minute();
int s = second();
boolean stopwatch = false;

void setup()
{
  size(500, 500);
}

void draw()
{
  background(255);
  clock();
  lines();
  clockhands();
  digitalclock();
  
  if (stopwatch)
  {
    s++;
    int s = second();
    int m = minute();
    int h = hour();
  }
  else
  {
    int s = second();
    int m = minute();
    int h = hour();
  }
}  

void clock()
{
  translate(width/2, height/2);
  stroke(0);
  fill(255);
  strokeWeight(2);
  ellipse(0, 0, 300, 300);
  strokeWeight(8);
  stroke(0);
  point(0, 0);
}
 
void lines()
{
  pushMatrix();
  for (int i = 0; i < 60; i++)
  {
    if (i % 5 == 0)
    {
      stroke(0);
      strokeWeight(2);
      line(0, -140, 0, -145);
    }
    else
    {
      stroke(0);
      strokeWeight(1);
      line(0, -140, 0, -145);
    }
    rotate(radians(6));
    }
  popMatrix();
}

void clockhands()
{
  int h = hour();
  int m = minute();
  int s = second();

  pushMatrix();
  rotate(radians(h*30));
  stroke(0); // hours
  strokeWeight(3);
  line(0, 0, 0, -100);
  popMatrix();
  
  pushMatrix();
  rotate(radians(m*6));
  stroke(150); // minutes
  strokeWeight(2);
  line(0, 0, 0, -140);
  popMatrix();
  
  pushMatrix();
  rotate(radians(s*6));
  stroke(229, 79, 55); // seconds
  strokeWeight(1);
  line(0, 0, 0, -140);
  popMatrix();
}

void digitalclock()
{
  pushMatrix();
  int h = hour();
  int m = minute();
  int s = second();
  PFont digital;
  digital = loadFont("PingFangHK-Ultralight-48.vlw");
  textFont(digital, 25);
  fill(0);
  text(h + ":" + nf(m, 2) + ":" + nf(s, 2), -45, 110); 
  popMatrix();
}

void mousePressed()
{
  stopwatch = !stopwatch;
}

I’m not good with boolean and events since I just started learning, hopefully you can give me some tipps for my problems. Thanks!!

hi @yamiyugi

i already started from your code to make that add START/STOP functionality
but just closed it, without saving…

pls. allow me some critic notes to your code:

  • -a- pls. move the 3 lines about the font loading and declaring
    from draw to setup

  • -b- the variable scope is a big thing and need some planning in a already modular
    code like yours

    • make global like

int now_h, now_m, now_s;

and make a

time_update() {}

function called from the beginning of draw.

and use that as variables in the other functions.
not much use to make int h = hour(); again in every function
as a kind of local variable ( hm. did you think a update is required?? )

for the Start Stop you require minimum

  • a long type variable
    long start,stop; … start = millis(); …
    stop = millis();
    int delta_seconds = int((stop - start)/1000)

  • or the luxury version need

int start_h,start_m,start_s,stop_h,stop_m,stop_s;

where you save the now_… to at the click moments.

int delta_seconds = (stop_h*60*60+stop_m*60+stop_s ) - (start_h*60*60+start_m*60+start_s )

the good thing would be that you can show exact start & stop time and elapsed seconds
( until next start…)

mouse click or a key entry for operation is ok,
much more nice would be a (toggle type ) button
means it changes its text depending on its status, so it shows “start” or “stop”
add even change background color.

for the mouse operation would need a
boolean function over_button(mouseX,mouseY)…

i recommend to try that operation button as a extra project first.


also why not just take a look at some great examples: