How to measure time between output and input?

Hi, I am building a reaction game, and am wondering how to measure the time distance between an output (like an LED) and an input (pushbutton), and have it show on the Serial Monitor. I am new to Arduino and may have missed something obvious. Many thanks in advance.

Use millis () - don't forget to store the result in unsigned long.

(Or, if you're really fast, micros())

what would the code for that look like? im a big noob :frowning:

unsigned long startTime = millis ();

thanks :slight_smile:

If you want to catch cheaters, make sure the switch is not closed BEFORE you turn on the LED.

  // Delay random time, 2 to 3.999 seconds
  delay(random(2000, 4000);

   // Check for cheaters
   if (digitalRead(InputPin) == LOW)
  {
    led.print("Jumped the gun!");
    delay(5000);
   }
  startTime = millis();
  digitalWrite(LEDPin, HIGH);
  while (digitalRead(InputPin) == HIGH)
    {
    // Do Nothing
    }

  unsigned long elapsed = millis() - styartTime;

:p

I will put the statement startTime = millis(); after digitalWrite just to make it slightly more "fair", although its highly likely that the actual time it takes to perform digitalWrite is incredibly small (20us) and thus wont matter anyway.

Many Thanks!

I think it is more like 6 microseconds (0.006 milliseconds)

Incredibly small: 3.4us -- per Arduino Fast digitalWrite - The Robotics Back-End