Reaction Time Game

Hi, I'm trying to make a simple reaction time based game. An LED lights up, and when it does, the program should start counting. I'm using the random function to make the time random, but when I run it, the delay doesn't happen at all and the randomTime variable keeps repeating. Here's my code

int ledPinRed = 5; //Red Pin
int ledState = LOW;

int buttonPin = 13;
int buttonState;

long randNumber;
long randomTime;

unsigned long startTime;
unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);

  pinMode(ledPinRed, OUTPUT);
  pinMode(buttonState, INPUT);
  
  randomSeed(analogRead(1));

}

void makeRandom() {
  randNumber = random(1, 10);
  randomTime = randNumber * 1000;

  Serial.print(randomTime);
  startGame();
}

void startGame() {
  unsigned long currentTime = millis();

  if (currentTime - previousMillis > randomTime ) {
    previousMillis = currentTime;
    unsigned long startTime = millis() - previousMillis;
    digitalWrite(ledPinRed, HIGH);
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH ) {
      digitalWrite(ledPinRed, LOW);
      long myTime = millis() - startTime;
      Serial.println(myTime/1000);
    } else if (buttonState == LOW) {
      digitalWrite(ledPinRed, HIGH);
    }
  }
}

void reset() {
  randNumber = 0;
}

void loop() {
  makeRandom();
}

Post your code on this forum in the reply box using the </> tags.
It is unlikely that you should be using delay for this as your program will stop while in the delay function.

ardly:
Post your code on this forum in the reply box using the </> tags.
It is unlikely that you should be using delay for this as your program will stop while in the delay function.

Instead of delay, I used millis()

void loop() {
  makeRandom();
}

Loop does what it says it loops, so makeRandom is called every time round - is that what you intended?

ardly:

void loop() {

makeRandom();
}




No. I want to run it only once until the button is pressed but I don't know how to do that.

VinceM:
No. I want to run it only once until the button is pressed but I don't know how to do that.

Turn that around. Run makeRandom() upon receiving a button press.

VinceM:

ardly:

void loop() {

makeRandom();
}




No. I want to run it only once until the button is pressed but I don't know how to do that.

Build your code up slowly step by step.
Make a sketch that does nothing but print "Hello world" over and over again every time loop is called.
Next add a button and change the sketch so that "Hello world" is only printed while the button is pressed.
Keep adding functionality gradually to your code. That way you always have a program that works and if it fails you will know exactly what you did that broke it.

Use plenty of print statements in the code so that you know where the logic is going and what the values of variables are. You will be surprised how often the code is not going where you thought it would because the values are different from what you expected. The computer is always right though.

ardly:
Build your code up slowly step by step.
Make a sketch that does nothing but print "Hello world" over and over again every time loop is called.
Next add a button and change the sketch so that "Hello world" is only printed while the button is pressed.
Keep adding functionality gradually to your code. That way you always have a program that works and if it fails you will know exactly what you did that broke it.

Use plenty of print statements in the code so that you know where the logic is going and what the values of variables are. You will be surprised how often the code is not going where you thought it would because the values are different from what you expected. The computer is always right though.

Okay. Thank you