How to use interrupts in Arduino projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i'm doing something i have not done before i'm redoing one of my old videos many reasons for doing this 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 i'm 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 can't work this way somebody is interrupting me again huh i guess this is okay in this video because it's about arduino interrupts if you're 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 it's 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 he's 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 it's 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 let's 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 today's 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 we'll 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 miley's becomes our new timestamp or the most recent led state change when we run this code we'll 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 let's 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. don't 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. let's connect the real thing let's 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 let's 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 how did you like the new version of this interrupt video did it make sense to redo it can you think of any other videos of mine that might need to be redone this way as well let me know in the comment section below i will see you in my next video ciao [Applause]
Info
Channel: Mario's Ideas
Views: 6,743
Rating: undefined out of 5
Keywords: Arduino, interrupt, interrupts, ISR, tutorial, attachinterrupt, push button, how to, blink
Id: EQuE7fGZizg
Channel Id: undefined
Length: 12min 37sec (757 seconds)
Published: Sat Sep 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.