{"id":4544,"date":"2023-12-15T14:32:07","date_gmt":"2023-12-15T14:32:07","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4544"},"modified":"2023-12-15T16:29:23","modified_gmt":"2023-12-15T16:29:23","slug":"heartbeat-timer","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/timer-and-timing\/heartbeat-timer","title":{"rendered":"Heartbeat Timer"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">A Example Heartbeat Timer<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">In your apps main loop<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    #----- HEARTBEAT TIMER -----\n    heartbeat_timer()<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">The Heartbeat Function<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\n\n#*******************************\n#*******************************\n#********** HEARTBEAT **********\n#*******************************\n#*******************************\nheartbeat_10ms_timer = 0\nheartbeat_100ms_timer = 0\nheartbeat_last_1ms_time = round(time.time() * 1000)\n\ndef heartbeat_timer():\n    global heartbeat_10ms_timer\n    global heartbeat_100ms_timer\n    global heartbeat_last_1ms_time\n\n    while ((round(time.time() * 1000) - heartbeat_last_1ms_time) >= 1):      #time.time() gives us seconds as a floating point value from the system clock\n        #-------------------------------\n        #----- HEARTBEAT EVERY 1mS -----\n        #-------------------------------\n        heartbeat_last_1ms_time += 1;\n\n\n        #&lt;&lt;&lt;&lt; Do every 1mS things here\n\n\n\n        heartbeat_10ms_timer += 1\n        if (heartbeat_10ms_timer >= 10):\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\n\n        heartbeat_100ms_timer += 1\n        if (heartbeat_100ms_timer >= 100):\n            #--------------------------------\n            #----- 100mS HEARTBEARTBEAT -----\n            #--------------------------------\n            heartbeat_100ms_timer = 0\n\n\n\n            #&lt;&lt;&lt;&lt; Do every 100mS things here<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Example Heartbeat Timer In your apps main loop The Heartbeat Function<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[347],"tags":[],"class_list":["post-4544","post","type-post","status-publish","format-standard","hentry","category-timer-and-timing"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4544"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4544\/revisions"}],"predecessor-version":[{"id":4557,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4544\/revisions\/4557"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}