// using an interrupt to do a 1 second process
// https://wokwi.com/projects/396818172976688129
//
// Built from
// https://learn.adafruit.com/multi-tasking-the-arduino-part-2/timers
// modified to do the code work of Blink without delay inside an ISR
// https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/

const int ledPin = LED_BUILTIN;  // the number of the LED pin
const long interval = 1000;  // interval at which to blink (milliseconds)
byte ledState = LOW;  // ledState used to set the LED
volatile unsigned long previousMillis = 0;  // will store last time LED was updated

void setup() {
  pinMode(ledPin, OUTPUT);
  // Timer0 is already used for millis() - we'll just interrupt somewhere
  // in the middle and call the "Compare A" function below
  OCR0A = 0xAF; // TIMER0_COMPA_ value
  TIMSK0 |= _BV(OCIE0A); // enable TIMER0_COMPA_vect ISR
}

void loop() {
  // delay(10000);
}

// Interrupt is called about once a millisecond,
ISR(TIMER0_COMPA_vect)
{
  // This is the main part of BWOD,
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis += interval; // update timestamp and account for slippage
    //   previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
Loading chip...chip-scope