Optimal way of making timed code

Use a library like MobaTools which is interrupt driven and does it all for you in the background.