Countdown Timer once the time is set

edited May 2016 in Questions about Code

Hi there, I'm totally new with processing. I'm trying to create something similar to this, Digital Stop watch by Charles Cave. But instead of starting it with 00:00:00, I want it start with a time that I have set. so for example if I set it to 20mins when I pressed T it will countdown to 0. something like that.

int accumTime;   // total time accumulated in previous intervals
int startTime;   // time when this interval started
boolean running = false;
int displayTime;   // value to display on the clock face

PFont smallfont;

void draw() {
  background(36, 19, 25);
  int thisTime = millis();
  int dHours, dMins, dSecs, dDecimal;

  if (keyPressed) {
      if ((key == 'c') || (key == 'C')) {
        // clear the time and stop the clock
        accumTime= 0;
        displayTime = 0;
      }
      if ((key == 't') || (key == 'T')) {
        // start timing (but only if running == false)
        if (running == false) {
            startTime = millis();
            running = true;
        }
      }
      if ((key == 's') || (key == 'S')) {
          // stop timing, but do not clear
          if (running == true) {
            println ("Stopped at " + thisTime);
            running = false;
            accumTime = accumTime + millis() - startTime;
          }
      }
    }
    if (running == true) {
          displayTime = accumTime + millis() - startTime;
    }

   dSecs = (displayTime / 1000) % 60;
   dMins = (displayTime / 1000 / 60) % 60;
   dHours = (displayTime / 1000 / 60/ 60);

    text(nf(dHours,2) + ":" + nf(dMins, 2) + ":" + nf(dSecs, 2),
      width/2, height/2 + 40);
}

void setup() {
  size(550,220);
  smooth();
  frameRate(20); 
  textSize(128);
  textAlign(CENTER);
  fill(202, 35, 55);

}

Any help greatly appreciated.

Tagged:
Sign In or Register to comment.