How to use interrupts in Arduino projects
For starters, the quality of sound is really bad. Then i did not script the voiceover for this video, so you have a lot of ease and ass and generally i sound like im falling asleep and of course i made the rookie mistake of having an annoying loud background music, but the main reason for doing this. Video again is that i did not cover the interesting topic in a very interesting and engaging way. Oh come on, i mean i cant work. This way somebody is interrupting me again huh. I guess this is okay in this video, because its about arduino interrupts if youre not familiar with them. Stick around Applause. Have you ever struggled to write a sketch? That is not reacting well to time critical, external events in your system. You are using digital, read to detect such events, but is it the best way to do it its not? This is what interrupts are for. Ah, by the way this is my dog ernie uh. You probably wonder why he is here: he is going to be featured in this video because he knows a thing or two about interrupts with the help of ernie. I will give you a simple explanation on how interrupts work. Ernie has two favorite things in life sleeping and eating. He could hang around the kitchen by the fridge waiting all day for someone to open it and give him something to eat, but that seldom happens and it would seem like a waste of time and energy.
So, being a smart dog as he is, he goes to the next room for a snooze, but each time someone opens a fridge. He jumps to his feet and springs to the kitchen. We may say that his sleep is interrupted in the kitchen. He approaches the open fridge and in those moments he gets lucky sometimes and gets a treat when hes finished with the food. He goes back to his other favorite activity. He goes back to sleep. Arduino interrupts work, the same way down to the t. Ernie performs his usual activity external event occurs. Normal activity is interrupted, task corresponding to external event, is performed dog resumes his normal activity. How does arduino interrupt work? They occur in response to an external event, such as an external interrupt, pin going high or low. When such event happens, the processor takes immediate notice, pauses the currently run code and save its execution state runs. A small chunk of code called interrupt service, routine isr. In short, and then returns back to whatever it was doing before, not all digital pins support interrupts. This is, for instance, the arduino nano and for this type of microcontroller, only digital pin, 2 and 3 can be used for this purpose. Here is the table that shows interrupt pins for the most commonly used arduino microcontrollers. There are three ways that interrupt can be triggered when the signal at the pin changes from low to high this mode is called rising.
The next one is when the signal changes from high to low and its called falling and the last one is called change and in this one interrupt is triggered on both the rising and falling edge of the signal at interrupt. Pin so lets look where programming of interrupts fits in your standard, arduino sketch each arduino program has a setup function which initializes and sets initial values it is executed once after microcontroller is started or reset, then you have a loop function where the code is executed. In a round robin fashion, we define interrupt in the setup function using attach interrupt method there. We specify the interrupt pin in this case. It is digital, pin 3. Then the name of the function that is going to be executed when the triggering condition is met, or, should i say, interactive, service, routine and finally, the mode which defines when the interrupt should be triggered in this case. Interrupt will kick in when the signal on digital pin 3 would change from low to high. So you can see the loop function code being executed, while the signal at interrupt pin is low when it changes to high the execution stops and the interrupt is triggered. We go to the isr defined for this interrupt and execute the code associated with it. When this is done, we go back to the loop function and resume execution from the place we left off. If we would define interrupt with a falling mode, then the loop function is executed, an interrupt kicks in only when the falling edge is detected at interrupt.
Pin after the interrupt is triggered. The process is exactly the same. If we use the change mode, then if we encounter the signal spike at interrupt, pin the isr routine would be executed twice on both the rising and falling edges of the spike. If you are still watching this video, that means that you enjoy this content. The video like this takes a lot of time and effort to create if you want to support my work, the best way to do it is to subscribe to this channel. You can also consider becoming my patron for as little as one euro per month. The links to my patreon website and my paypal account can be found in the description below back to our todays topic. To demonstrate the use of interrupts. We would use this rgb led module. We will use the most basic blink sketch, which would cause this led to blink in one color and then without any changes to the original loop function. By providing external event like finger snap, we will try to change the color in which led is blinking. You probably are familiar with the blink sketch which uses delay function. This is not the one that well use here. We will use millis function. Instead, this function shows in milliseconds elapsed time since the sketch was started. If you are not familiar with that function, i have a whole new tutorial for you check it out. It is very useful. You will find out that extensive use of delay function in your program is not recommended.
So our blink sketch would look like this. We need few variables. First, one will store the elapsed time since sketch was started and the other one would store the time that has passed since the last change of led state. Then you have led variable that points to the arduino pin that is connected to anode of one of the rgb leds representing currently blinking color. So we start with pin 9, which is connected to anode or the blue led, and finally, we have led state. One is for led to be on, and zero is for led to be off in setup. We define led pin as output in loop. We take the time snapshot with millis and save it to ms from start variable. Then we check it against the last time. The state of led changed if more than one second passed since then we change the led state. We send the signal corresponding to that state to the led pin causing the led either to lit or turn off, and then time snapshot we took with mileys becomes our new timestamp or the most recent led state change. When we run this code well observe the following result: what we want to do with interrupts is to introduce external event, eg finger snap, which will not affect the interval with which the led is blinking, but would change the color of the led. So this is what we want to achieve. We do it by detecting the snap with the sound sensor.
Each time we detect it. There is a high signal that is sent to the interrupt, pin that kicks the interrupt and changes the color of blinking led before we adjust the blink sketch to add the interrupt and corresponding isr lets. Look at the way all the components should be connected together. Here is the arduino nano we connect, the rgb led to it so that common cathode is connected to ground of arduino, and the anodes of red, green and blue leds are connected to digital pins, 7, 8 and 9. dont forget to use current limiting resistors the sound Sensor is connected to ground and 5 volt pins of arduino to power it, and then signal pin, is connected to interrupt enable digital pin 3. lets connect. The real thing lets look at the changes we need to make in the blink sketch to introduce the color switch in setup. We need to set all digital pins connected to rgb, led as output pins and with the attached interrupt method. We define the interrupt on digital pin 3 with color swap function. Executed in case. Interrupt is triggered. Isr functions should be short. In our case we increase the led variable, so it points to a different pin representing different color, since we oscillate between pins 7 and 9. If, after that, increase led variable is equal to 10, we set it back to 7.. That is all there is to it. I sometimes add few additional lines of code to protect my sketch against glitches.
If we react to press of the button, sometimes a single press generates two single spikes with those lines of code. I make sure that only if 50 milliseconds pass since the last isr run, it can be run again. Lets load the code to the microcontroller and see if we can control the color of the led with the finger snap. Yes, we can with this type of external event, it is difficult to present all possible interrupt modes. It will be easier if i replace sound sensor with the push button, starting with rising mode. The color changes each time i press the button down releasing the button does not affect the color of the led now onto falling mode. You can see that when i press the button down, the color does not change only when i release the button, color changes and finally, in change mode. The color changes when i press the button down, but also when i release it as well. This is all for now.