Reaction time experiment

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?

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

Hello,

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

:)

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.

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

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:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

:)

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?

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

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