# 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:** 1\
**Showing post:** 5

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

```

---

_[View the full topic](https://discourse.processing.org/t/reaction-time-experiment/19813)._
