In this tutorial you will learn what you can and cannot do with interruptions on your Arduino plate and how. You will learn how with a few simple steps you can take advantage of one of the most interesting tools of Arduino (as well as any other microcontroller), so I’m sure that many of your projects will improve considerably (they will be more efficient and accurate).
This one (like the one I told you about using Arduino’s EEPROM memory) is one of the types of post I like best and that’s because you don’t need to buy any module or badge, it’s something you already had and you didn’t know how to use, it’s something you had and you were just wasting, it’s something that reminds me to tell you: know your Arduino! Okay, I’m not gonna go all Bruce Lee, either, but seriously, we spend a lot of money doing things that we could do with what we already have and using the interruptions in Arduino is a great example of this.
An interruption is basically to detect an event and perform something accordingly. Your Arduino has a series of pins (you can see which ones later) to which you can associate a module (a code segment) so that it is executed when in that pin changes from one state to another that you have previously established.
In fact, you’ve probably already used Arduino’s interruptions without knowing it. When you use millis () type instructions you are using interruptions. Does that sound familiar to you that the millis() function returns to 0 after 50 days with your Arduino connected? Exactly! That is also done with interruptions (although of another kind you will see in this post).
What Are The Interrupts Of The Arduino?
Like All codes, it works for whatever you want to use in your project. Has not helped you much Right? Well, to give you a little idea of what you can use the interrupts you get are a few examples:
- To detect changes like a push button that has been pressed.
- To determine when you have finished managing the memory EEPROM or Flash your Arduino.
- Controller alarm mode. This is an interesting feature of your badge that allows you to keep consumption to a minimum, leaving your Arduino in standby until some event happens. With it you can make your batteries last much longer. As this part is particularly interesting, I will write an article about it. In the meantime, I recommend you take a look at this post if you want to learn how to optimize your plate batteries.
- As a Watchdog. The idea is similar to that of the previous point and I also hope to make a post about this.
What I just told you as an advantage is at the same time an inconvenience. If your Arduino responds to the interruption regardless of the code being executed, what if a delay() or millis () type instruction was running? Because your Arduino attends to the interruption and returns where it was going, that is, now the real time and the time that has passed for your Arduino are not the same because the interruption started to run when the time that had elapsed since you connected your Arduino (Millis () instruction ) was 200ms your interruption was activated and it has lasted 20ms but for your microcontroller that time has not existed, that is to say, the value of the millis () function has not increased and now you cannot trust the elapsed time and the worst is that you may not even be aware that the interruption has happened. What if the engine in that car had to be on 200ms and nothing but 200ms?