{"id":82,"date":"2013-10-25T10:00:31","date_gmt":"2013-10-25T10:00:31","guid":{"rendered":"https:\/\/arduino-developer.com\/arduino\/?p=82"},"modified":"2022-11-28T17:00:30","modified_gmt":"2022-11-28T17:00:30","slug":"heartbeat-timer","status":"publish","type":"post","link":"https:\/\/arduino-developer.com\/arduino\/programming\/timing\/heartbeat-timer","title":{"rendered":"Heartbeat Timer"},"content":{"rendered":"<h4>A Example Heartbeat Timer<\/h4>\n<h5>In your apps main loop<\/h5>\n<pre><code>\n  HeartbeatTimer();\n<\/code><\/pre>\n<h5>The Heartbeat Function<\/h5>\n<pre><code>\n\n\/\/*******************************\n\/\/*******************************\n\/\/********** HEARTBEAT **********\n\/\/*******************************\n\/\/*******************************\nvoid HeartbeatTimer()\n{\n  static byte heartbeat_10ms_timer = 0;\n  static byte heartbeat_100ms_timer = 0;\n  static unsigned long heartbeat_last_1ms_time = 0;\n\n  while ((micros() - heartbeat_last_1ms_time) &gt;= 1000)      \/\/micros() provides exact 1mS timing, millis() does not!\n  {\n\n    \/\/-------------------------------\n    \/\/----- HEARTBEAT EVERY 1mS -----\n    \/\/-------------------------------\n    heartbeat_last_1ms_time += 1000;\n\n\n    \/\/&lt;&lt;&lt;&lt; Do every 1mS things here\n\n\n    \/\/-- Toggle pin 13 test --\n    \/\/static byte toggle = 0;\n    \/\/toggle ^= 1;\n    \/\/digitalWrite(13, toggle);\n\n\n    heartbeat_10ms_timer++;\n    if (heartbeat_10ms_timer &gt;= 10)\n    {\n      \/\/-------------------------------\n      \/\/----- 10mS HEARTBEARTBEAT -----\n      \/\/-------------------------------\n      heartbeat_10ms_timer = 0;\n    \n    \n      \/\/&lt;&lt;&lt;&lt; Do every 10mS things here\n    \n    \n    } \/\/if (heartbeat_10ms_timer &gt; 10)\n\n    heartbeat_100ms_timer++;\n    if (heartbeat_100ms_timer &gt;= 100)\n    {\n      \/\/--------------------------------\n      \/\/----- 100mS HEARTBEARTBEAT -----\n      \/\/--------------------------------\n      heartbeat_100ms_timer = 0;\n    \n\n    \n      \/\/&lt;&lt;&lt;&lt; Do every 100mS things here\n    \n    \n    } \/\/if (heartbeat_100ms_timer &gt; 100)\n  }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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() &#8211; heartbeat_last_1ms_time) &gt;= 1000) \/\/micros() provides exact 1mS timing, millis() does not! { \/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-timing"],"_links":{"self":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":6,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":671,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/82\/revisions\/671"}],"wp:attachment":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}