LED Heartbeat

This routine can be called within the main execution loop() of a sketch to cause the onboard LED (or other device hooked to an I/O pin) to blink at a specified rate. It is an easy way to give some kind of visual indication that the device is still alive.

To use, call the ledBlink() function from within the main loop(). As long as the function is called with a greater frequency than the blink rate, it will work as expected. A fast spinning loop will be very accurate, but a slower loop may not blink as regularly.

// Define these at the top of your sketch.
#define LEDBLINK_PIN    13    // I/O pin connected to the LED.
#define LEDBLINK_MS     1000  // Blink rate (in milliseconds).

void setup()
{
  // For ledBlink(), set LEDBLINK_PIN to output.
  pinMode(LEDBLINK_PIN, OUTPUT);
}

void loop()
{
  // Blink the LED to let the user know we are still alive.
  ledBlink();

  // Do something.
}

//
// LED Heartbeat routine by Allen C. Huffman (www.appleause.com)
//
void ledBlink()
{
  static unsigned int  ledStatus = LOW;  // Last set LED mode.
  static unsigned long ledBlinkTime = 0; // LED blink time.

  // LED blinking heartbeat. Yes, we are alive.
  // For explanation, see:
  // http://playground.arduino.cc/Code/TimingRollover
  if ( (long)(millis()-ledBlinkTime) >= 0 )
  {
    // Toggle LED.
    ledStatus = (ledStatus==HIGH ? LOW : HIGH);

    // Set LED pin status.
    digitalWrite(LEDBLINK_PIN, ledStatus);

    // Reset "next time to toggle" time.
    ledBlinkTime = millis()+LEDBLINK_MS;
  }
} // End of ledBlink()

Share