A Example Heartbeat Timer

In your apps main loop

  HeartbeatTimer();
The Heartbeat Function


//*******************************
//*******************************
//********** HEARTBEAT **********
//*******************************
//*******************************
void HeartbeatTimer()
{
  static byte heartbeat_10ms_timer = 0;
  static byte heartbeat_100ms_timer = 0;
  static unsigned long heartbeat_last_1ms_time = 0;

  while ((micros() - heartbeat_last_1ms_time) >= 1000)      //micros() provides exact 1mS timing, millis() does not!
  {

    //-------------------------------
    //----- HEARTBEAT EVERY 1mS -----
    //-------------------------------
    heartbeat_last_1ms_time += 1000;


    //<<<< Do every 1mS things here


    //-- Toggle pin 13 test --
    //static byte toggle = 0;
    //toggle ^= 1;
    //digitalWrite(13, toggle);


    heartbeat_10ms_timer++;
    if (heartbeat_10ms_timer >= 10)
    {
      //-------------------------------
      //----- 10mS HEARTBEARTBEAT -----
      //-------------------------------
      heartbeat_10ms_timer = 0;
    
    
      //<<<< Do every 10mS things here
    
    
    } //if (heartbeat_10ms_timer > 10)

    heartbeat_100ms_timer++;
    if (heartbeat_100ms_timer >= 100)
    {
      //--------------------------------
      //----- 100mS HEARTBEARTBEAT -----
      //--------------------------------
      heartbeat_100ms_timer = 0;
    

    
      //<<<< Do every 100mS things here
    
    
    } //if (heartbeat_100ms_timer > 100)
  }
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *