Using millis() in an interrupt service routine?

I'm teaching myself hardware interrupts and timers. So, during a tutorial it was explained to me that delay() and millis() wouldn't work inside an ISR.

The tutorial featured a a button, an LED and serial communication with a serial monitor. The idea was that inside the loop() function there was a massive delay that prevented any inputs from the button. After that, an ISR was added and it executed a function to turn on/off the LED without bothering the loop() function.

The problem was that there was no de-bouncing the button and the LED would instead turn on and off rapidly or incorrectly. I looked up a solution on the forums and they used millis() and a container variable to create a time frame of when the button was pressed, so that any extra button inputs would be ignored until a certain amount of time passed.

Here's the function:

void my_interrupt_handler()
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  // If interrupts come faster than 200ms, assume it's a bounce and ignore
  if (interrupt_time - last_interrupt_time > 200) 
  {
     if(digitalRead(buttonPin) == LOW){
    toggleState = !toggleState;
    digitalWrite(ledPin, toggleState);
  }
  }
  last_interrupt_time = interrupt_time;
}

I tested it out and it does in fact work on the hardware. I wanted to understand why. From the information I looked up, it might be possible that millis() actually doesn't work if you try calling it multiple times inside an ISR but it will work if some time passes after an ISR and then you can call it in once, and that one time is from the point just before the ISR was called.

Can anyone tell me if this is true? I assume that delay() might not work because it might be tied to millis() in some way.

The value of millis() don't change(don't increment) during the interrupt routine, but you can read it current value.

The delay function uses micros, IIRC.

It's not that it won't work, it's that spending at least a millisecond in interrupt context is far too long.

The main problem with software debouncing in an interrupt, is processor burden. Because, the interrupt is called on every input transition, even if the ISR subsequently ignores it.

Yes, on an AVR processor delay() uses micros() to count off milliseconds. However, micros() uses the Timer0 overflow interrupt to keep a count of overflows. Inside an ISR, the micros() function will lose time after between 0.26 and 1.26 milliseconds.

...both of which are waaaay too long to spend in interrupt context.