I need help with a code (timer)

I have this code and I want to make the square disappear when clicking a key and two seconds after clicking it, the square reappears in another position.
Thanks!

int cuadrado=60;

float recX, recY;
void setup()
{
fullScreen();
cursor(CROSS);
fill(250);
frameRate(30);
}

void draw()
{
background(0);
rect(recX, recY, cuadrado, cuadrado);
}
void keyPressed ()
{
recX = random (width);
recY = random (height);

}

Hey, Ruben, welcome to the forum!

Great to have you here!

Here is an example. The variable showingRect indicates whether

  • we pause the rect or
  • we show the rect.

int cuadrado=60;

float recX, recY;

int timer; 

boolean showingRect=true; 

void setup() {
  fullScreen();
  cursor(CROSS);
  fill(250);
  frameRate(30);
  recX = random (width-cuadrado);
  recY = random (height-cuadrado);
  background(0);
}

void draw() {
  background(0);

  // Eval flag showingRect 
  if (showingRect) {
    // show rect
    rect(recX, recY, cuadrado, cuadrado);
  } else {
    // pausing, wait for timer 
    if (millis() - timer > 2000) {
      // NEW RECT 
      showingRect=true;
      recX = random (width-cuadrado);
      recY = random (height-cuadrado);
    }
  }
}

void keyPressed () {
  // pause the rect
  timer=millis();
  showingRect=false;
}

Warm regards,

Chrisir

Thank you very much for your help.
i have another question.
If I want to print the exact time in which I have clicked the key as it would be.




 hour() + minute() + second()

example



int cuadrado=60;
float recX, recY;
int timer; 
boolean showingRect=true; 
String timeString = ""; 

void setup() {
  fullScreen();
  cursor(CROSS);
  fill(250);
  frameRate(30);
  recX = random (width-cuadrado);
  recY = random (height-cuadrado);
  background(0);
}

void draw() {
  background(0);

  // Eval flag showingRect 
  if (showingRect) {
    // show rect
    rect(recX, recY, cuadrado, cuadrado);
  } else {
    // pausing, wait for timer 
    text(timeString, 100, 122); 
    if (millis() - timer > 2000) {
      // NEW RECT 
      showingRect=true;
      recX = random (width-cuadrado);
      recY = random (height-cuadrado);
    }
  }
}

void keyPressed () {
  // pause the rect
  timer=millis();
  showingRect=false;
  timeString =  
    nf(hour(), 2) 
    + ":"
    + nf(minute(), 2)
    + ":" 
    + nf(second(), 2);
}

Thank you very much but I think I did not express myself well I meant to put the time in the console. It is to later be able to use that time in something else

println( timeString );

Remark

this is time in hour, minute, second,

NOT the millis of the 2 seconds timer

Okay, but if I want to know the time in milliseconds that passes between each time I click a key, how do I do it?

just store the time and measure the difference

timer2=millis()…

like I have shown

OR the time between the box appears and you click a key?

1 Like

thanks.
that’s exactly what I want to do

that was a question.

a OR b?

when clicked

int result = millis() - timer2;

timer 2 is millis()- timer?

my apologies, timer2 is just millis();

when you measurement starts

Sorry, it is a policy of this forum not to write full code for newbies, that’s why I am giving only hints at the moment. Not because I am lazy, haha…

It’s so that you can figure it out by yourself.

I want to mention that when you plan a “reaction time test” you want to have a random time passing before the rect appears

I understand what you say haha
That would be the idea but for a beginner that would be even more difficult to do and if it is already difficult for me to do the timer

1 Like

Just instead of 2000 define a variable randomWait as random(1200,3200);

i do that is correct?
int cuadrado=20;
float recX, recY;
float randomWait= random(4000,10000);
int timer;
int timer2=millis();
int result= millis()-timer2;
boolean showingRect=true;
String timeString = “”;

void setup() {
fullScreen();
noCursor();
fill(250);
frameRate(30);
recX = random (width-cuadrado);
recY = random (height-cuadrado);
background(0);
}

void draw() {
background(0);

// Eval flag showingRect
if (showingRect) {
// show rect
rect(recX, recY, cuadrado, cuadrado);
} else {
// pausing, wait for timer
text(timeString, 100, 122);
if (millis() - timer > randomWait) {
// NEW RECT
showingRect=true;
recX = random (width-cuadrado);
recY = random (height-cuadrado);
}
}
}

void keyPressed () {
// pause the rect
timer=millis();
showingRect=false;
println(result);
}

I keep thinking about how to make the reaction time appear on the console