Time elapsed correlated with FrameCount

Hello guys and girls,
Hi made a function to see seconds elapsed from the the first frame.
I did like that

int Time=0;
void setup (){
 frameRate(30);
  }
void TimeElapsed () {
  if ((frameCount%30)==0)  {
   Time+=1;
   } 
      textSize (100);
      text (Time, -width, -height);
      text( second(), width/2, -height);
     } 

But there is an offset between the counter shown on the Processing’s screen and the clock of my computer.
The counter does not go fast enough.

What did I make wrong?
Thanks for your help.

Time releated functions in Processing:

  • millis() - time since the start of the program (in milliseconds)
  • second() - computer time
  • minute() - computer time - min
  • hour()

  • check processing reference for more info

I really need to have time correlated with frame, because sometimes I stop the program with noLoop and run it again. I make sounds with an other software and I need to see the time exactly.
Normally with frameRate(30); I should have each 30 frames, one seconde, no?
My program below

int TimeFrame;
int sec,counter; 

void setup (){
 counter=0;
 frameRate(30);
  }
public void settings() {
   size(800, 800);
  }  
void draw  () {
   background (0);
   println (frameCount);
   TimeFrameElapsed (); 
   }
   
void TimeFrameElapsed () {
if ((frameCount%29)==0)  {
   TimeFrame+=1;
   } 
      fill (255);
      textSize (100);
      text (TimeFrame, width/2, height/2);
      println (TimeFrame);
      text(second(), width/4, height/2);
  } 

Thanks for your help.

it’s normal because frameRate(30) means Processing “tries” to reach 30 frames per second, but it’s not guaranteed.

you need “delta time” between the previous and current frames, and this thread may be helpful

Applying jeremy’s idea on your code

float TimeFrame;
int lastMillis = 0;
int sec, counter; 

void setup () {
  counter=0;
  frameRate(30);
}
public void settings() {
  size(800, 800);
}  
void draw  () {
  background (0);
  println (frameCount);
  TimeFrameElapsed ();
}

void TimeFrameElapsed () {
  //if ((frameCount%29)==0) {
  //  TimeFrame+=1;
  //} 
  int m = millis();
  TimeFrame += float(m - lastMillis) * 0.001;
  lastMillis = m;
  fill (255);
  textSize (100);
  text (TimeFrame, width/2, height/2);
  println (TimeFrame);
  text(second(), width/4, height/2);
}

note that the displayed times are different because second is the clock time (like, your clock on the wall) and millis is based on the milliseconds elapsed since you started your sketch.

1 Like

Hi micuat!

Actually I’m looking for a timer that turns off when I stop the loop, with key == ‘£’, and start again when I run the loop.
I did this, it doesn’t work.

float TimeFrame;
int lastMillis = 0;
int sec, counter; 
 

void setup () {
//  noLoop ();
  counter=0;
  frameRate(30);
}
public void settings() {
  size(800, 800);
}  
void draw  () {
  background (0);
  TimeFrameElapsed ();
 // println (frameCount);
  text (TimeFrame, width/2, height/2);
 
}

void TimeFrameElapsed () {
  //if ((frameCount%29)==0) {
  //  TimeFrame+=1;
  //} 
 
  
  int m = millis();
  TimeFrame += float(m - lastMillis) * 0.001;
  lastMillis = m;
  fill (255);
  textSize (100);
  text (frameCount, width/2, height/4);
  text (TimeFrame, width/2, height/2);
  println (TimeFrame);
  text(second(), width/4, height/2);
}

            
void  keyPressed () {
  if  (key == '£') {   
             noLoop();     
            }
else      { 
        loop();          
             }    
                    
}
1 Like

yes, because right now the “delta time” would be still counting when you set it noLoop. I guess you have to make a flag (boolean variable) - that turns on when you pause the sketch and goes off when you resume it. If the flag was on (it was paused) then you skip adding the delta time so you discard the time elapsed while the sketch was paused. I will give this to you as an “assignment” but if you try and still have issues feel free to post it!

1 Like

Hi it’s a bit complicated for me because I don’t how to use flag.
I did only this for the moment but I need quickly a solution because I have an other part of my work to do depending of this timer stopping and starting again.

int lastTime = 0;
int delta = 0;
int time1, time2;
int actualTime; 

