Printing millis() after button turns off LED

Hello,
I am trying to create a project where an LED turns on after a random time (between 1 and 10 sec) and then is turned off using a button. I also want to calculate the time between the LED turning on and off. My code successfully turns on the LED at the required random times and is successfully turned off when the button is pressed. The process of random LED on and button off continues as expected. However, Serial.println is not printing 'ledOff' and 'reaction' as noted in the code below. I have read through many examples and I have not found a solution. I would appreciate a hint or two, or a pointer to a related example.

Thanks,

Cam

long randNumber;
unsigned long ledOn,ledOff,reaction;
int outPin=10;
int inPin=2;
int reading;

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT);
  digitalWrite(outPin, LOW);         // Start with LED off
}

void loop() {
 
   randNumber = random(1000, 10000); // Random wait time (1 to 10 sec)
 delay(randNumber);  
 digitalWrite(outPin, HIGH);        // Turn LED on
   ledOn = millis();                     // Measure start time
 Serial.println(ledOn);                // Print start time ***** This works
   reading = digitalRead(inPin);   // Check state of button
     do 
     {
      digitalRead(inPin);       
     } while(reading==LOW);     // Check until button pressed
 digitalWrite(outPin, LOW);       // Turn LED off
      ledOff = millis();                // Measure finish time
 Serial.println(ledOff);              // Print finish time **** This does not work
      reaction = ledOff - ledOn;   // Calculate reaction time
 Serial.println(reaction);           // Print reaction time (ms) **** This does not work
   
}

digitalRead(inPin);

By itself, this does nothing.

    do 
     {
      digitalRead(inPin);       
     } while(reading==LOW);     // Check until button pressed

You are not changing the value of the reading variable so the do/while will never end

How is the input wired ? Do you have a pulldown resistor in place or is the input floating at an unknown voltage ? Consider using INPUT_PULLUP in the setMode() to activate the built in pullup resistor and wire the switch to take the pin LOW when turned on.

Thank you both, my project is working perfectly now. Both hints were useful. Now I know about pulldown and pullup resistors too. A code change and wiring in the resistor worked.

Regards,

Cam

long randNumber;
unsigned long ledOn,ledOff,reaction;
int outPin=10;
int inPin=2;
int reading;

void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(outPin, OUTPUT);
pinMode(inPin, INPUT);
digitalWrite(outPin, LOW); // Start with LED off
}

void loop() {

randNumber = random(1000, 10000); // Random wait time (1 to 10 sec)
delay(randNumber);
digitalWrite(outPin, HIGH); // Turn LED on
ledOn = millis(); // Measure start time
Serial.println(ledOn); // Print start time
reading = digitalRead(inPin); // Check state of button
do
{
reading = digitalRead(inPin);
} while(reading==LOW); // Check until button pressed
digitalWrite(outPin, LOW); // Turn LED off
ledOff = millis(); // Measure finish time
Serial.println(ledOff); // Print finish time
reaction = ledOff - ledOn; // Calculate reaction time
Serial.println(reaction); // Print reaction time (ms)

}

Some observations

Using INPUT_PULLUP would remove the need for an external resistor.

The pin numbers could be const byte rather int because their value is below 255 and it will never change

Very helpful,

Thanks,

Cam