Problems with delay()

Often when we first started out learning how to program a microcontroller, we will start out with a project called the blinking LED. It is the hardware version of printing “Hello World” when you are starting out a programming language.

Most of the time we will tackle this problem with a function called delay(), or some variation of that. Does something like this looks familiar with you?

void loop()
{
	digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
	delay(500);                // wait for 0.5 second
	digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
	delay(500);                // wait for 0.5 second
}

This is a code based on an Arduino project by the way. It makes the LED toggle between ON and OFF in 1 second interval (1Hz).

The waiting problem

While the function works in a lot of ways, the waiting part is the main issue. Imagine yourself lining up in a queue. While you are lining up, are you stationary, or are you doing something else (like reading a book, or picking your nose) ?

If your answer is the latter, you can see the main problem, you are wasting the processing power of the microcontroller by just waiting. What will happen if there’s a signal that whoosh by in that period of 500ms? You will miss it entirely. Your microcontroller can achieve so much more than just waiting.

The solution – Timer

I use mainly Microchip’s PIC microcontroller in my projects, and I will go out of my way avoiding delay() function if I had to. A simple timer function works by using timer interrupt. If I am visualizing it with pseudo code, it will be something like this.

system_init()	// init system
{
	// your system initialization
	set_timer(delay_timer); // set a timer called delay_timer
}

main_loop() // the loop routine that runs forever
{
	if(check_timer(delay_timer, 1000)) // check delay_timer if 1000milliseconds flag
	{
		toggle_state(LED);	// toggle LED state LOW or HIGH
		set_timer(delay_timer); // reset the timer again for next check
	}
	
	// do whatever you want in the mean time
}

Of course it is a lot more complicated setting up this timer because:

  1. You have to write the function of check_timer() and set_timer().
  2. You have to handle interrupt function for checking the timer accurately.

Fortunately, there are a lot of people are doing just that already, you can google it up to see how other people do it. This is how I do it though! >> Timer <<

Arduino’s Solution – millis() and micros()

If you are using Arduino, you are in luck because you just have to use their millis() and micros() function! They are basically the same thing, but 100 times easier because you don’t have to handle interrupt or writing function yourself. This website has a good guide regarding using the millis() function. using-millis-instead-of-delay

The Takeaway

Using delay() will affect your microcontroller’s performance in a lot of ways. Try using a more efficient timer, and your project will thank you for going the extra mile!

Leave a Comment