Close

Adding interrupt routines for the interface

A project log for Milapli

An open source/hardware meteo station, also monitoring other environemental parameters

audrey-robinelAudrey Robinel 11/20/2014 at 02:210 Comments

As of today, i had a little box showing some parameters. After some time, it would show another parameter on the second line. However, what if i wanted to check another parameter when i want?

For that, i need to have a button to change what is shown on the LCD. The problem is that reading the sensors is made in a blocking way, meaning that i can't just poll the button state, because i will often miss the presses. A solution to this problem would be to modify the libraries so that it becomes non-blocking, but i don't feel like doing so. Another solution is to use interrupts.

Indeed, Atmega chips can handle multiple interrupts, and i can have a function executed whenever a pin status change occurs. Nick Gammon wrote a nice tutoriel about it here. Using this trick, i was able to capture the button presses.

In interrupts, delay() is disabled, as it relies on interrupts too. You can enable interrupts within interrupts, but it can be complicated. So to debounce the button, i have declared a variable as "volatile long" to store the last button press time (obtained with "millis()"). If enough time has passed since the last button press, my function does something, otherwise nothing happens.

When a good button press is recorded, i reverse the value of a volatile int variable, and end my function there.

In the loop function, a parameter or another is printed, depending on the value of the int variable.

Variable used by an interrupt must be declared as "volatile", so that the chip loads the variable value each time it uses it, instead of using the previously loaded value. As the interrupt can alter those variables any time, i ensures that the latest value is used.

Thanks to interrupts, one can design an user interface for a project , whatever the chip is programmed to do. It is possible to check any pin in the interrupts.

I will later include github links to the code for this example.

Discussions