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);
}
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.
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();
}
}
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!
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.
}
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”
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;
}
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.