Can you distinguish between the first time pressing a space bar and the second?

for my code, I need to use the space bar for both actions. but as one does something and the other does another, they end up clashing, is there any way to distinguish them?

my code is for a reaction time experiment, and the first space bar press should start the experiment and the second should record your reaction time, which should be the time from the square turning red and you pressing the space bar.

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);
    text("totalTime" + totalTime, 100, 100);

    if (r <= totalTime) {
      reaction = true;
      //timeOne = millis() - totalTime;
      //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;
        }
      }
    }
  }
}
1 Like
boolean spaceBarFirstTime = false;

then set it to true and evaluate it

1 Like