Make your Arduino more responsive

Modified on Fri, 30 Dec, 2022 at 4:47 PM

Why?

The more responsive your Arduino is, the quicker it is to react to external events such as a switch actuation, and the quicker it is to trigger actions accordingly. This is particularly important for projects where your Arduino must deal with :

  • Time-critical tasks (timed tasks, scheduled tasks, synchronized tasks)
  • Several tasks running in parallel
  • Several devices in parallel, such as sensors, motors, controllers (motion controllers, MIDI controllers, MP3 players, RTC chip…), and computers (for control and monitoring with software such as HITIPanel).



How?

By using interrupts

Interrupts are triggered by internal or external events (timer overflow, rising or falling edge on a pin…). Use interrupts for starting time-critical tasks (encoder reading, limit switch reading…). Remember to always keep the code inside an interrupt as short as possible.



By optimizing your code inside the loop()

At startup, your Arduino starts by running the code you placed inside the setup(). The setup() is typically the place where you initialize your Arduino (pin modes) and the connected devices (serial communications…).


Once the setup() is executed, your Arduino goes on by running the code you placed inside the loop(), and repeat the execution of the loop() indefinitely. The loop() is typically the place where you deal with all the tasks defining your project. Let’s say your Arduino is connected to several devices (motors, sensors, controllers, computers). What you would like is to have your Arduino read your sensors when needed, actuate your motors at the right time, and speak fluently with all your controllers and computers. And all of this done “at the same time”, or in other words, in parallel.


As you can see, there can be a lot of tasks to deal with, possibly running in parallel. Moreover, some of these tasks may take a very long time to complete.


So how should we manage all that? By keeping the execution time of the loop() as small and regular as possible.


Measuring the loop cycle time

The loop cycle time is the time that your Arduino takes to complete one execution of the loop(). This time can vary greatly from one cycle to another, depending on the tasks that are being executed. 


As seen above, you should keep the loop cycle time as small and regular as possible. HITIPanel lets you monitor this time directly in the Chart.


Refer to this example : Checking the loop cycle time.


Note : Check the impact of replacing blocking functions with non-blocking functions by looking at the loop() cycle time.


Using non-blocking functions

A blocking function is a function which prevents the Arduino program from doing anything else until that function has completed. In other words, it prevents any other task to run in parallel (except interrupts).


For instance, delay() is a blocking function which blocks execution of an Arduino program during a specified duration. It prevents the program from running any other task during that time.


Such a function placed inside the loop() becomes a problem when you need the loop() to repeat quickly (small loop() cycle time). For instance, let’s say you perform a sensor reading at each loop() cycle, and you need this reading to be done at least every 500ms. If you add delay(2) somewhere inside the loop(), for instance to handle a LED blinking every 2s, the loop() will repeat every 2s, and sensor reading will only be done every 2s.


There are many tasks which requires the loop() to repeat as quickly as possible (e.g. communication with a device, control and monitoring from a software such as HITIPanel, sensor reading at fixed rate…).


For these reasons, use of non-blocking functions inside the loop() is highly recommended to have a loop cycle time as small and regular as possible.


HITIComm libraries are all non-blocking. For instance, You can use our Timing library as a replacement for the function delay() to control task durations.








Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article