Arduino Delay function

May 10th 2018

So a while back I was debugging some code and found the processor stuck in a while loop.  I had a simple time out system in the code, like:

to=1000; //wait up to 1 second
while(!flag && t0>0)

	delay(1); //Arduino delay 1ms


The code was simple enough but never timed out.  As it turns out the Arduino delay for the SAMD21 uses the systick interrupt to do the delay and I had the interrupts turned off...   

Although this was a simple problem to work around, it points out that regardless of what libraries you use, you must understand how the libraries work, else you are doomed to create problems like I did.  

Another delay problem I ran into was playing with Atmel's example code for FreeRTOS.  Specifically the ASF included a delay function where the delay function modified the systick counter and waited for it to count down.  However FreeRTOS used the systick timer as well.  So the first person who tries to use the ASF's delay function with FreeRTOS would end up cursing when things broke.