# I need help with a code (timer)

**URL:** https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377
**Category:** Coding Questions
**Tags:** homework
**Created:** [November 12, 2020, 6:23pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377 "2020-11-12T18:23:52Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 6:23pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/1 "2020-11-12T18:23:52Z")

</div>

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);

}

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:19pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/2 "2020-11-12T19:19:26Z")

</div>

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.

```auto

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

---

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 7:26pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/3 "2020-11-12T19:26:38Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:27pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/4 "2020-11-12T19:27:45Z")

</div>

```auto

 hour() + minute() + second()

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:30pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/5 "2020-11-12T19:30:50Z")

</div>

example

```auto

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);
}

```

---

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 7:38pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/6 "2020-11-12T19:38:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:40pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/7 "2020-11-12T19:40:43Z")

</div>

`println( timeString );`

**Remark**

this is time in hour, minute, second,

NOT the millis of the 2 seconds timer

---

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 7:44pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/8 "2020-11-12T19:44:37Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:48pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/9 "2020-11-12T19:48:42Z")

</div>

just store the time and measure the difference

timer2=millis()…

like I have shown

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:50pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/10 "2020-11-12T19:50:49Z")

</div>

> [@ruben89](#):
>
> want to know the time in milliseconds that passes between each time I click a key

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

---

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 7:52pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/11 "2020-11-12T19:52:44Z")

</div>

thanks.  
that’s exactly what I want to do

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:53pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/12 "2020-11-12T19:53:17Z")

</div>

that was a question.

a OR b?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:54pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/13 "2020-11-12T19:54:09Z")

</div>

> [@Chrisir](#):
>
> just store the time and measure the difference
> 
> timer2=millis()…
> 
> like I have shown

when clicked

`int result = millis() - timer2;`

---

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 7:58pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/14 "2020-11-12T19:58:19Z")

</div>

timer 2 is millis()- timer?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 7:59pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/15 "2020-11-12T19:59:15Z")

</div>

my apologies, timer2 is just millis();

when you measurement starts

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 8:01pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/16 "2020-11-12T20:01:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 8:05pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/17 "2020-11-12T20:05:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 12, 2020, 8:11pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/18 "2020-11-12T20:11:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 8:19pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/19 "2020-11-12T20:19:14Z")

</div>

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);  
}

---

<div class="post-metadata">

### Author: ![ruben89](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben89](https://discourse.processing.org/u/ruben89)
#### Post date: [November 12, 2020, 8:20pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/20 "2020-11-12T20:20:06Z")

</div>

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

[Next page](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377.md?page=2)
