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)
}
}
