# Reaction time experiment

**URL:** <https://discourse.processing.org/t/reaction-time-experiment/19813>\
**Category:** Coding Questions\
**Created:** [April 15, 2020, 7:01pm UTC](https://discourse.processing.org/t/reaction-time-experiment/19813 "2020-04-15T19:01:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![14mmss](https://avatars.discourse-cdn.com/v4/letter/1/d07c76/32.png) [@14mmss](https://discourse.processing.org/u/14mmss)\
**Post date:** [April 15, 2020, 7:01pm UTC](https://discourse.processing.org/t/reaction-time-experiment/19813/1 "2020-04-15T19:01:13Z")

</div>

hello, I am working on a reaction time experiment. I need to record the time from the second screen onwards until the rectangle changes from black to red in a random time stamp between 2 and 7 seconds and as a reaction to that, the user presses the space bar and the time counting stops. I have come up with this for now but I am stuck as the millis() function is not subtracting properly, so the time that is shown in the timer is the whole time that the window is open, not just since pressing space to start the experiment. and I am also stuck with how to select the random time stamp with the random function. can someone help?

```auto
boolean startTimer = false;
int startingTimer;
int CONSTANT = 1000;

void setup() { // setting up the initial screen
  startingTimer = millis();
  size(800, 800);
  textSize(36);
  text("Reaction time experiment", 180, 200);
  textSize(32);
  text("Press SPACE to start", 250, 400);
}

void draw() {
  if (keyPressed) {
    if (key == ' ') {
      startTimer=true;
    }
  }
  if (startTimer==true) { // dicates what happens after space is pressed
    background(255);
    int miliseconds = (millis() - startingTimer); // subtracts the time spent from pressing space from the real time
    int seconds = miliseconds / CONSTANT;
    miliseconds -= seconds * CONSTANT;
    fill(0, 0, 0);
    text((seconds) + ":" + (miliseconds), 100, 100); // makes the timer pretty
    rectMode(CENTER);
    rect(400, 400, 400, 300); // inital black rectangle
    float r = random(2, 7); // selects random timestamp
  }
}  

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 15, 2020, 7:50pm UTC](https://discourse.processing.org/t/reaction-time-experiment/19813/2 "2020-04-15T19:50:11Z")

</div>

Hello,

> [@14mmss](#):
>
> the time that is shown in the timer is the whole time that the window is open, not just since pressing space to start the experiment.

If space bar starts the experiment\timer you should consider putting this in your keyPressed routine:  
startingTimer = millis();

`:) `

---

<div class="post-metadata">

**Author:** ![14mmss](https://avatars.discourse-cdn.com/v4/letter/1/d07c76/32.png) [@14mmss](https://discourse.processing.org/u/14mmss)\
**Post date:** [April 15, 2020, 8:09pm UTC](https://discourse.processing.org/t/reaction-time-experiment/19813/3 "2020-04-15T20:09:46Z")

</div>

thank you, I managed to get that to work. However, now I have the problem that the rectangle doesn’t change color when the time is reached. This was working when I was only doing it for the seconds, but now that I added it to take into account the milliseconds it doesn’t work anymore.

```auto
boolean startTimer = false;
int startingTimer;
int CONSTANT = 1000;
int s;
int m;

void setup() { // setting up the initial screen
  size(800, 800);
  textSize(36);
  text("Reaction time experiment", 180, 200);
  textSize(32);
  text("Press SPACE to start", 250, 400);
}

void keyPressed() {
    if (key == ' ') {
      startTimer=true;
      startingTimer = millis();
      
      s = int(random(2, 6.1)); // selects random timestamp for seconds
      m = int(random(0, 1000.1)); // selects random timestamp for miliseconds
    }
  }

void draw() {
  
  if (startTimer==true) { // dicates what happens after space is pressed
    background(255);
    
    int miliseconds = (int(millis()) - startingTimer); // subtracts the time spent from 
    // pressing space from the real time
    int seconds = miliseconds / CONSTANT;
    miliseconds -= seconds * CONSTANT;
    fill(0, 0, 0);
    text((seconds) + ":" + (miliseconds), 100, 100); // makes the timer pretty
    
    rectMode(CENTER);
    rect(400, 400, 400, 300); // inital black rectangle
    
    text(s, 200, 200); // TAKE THIS OUT
    text (m, 250, 250); // TAKE THIS OUT
    
    if (s == seconds) {
      if (m == miliseconds) {
        fill(255,0,0);
        rect(400, 400, 400, 300);
      }
    }
  }
}  

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 15, 2020, 10:09pm UTC](https://discourse.processing.org/t/reaction-time-experiment/19813/4 "2020-04-15T22:09:57Z")

</div>

> [@14mmss](#):
>
> if (s == seconds) { if (m == miliseconds)

This condition may not ever be met because millis() is quietly updating in the background and you are only checking it every draw cycle which is 1/60 of a sec (default framerate is 60 fps).

The chance that they are exactly == at the instance you check is not likely.

Other options are \>, \<, \>=, \<=, != …

Pick one that makes sense and try it.

Reference:

> **[Equality, Relational, and Conditional Operators (The Java™ Tutorials \> ...](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html)**
>
> This beginner Java tutorial describes fundamentals of programming in the Java programming language

`:)`

---

<div class="post-metadata">

**Author:** ![14mmss](https://avatars.discourse-cdn.com/v4/letter/1/d07c76/32.png) [@14mmss](https://discourse.processing.org/u/14mmss)\
**Post date:** [April 16, 2020, 7:28pm UTC](https://discourse.processing.org/t/reaction-time-experiment/19813/5 "2020-04-16T19:28:59Z")

</div>

thank you! I realized that. Now I’m having a problem with having the calculation of the reaction time work. It should show the time that it takes for the user to press space since the square turned red. Could you help?

```auto
boolean startTimer = false;
int startingTimer;
int CONSTANT = 1000;
int r;
int timeOne;
int timeTwo;
int totalTime;
int reactionTime;
boolean reaction;

void setup() { // setting up the initial screen
  size(800, 800);
  textSize(36);
  text("Reaction time experiment", 180, 200);
  textSize(32);
  text("Press SPACE to start", 250, 400);
}

void keyPressed() {
  if (key == ' ') {
    startTimer=true;
    startingTimer = millis();

    r = int(random(2000, 6000)); // selects random timestamp
  }
}

void reaction() {
  reactionTime = millis() - totalTime;
}

void draw() {

  if (startTimer==true) { // dictates what happens after space is pressed
    background(255);

    totalTime = (int(millis()) - startingTimer); // subtracts the time spent from pressing space from the real time
    fill(0, 0, 0);
    rectMode(CENTER);
    rect(400, 400, 400, 300); // inital black rectangle
    text("reaction" + reactionTime, 400, 200);

    if (r <= totalTime) {
      reaction = true;
      //timeOne = millis() - totalTime;
      text("totalTime" + totalTime, 100, 100);
      //text("timeOne" + timeOne, 200, 200);
      //text("timeTwo" + timeTwo, 200, 200);
      fill(255, 0, 0);
      rect(400, 400, 400, 300);
      fill(0, 0, 0);
      if (keyPressed == true) {
        if (key == ' ') {
          //timeTwo = millis() - totalTime;
          reaction = false;
        }
      }
    }
  }
}

```

---

<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:** [April 16, 2020, 8:25pm UTC](https://discourse.processing.org/t/reaction-time-experiment/19813/6 "2020-04-16T20:25:08Z")

</div>

> [@14mmss](#):
>
> It should show the time that it takes for the user to press space since the square turned red.

then when it turns red, set timer to millis()
