How to stop timer at the certain time automaticlly?

edited January 2017 in How To...

This is "my" code (credits goes to @Chrisis. He created similiar timer here: So its more his code then mine.

CODE

What I want:

1.) When key "T" is pressed timer starts to run (I did this).

2.) When the timer comes to 45:00, timer stops.

3.) When key "T" is pressed again, timer starts where it stoped.

So with key T I want to start and stop the timer, and when timer comes to 45:00 to automaticlly stop at this time until key "T" is pressed again to continue counting.

Tagged:

Answers

  • 2.) goes back to zero and starts?

  • Sorry, but this is more of a stopwatch to measure a time elapsed and not a timer

    But I can have a look later

  • I don't want to go back to zero. I need this timer for a football purposes. So when the timer comes to 45:00, timer stops and then when I press key T timer goes on from 45:00.

    Like that:

    44:56

    44:57

    44:58

    44:59

    45:00 (at this time Timer stops autamaticlly)

    When key "T" is pressed timer goes on

    45:01

    45:02

    45:03

    ...

  • till 90 then 0

  • till 90 yes, then it's over. :)>-

  • do you mean 45 Minutes or seconds ?

  • Answer ✓
    StopWatchTimer sw;
    
    void setup() {
      size(400, 400);
    
      sw = new StopWatchTimer();
      sw.setStopTime(4500);
      sw.start();
    }
    
    void draw () {
      background(0);
    
      time();
      sw.checkStopTime();
    }
    
    //-----------------------------------------
    
    void time() {
      fill(255); // Barva števca
      textAlign(CENTER);
      textSize(15); // Velikost časa
      text(nf(sw.minute(), 2)+":"+nf(sw.second(), 2), width / 2, height / 2);
    
      if (sw.paused) {
        fill(255, 0, 0); // red
        text("Paused, Hit t to continue.", width / 2, height / 2+77);
      } else if (sw.continuing) {
        fill(0, 255, 0); //
        text("2nd Half of the game.", width / 2, height / 2+77);
      } else if (sw.gameIsOver) {
        fill(0, 0, 255); //blue
        text(" - Game over - ", width / 2, height / 2+77);
      }
      //
    } // func 
    
    void keyPressed() {
      println(keyCode);
    
      // 84 = t, T
      if (keyCode == 84) {
        // this is after the break 
        sw.setStopTime(4500);
        sw.continueTime();
        println("T");
      }
    
      // 83 = s, S
      if (keyCode == 83) {
        sw.stop();
      }
    }
    
    // =====================================================================
    // CLASS
    
    class StopWatchTimer {
    
      int startTime;  // at which millis did we start? 
      int stopTime;   // how many millis do we run?
    
      // pause data 
      int beginOfPauseTime; 
      int endOfPauseTime;
      int howLongWasThePause; 
    
      // other data 
      int endTime;
    
      int firstTimeLength=-1;
    
      // boolean data 
      boolean running = false;
      boolean continuing=false;
      boolean paused = false;
      boolean gameIsOver=false;
    
      // constr 
      StopWatchTimer() {
        startTime  = 0;
        stopTime   = 0;
        running    = false;
      }// constr 
    
      // -----
    
      void setStopTime (int stopTime_) { 
        this.stopTime = stopTime_;
        if (firstTimeLength==-1) {
          firstTimeLength=stopTime_;
        }
      }
    
      void checkStopTime() {
        // checks if we need to stop the timer 
    
        if (gameIsOver) {
          return; // leave
        }
    
        if (continuing) {
    
          // check after the break
    
          if ((millis() - (startTime+howLongWasThePause)) > stopTime) {
            running = false;
            // pauseTime = millis();
            endTime = millis();
            paused = false;
            gameIsOver=true;
            continuing = false;
          }
        } else {
    
          // check before the break
    
          if (getElapsedTime() > stopTime) {
            running = false;
            beginOfPauseTime = millis();
            paused = true;
          }
        }
      }
    
      void start() {
        startTime = millis();
        running = true;
      }
    
      void continueTime() {
    
        if (continuing||gameIsOver) {
          return; // leave
        }
    
        endOfPauseTime = millis(); 
        startTime = beginOfPauseTime;
        howLongWasThePause = endOfPauseTime - beginOfPauseTime; 
        running = true;
        continuing=true;
        paused = false;
      }
    
      void stop() {
        stopTime = millis();
        running = false;
      }
    
      int getElapsedTime() {
    
        // returns millis 
    
        int elapsed;
    
        if (gameIsOver) {
          // game over 
          elapsed=endTime-howLongWasThePause;
        } else if (continuing) {
          // after the break
          elapsed = millis() - (endOfPauseTime);
          elapsed += startTime;
        } else if (running) {
          elapsed = millis() - startTime;
        } else {
          elapsed = stopTime - startTime;
        }
        return elapsed;
      }
    
      int second() {
        return (getElapsedTime() / 1000) % 60;
      }
    
      int minute() {
        return (getElapsedTime() / (1000*60)) % 60;
      }
      //
    }//class
    // ===========================
    
  • it's for two times 4,5 seconds now

    you need both times say 4500 (NOT 9000)

  • Thank you Chrisis. I'll look later in more detail and I'll ask you some questions too.

  • edited January 2017

    If I move this two lines of code

    sw.setStopTime(4500);
    sw.start();
    

    from void setup() to void keyPressed() under an if keyCode 10 (Enter key) statment it's not working properly. Why? I don't get it.

    Let's say I want to start timer when I press Enter key and not when I run Sketch. This should work but doesn't and I don't understand why not. :(

  • Answer ✓

    It's because we use millis and millis counts from start of the sketch and not from moment you pressed Enter

    It took me ages to figure that out for the 2nd half of the match (where a similar problem occurs)

    Just set up a var and when enter is pressed read the current millis in it.

    Then use the vars data to recalculate the results

    (brief description for a potential harder problem)

  • edited January 2017

    I still have some problem with that. Code bellow works like that: When I press key "T" timer starts counting from zero. "S" key stops the timer and continue timer. When pressing key "h" displays text "halftime" and when pressing key "enter" displays text "fulltime".

    /////////////// KEYS /////////////////
    // T = Starts the timer
    // S = Stops and continue the timer
    // H = Display text "Halftime"
    // ENTER = Display text "Fulltime"
    /////////////// KEYS /////////////////
    
    int timerStart = 0;  
    int offset;        
    int mill;       
    int minutes;   
    int seconds;
    boolean stopped = true;
    boolean continued = false;
    
    boolean gameStartInfo = true;
    boolean timer = false;
    boolean halfTime = false;
    boolean fullTime = false;
    
    void setup() {
      size(640, 360);
    }
    
    void draw() {
      background (0);
      timer();
    }
    
    void timer() {
    
      // Timer position
      float xTimerPosition = width / 2;
      float yTimerPosition = height / 2;
    
      fill(0, 0, 255);
      textAlign(CENTER);
      textSize(15);
    
      if (gameStartInfo)
        text("Match starts at 17h", xTimerPosition, yTimerPosition);
      else if (timer)
        text(nf(minutes, 2, 0) + ":" + nf(seconds, 2, 0), xTimerPosition, yTimerPosition);
      else if (halfTime) {
        text("HALFTIME", xTimerPosition, yTimerPosition);
      } else if (fullTime) {
        text("FULLTIME", xTimerPosition, yTimerPosition);
      }
    
      if (!stopped) {
        mill=(millis()-timerStart);
        if (continued) {
          mill += offset;
        }
        textSize(14);
        seconds = mill / 1000 % 60;
        minutes = mill / (1000 * 60) % 120;    
      }
    }
    
    void startTimer()
    {
      stopped = false;
      continued = false;
      timerStart = millis();
    }
    
    void stopTimer()
    {
      stopped = true;
    }
    
    void continueTimer()
    {
      stopped = false;
      continued = true;
      timerStart = millis();
      offset = mill;
    }
    
    void keyPressed() {
    
      // 84 = t, T
      if (keyCode == 84) {
        startTimer();
        timer = true;
        gameStartInfo = halfTime = fullTime = false;
      }
    
      // 83 = s, S
      if (keyCode == 83) {
        if (!stopped) {
          stopTimer();
          halfTime = false;
          timer = true;
        } else {
          continueTimer();
          halfTime = false;
          timer = true;
        }
      }
    
      // 72 = h, H
      if (keyCode == 72) {
        halfTime = true;
        gameStartInfo = timer = fullTime = false;
      }
    
      // 10 = ENTER
      if (keyCode == 10) {
        fullTime = true;
        gameStartInfo = timer = halfTime = false;
      }
    }
    

    What I want is this: If there is text halftime and I press button T, timer starts from 45:00.

    I tried that like that, but without success. Only for a split second timer start from 45:00 but then goes back to 00:00

      // 84 = t, T
      if (keyCode == 84) {
        if (halfTime){
          startTimer();
          timer = true;
          gameStartInfo = halfTime = fullTime = false;
          minutes = 45 + mill / (1000 * 60) % 120; 
        } else {
        startTimer();
        timer = true;
        gameStartInfo = halfTime = fullTime = false;
        }
      }
    
  • Wow, I would be surprised if no library already existed for all this - this is far too common a question for no one to have made one.
    P.S. Pls tell me if no such library exists. I have one and will upload it.

  • Yeah, please post it!

  • Ok. So I guess no one has made any library like it before. Also note that this is not a library to trigger time based events (though it may not be too difficult to add such functionality).
    Probably in two days time. It will (obviously) take longer to provide a complete documentation, but the library will be on GitHub with examples before the weekend.

  • Thank you!

  • edited January 2017

    Seriously @Lord_of_the_Galaxy? You really do not need to do this just for my example. I feel guilty right now. :( You guys are so unbelivable helpfull here that scares me ^:)^

    Edit: My horrible english tho :))

  • edited January 2017

    @Lord_of_the_Galaxy -- it is true that stopwatch timers are a common question -- looking forward to seeing what you come up with!

    While I haven't used them, the two timing-related libraries that I'm aware of in Processing Contributions (Utilities) are CountdownTimer by Dong Hyun Choi and TimedEvents by Jason Gessner -- it might be interesting to look at them for reference / comparison.

  • edited January 2017

    ^:)^ ^:)^ ^:)^ Thank you very much Sir! ^:)^ ^:)^ ^:)^

  • I still don't know how to get it on the Processing website. Trying to find out now. Will let you know then.

Sign In or Register to comment.