Millisecond timing of an event?

edited August 2017 in How To...

Hi all, I'm very new to processing and having some troubles with timing events, specifically with recording and counting the amount of time needed. I've searched a lot on the forums and don't seem to find an answer that is 100% fitting my scenario.

Let's say I have an object appearing on screen, it'll appear for a certain amount of time and then disappear. I am wanting to save/record (perhaps print it to console after) the amount of time (in milliseconds) it takes from the moment the thing appears, to the moment the mouse is clicked. I am just confused on using millis() on getting a value and having a timer reset each time since any attempt, or if I should even be using millis().

I don't have a problem with the making objects spawn after time situation, but I'm having trouble of getting the milliseconds time from the moment they appear to the point when a mouse click happens (before they disappear). I got the spawning to occur without using millis(), but don't know if I should use millis() for this next task. Any assistance is greatly appreciated.

Tagged:

Answers

  • Try this next. A keyboard event will activate the timer. A mouse event will stop the timer and display the time difference. Notice a mouse event can take effect only after a keyboard event. Any keyboard event will reset the initial time.

    You can also check the timing utilities in the library manager as it provides example of how to use a timer.

    Kf

    boolean active=false;
    int ctime;
    int stopTime;
    
    void setup() {
      size(400, 600);
      fill(255);
      textAlign(CENTER, CENTER);
    }
    
    void draw() {
      background(0);
    
      if (active==true) {
        text("Time passing: " + (millis()-ctime) +" msecs", width/2, height/2);
      } else {
        text("Time passing: " + (stopTime-ctime) +" msecs", width/2, height/2);
      }
    
      text("INSTRUCTIONS: key to start, mouse to stop",width/2,height*0.75);
    }
    
    void keyPressed() {
      active=true;
      ctime=millis();
      stopTime=0;
    }
    
    void mouseReleased() {
    
      if (active==true) {
        active=false;
        stopTime=millis();
      }
    }
    
  • For some background on related problems in precise timing, see;

Sign In or Register to comment.