Reading a sensor for a short time

I have a photocell reading light level. When the sensor goes below threshold I want to read what it is collecting for the next 500 milliseconds then disregard any readings after that. What would be the best way to do this?

Keep track of the time before the sensor goes below the threshold. If it's below the threshold, check if it's been 500 ms since it was last recorded as above the threshold

if (sensor is above threshold)
  set lastTimeSensorWasAboveThreshold to now
else
  if (it's been less than 500 ms since lastTimeSensorWasAboveThreshold)
    do something with the reading