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
}