# 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:** 17\
**Page:** 2

<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:24pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/21 "2020-11-12T20:24:47Z")

</div>

> [@ruben89](#):
>
> i do that is correct?

Does it do what you want?

---

<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:25pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/22 "2020-11-12T20:25:35Z")

</div>

> [@ruben89](#):
>
> I keep thinking about how to make the reaction time appear on the console

with full screen you don’t see the console anyway

do you mean println(result);

---

<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:27pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/23 "2020-11-12T20:27:04Z")

</div>

**How to do this**

This  
randomWait= random(4000, 10000);  
won’t work.

you need this command where you start a new rect otherwise the value is random once and keeps the value forever. Bad.

**same here:**

```auto
int timer2=millis();
int result= millis()-timer2;

```

you need to have

```auto
int timer2;
int result;

```

before setup()

BUT

say

`timer2=millis(); // start measurement`

and

```auto
result = millis()-timer2; // stop measurement  
println(result);

```

in the **correct places** in your code where you want to start and stop (measure) the timer!!!

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, 8:37pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/24 "2020-11-12T20:37:10Z")

</div>

> [@Chrisir](#):
>
> with full screen you don’t see the console anyway
> 
> do you mean println(result);

i need the result in console because after the program i need copy the times

---

<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:39pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/25 "2020-11-12T20:39:42Z")

</div>

> [@ruben89](#):
>
> i need the result in console because after the program i need copy the times

yes, follow my ideas above

I just finished here your Sketch

**by the way:** say frameRate(1900); or so, because a very low frameRate like 30 would destroy the measurements

---

<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:45pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/26 "2020-11-12T20:45:51Z")

</div>

don’t know probably we measure how fast your keyboard is also… 😉

```auto
343
300
351
294
320
311
300
260
340
255
321
319
30
549
294
401
291
350
275
400
284
345
289
240

```

---

<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:49pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/27 "2020-11-12T20:49:21Z")

</div>

I have this but in console when i click a button put a all the values 0

int cuadrado=20;  
float recX, recY;  
float randomWait= random(2000,6000);  
int timer;  
int timer2;  
int result;  
boolean showingRect=true;  
String timeString = “”;

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

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:** ![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:52pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/28 "2020-11-12T20:52:25Z")

</div>

please make sure you start the measurement in the correct place in your code.

When does the measurement begin?

and end it in the right section of your code.

**Hint:** setup() is not the right place and having one directly after the other won’t work either.

As I tried to explain:

> [@Chrisir](#):
>
> say
> 
> `timer2=millis(); // start measurement`
> 
> and
> 
> ```auto
> result = millis()-timer2; // stop measurement  
> println(result);
> 
> ```
> 
> in the **correct places** in your code where you want to start and stop (measure) 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, 9:09pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/29 "2020-11-12T21:09:21Z")

</div>

```auto
479
280
236
260
250
239
260
280
256
271
306
270
266
280
280
370
238
250
289
269
300
400
270
450
270
330
290
270
340
270
290
29662
343
340
350
300
348
335
310
320
290
275
320
279
380
280
250
361
311
280
339
521
311
350
330
291
260
284
290
1381
296
340
30

```

---

<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 14, 2020, 3:21pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/30 "2020-11-14T15:21:41Z")

</div>

hi chrissir i cant reply this days but i think im finished is this correct?

int cuadrado=20;  
float recX, recY;  
float randomWait= random(2000,6000);  
int timer;  
int timer2;  
int result;  
boolean showingRect=true;  
String timeString = “”;

void setup() {  
fullScreen();  
noCursor();  
fill(250);  
frameRate(1900);  
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);  
timer2=millis();  
}  
}  
}

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

---

<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 14, 2020, 10:27pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/31 "2020-11-14T22:27:55Z")

</div>

I think that’s correct!!

Did you test 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 14, 2020, 10:28pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/32 "2020-11-14T22:28:48Z")

</div>

> [@ruben89](#):
>
> randomWait= random(2000,6000);

This you must have where you start a new rect

---

<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 14, 2020, 11:30pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/33 "2020-11-14T23:30:48Z")

</div>

Yes and is perfect for my project

---

<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 14, 2020, 11:33pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/34 "2020-11-14T23:33:16Z")

</div>

I will try it

…,…

---

<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 15, 2020, 4:40pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/35 "2020-11-15T16:40:25Z")

</div>

Chrissir the game is going as I wanted to do it, thank you very much for your help.  
I hope everything goes very well  
bye.

---

<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 15, 2020, 4:55pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/36 "2020-11-15T16:55:40Z")

</div>

> [@ruben89](#):
>
> randomWait= random(2000,6000);

Thanks for your feedback.

This: `randomWait= random(2000,6000);` is important so that the time between appearances of rect is always different

---

<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 15, 2020, 5:17pm UTC](https://discourse.processing.org/t/i-need-help-with-a-code-timer/25377/37 "2020-11-15T17:17:21Z")

</div>

i do it and of course is perfect

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