void draw(){
  frameRate (10);
  delta = millis() - lastTime;
   print ("delta: "); println(delta);
  lastTime = millis();     
}

void keyPressed(){
  
    if ((key == '`') || (key == '£')) {   
          noLoop(); 
            }
         else  { 
          loop();
   }
  time1 = millis();
  print ("time1: "); println(time1);

}

void keyReleased(){
   time2 = millis() - time1; // delta betwwen KeyPressed and KeyRelased
  print ("time2: "); println(time2);
 // actualTime+= ??
  print ("actualTime: ");
  println (actualTime);// Here I need to see time running with the add of the last time when the loop was off.
}


1 Like

Hello
I have tried this. When I push a key time, the time elapsed adding itself.
When I released a key, it stop

int lastTime = 0;
int delta = 0;
int time1, time2;
int actualTime;

public void settings() {
  size(800, 800);
}  

void draw(){
  background (0);
  
text (actualTime, width/2, height/2);
}

void keyPressed(){
 
  noLoop();
  time1 = millis(); // when i push a key, time elapsed add itself
  print ("time1: "); println(time1);
    background (0);
  
text (actualTime, width/2, height/2);

}

void keyReleased(){
 loop();
   time2 = millis() - time1;
   actualTime+= time2; 
  print ("actualTime "); println(actualTime);
  
    background (0);
  
text (actualTime, width/2, height/2);
  time1 = millis();
 
}

nice attempt!

I see that noLoop/loop makes it a bit harder to track what is happening. I would suggest replacing them with a variable (in this case running) and draw function is executed only when running is true:

int lastTime = 0;
int delta = 0;
int time1, time2;
int actualTime;

boolean running = true;

public void settings() {
  size(800, 800);
}  

void draw() {
  if (running == false) return;
  background (0);

  text (actualTime, width/2, height/2);
}

void keyPressed() {
  time1 = millis(); // when i push a key, time elapsed add itself
  print ("time1: "); 
  println(time1);

  running = false;
}

void keyReleased() {
  time2 = millis() - time1;
  actualTime+= time2; 
  print ("actualTime "); 
  println(actualTime);
  time1 = millis();
  
  running = true;
}

and let’s adapt the code from above to use “deltatime”

float timeFrame;
int lastMillis = 0;

boolean running = true;

public void settings() {
  size(800, 800);
}  

void draw() {
  if (running == false) return;
  
  int m = millis();
  timeFrame += float(m - lastMillis) * 0.001;
  lastMillis = m;
  
  background (0);

  textSize(100);
  textAlign(CENTER, CENTER);
  text(timeFrame, width/2, height/2);
}

void keyPressed() {
  running = false;
}

void keyReleased() {
  running = true;
}

it’s almost there but the time still counts when the key is pressed. The easiest way to fix this would be to reset lastMillis when key is released - so basically we are “faking” that no time has passed between when you started pressing button and when you released (note that draw function is skipped during that time)

float timeFrame;
int lastMillis = 0;

boolean running = true;

public void settings() {
  size(800, 800);
}  

void draw() {
  if (running == false) return;
  
  int m = millis();
  timeFrame += float(m - lastMillis) * 0.001;
  lastMillis = m;
  
  background (0);

  textSize(100);
  textAlign(CENTER, CENTER);
  text(timeFrame, width/2, height/2);
}

void keyPressed() {
  running = false;
}

void keyReleased() {
  running = true;
  int m = millis();
  lastMillis = m;
}
1 Like

Thank you very much. :kissing_heart:

There is an easier way to find the Delta Time.

deltaTime = 1000 / frameRate

this works because frameRate is equal to cycles per Second, so you can get the time per cycle → delta time

1 Like

thanks for the tip! however, as I wrote above, framerate is not guaranteed. Your suggestion can be used as a handy (but not accurate) trick and also it works perfectly when you output png files to create a gif. But if you want to work with, for example, sound in real time then you need to stick to millis; otherwise the video will eventually lag behind the sound.

frameRate is the Current FrameRate, which the max can be set trough frameRate();
frameRate changes based of the actual frameRate.

You’re right! I missed that.

However strictly speaking it is still not accurate as frameCount takes a moving average

I am not sure if you accumulate error when you use the averaged framerate as a delta time.