A Example Heartbeat Timer

In your apps main loop
    #----- HEARTBEAT TIMER -----
    heartbeat_timer()
The Heartbeat Function
import time

#*******************************
#*******************************
#********** HEARTBEAT **********
#*******************************
#*******************************
heartbeat_10ms_timer = 0
heartbeat_100ms_timer = 0
heartbeat_last_1ms_time = round(time.time() * 1000)

def heartbeat_timer():
    global heartbeat_10ms_timer
    global heartbeat_100ms_timer
    global heartbeat_last_1ms_time

    while ((round(time.time() * 1000) - heartbeat_last_1ms_time) >= 1):      #time.time() gives us seconds as a floating point value from the system clock
        #-------------------------------
        #----- HEARTBEAT EVERY 1mS -----
        #-------------------------------
        heartbeat_last_1ms_time += 1;


        #<<<< Do every 1mS things here



        heartbeat_10ms_timer += 1
        if (heartbeat_10ms_timer >= 10):
            #-------------------------------
            #----- 10mS HEARTBEARTBEAT -----
            #-------------------------------
            heartbeat_10ms_timer = 0


            #<<<< Do every 10mS things here




        heartbeat_100ms_timer += 1
        if (heartbeat_100ms_timer >= 100):
            #--------------------------------
            #----- 100mS HEARTBEARTBEAT -----
            #--------------------------------
            heartbeat_100ms_timer = 0



            #<<<< Do every 100mS things here
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

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