How to Use Arduino Interrupts The Easy Way

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone and welcome so today we are going to be teaching our very single-minded Arduino how to multitask now of course there are tons of different ways that you can do this but today in particular we're going to be looking at interrupts into very simple ways to incorporate these interrupts so if you have questions I encourage you guys to join the community where you can watch all of these lessons live ask your questions on the spot and also of course afterwards because the community is open 24 7. what is an interrupt right first of all super rude right so your Arduino is executing its code line by line and then it's a way to interrupt it in the middle of what it's doing and say Hey you know can you do this thing over here just like take care of this real quick and then you can get back to what you're doing and continue reading the code it's great for reading sensors is at a very timed interval or when you have a button press or any type of internal or external signal that you want to respond to so you can see that I have two LEDs and the objective here is to have that red LED flashing at some rate whatever we'll pick it and then the blue LED when I press that button well that blue LED better turn on better turn on all right so this is kind of like the challenge that we're gonna try and solve with two different kinds of interrupts there are Hardware interrupts and software interrupts now Hardware interrupts rely on an external signal so like a button press uh sensor reading or something that triggers our interrupt and then there are software interrupts and these are internal to the Arduino using its timers so we're going to look at two examples and we're going to look at very simple examples so if you're just getting started out with Arduino this is really great to add some multitasking functionality and then of course there are more advanced techniques that we're going to look at in future streams all right so we have a blankety blank sketch and let's get started so how would we do this well first I'm going to declare my pins as variables so we got I'm going to call it red LED and red LED was hooked up to pin 12 I believe and then we have our blue LED and blue is just not spelling itself because it's like yeah it's already trying to sabotage my attempt here in proving that you know maybe we can get this to work without interrupts you know and then what else do we need we need probably oh the button we can't forget the button so the button pin and that is on pin four because I just moved it all right I think that's kind of it and of course we're gonna need a button state but I think I'm going to declare that locally so now we have our most prestigious setup where we'll set up our pin modes so pin mode for the red LED is going to be output and then pin mode for our blue LED now if any of you guys have ever tried this like trying to get the Arduino to multitask and have successfully done this without any interrupts or stuff I would like to hear and then pin mode for our button pin well we're using the internal pull-up resistor so we're gonna go um input pull up all right and I think that's it for what we need I'm just gonna shrinkify this space right here and get into the loop all right so how are we going to handle this because I want it to really detect my button press I'm going to have it read that button right off the bat first line of code you know like read my button all right so the button state so I'm declaring a variable right in this function a local variable and I'm calling it button state and how are we going to get the state of the button well we're going to digital read the button pin all right sounds good now if that button pin if the button States I guess the state of the PIN is equal to low and low because we're using the internal pull-up means the button has been pressed what's going to happen nope but we're not finishing our our scene right there we're continuing what's going to happen well we want to turn that blue LED on so we're going to turn it high okay sounds good to me now let's say that the button is not pressed so if the button State it's just a state is equal to high what happens well we want to turn that blue LED off I almost went off like that no we want to turn it low which means off we are sending zero volts to it all right so I think we have some conditional statements there so we've handled the button pressing right off the bat so then we want to arrange this red Blinky type of situation here so let's do the red LED or digital right the red LED High and what rate do we want let's do like uh maybe a 250 milliseconds you know kind of a fast blink and then we're going to digital right the red LED glow oh and we're going to delay for also 250. so we get a nice even blink you know not like a syncopated blink although that is cool for you know different animations and things so it looks right to me so let's upload now this code here and see what happens and it's asking to save I care not for saving famous last words right okay so so far so good look that red LED is indeed blinking now will that blue LED turn on uh oh okay it turned on yeah it's not real responsive like I was hoping that by putting our button read code right up front we'd we pretty much would be Problem free but as you can see those delays when we get later into the sketch prevents the button from reading and because we have fast delays it's catching more button presses if we were to make those delays even longer like say a second each well those are whole second blocks that were kind of stuck you know where we can't really do anything so yeah not as responsive as I'd like it to be so let's see if we can fix this and the first type of interrupt we're going to try and use is the hardware interrupt Hardware interrupts work on very specific pings of different Arduino boards for the Arduino Uno the hardware interrupt pins are pinned two and three so whatever you want to interrupt like the button is our interruptor you know whatever is the interrupter needs to be plugged into one of those pins and you can see that pin two is also known as interrupt Vector zero and pin 3 is interrupt Vector one now we're going to need to know Vector 0 and Vector one for our code so we're going to kind of like keep that in mind all right so I am moving the button from pin four to pin two now it's good practice unlike you saw me do to always unplug your Arduino when you're making changes in your electrical connection that just kind of like protects all your components and every time I approach the Arduino the components are like oh she's coming again we're gonna die we're gonna die so yes it's good practice to actually unplug your Arduino before making changes even the simplest changes I know seems kind of lame but you forget and then when you're working with other devices that are more sensitive then yeah they get they can get quite unhappy all right so how does one Implement a hardware interrupt number one plug your interruptor into the correct pin you know two or three you can also look at your board's data sheet and determine how many they have like the Arduino Uno has two other boards have more so you might have more options in case you want many Interrupters now you can have more than one interrupter but they can only run one at a time you can't have like multiple interrupting it becomes far too rude your sketch becomes just way rude right now we're just kind of like casually rude you know interrupting the Arduino from doing what it was doing all right so that's step one step two is we need to create and interrupt service routine and so this is a function that contains all the code of what we want to happen once the interruption occurs so in our case what we want to happen is for that blue LED to come on so that would be our interrupt function and so you're are familiar with void setup void Loop and so now we're going to create void call it whatever we want you know and then put the code in there that we want to use that we want to happen and then third is once that function happens you have it in your code you want to attach that function to the action like the actual button press here is the attach function that we're going to use in the setup section once we've written our ISR or interrupt service routine function this is how we attach it to the actual action so you can see the function is rather easy it does take a couple parameters that are somewhat self-explanatory like attach interrupt you know that that kind of describes itself so the first parameter it takes is the interrupt Vector not the pin we connected it to pin two but pin 2 is interrupt Vector zero so you would put a zero where that is and then your ISR which is your interrupt service routine name whatever you name it and we're going to name it something and that'll go in there and then the mode and then you see for the mode there are four options all in caps it's got to be caps for it to work just like high or low with your digital rights so it has Rising so when an input goes low to high that's when it triggers it falling when it goes high to low low when it just stays low and then change whenever the input changes so for a button change is kind of good you know because it's either low high and it can kind of track those changes so I think we're going to use change now one of the things you're going to see a lot when people use Hardware interrupts is this interrupt Vector situation will look a little bit different because wouldn't it be so much easier if we just put a pin in there because then you can create a variable for your pin and if you change boards you just change it in that variable it's so much better so is there a way to do that and yes and so there's another function called the digital to pin interrupt and you see that I have inserted it where our Vector information was so it's still the attach interrupt but instead of putting that zero where you have to look up the you know the vector you can just put your digital to pin interrupt function and in between the parentheses where it says pin you put a PIN number and it returns that number in the Arduino so you don't have to worry about tracking that also makes it much easier to swap boards for your project in case your project grows and you need even more pins and then the rest of the same is the same you have your interrupt service routine and your mode so those are the advantages of instead of putting your vector number in there is to use the digital pin to interrupt function function within a function basically all right so how do we use this oh we have to remember that pin the button pin is now pin two so that's that's important so our pin modes are pretty much the same for the setup and remember we have to add our service interrupt routine we had to attach it in the setup and we need a name for it we haven't created it so how are we going to name it now when you create additional functions other than the void setup or the void Loop you'll see it's like about 50 50. some people kind of put them at the top all of their functions that then get called in the main Loop or they put it at the very bottom the choice is yours because we're going in order I guess I'll put it right here at the top so we're going to start with void and you can already see it's all hap it's all happy it's like we're going to create a new function so I'm going to call it button interrupt I know real creative right button interrupt and you're gonna create it just like your Void setup and your Void Loop function with the double parentheses and then an open curly bracket so in this code what do we want to happen if the button is pushed if it is pushed so we want to take that button state and digital read the button pin basically kind of like what we did before we can probably copy paste a lot of this and one thing I'm noticing is that button state it's declared kind of like down here and we'll get to that I think I'm going to change the way this is done because there's something really important about Hardware interrupts where you want to make sure that your variables update within your ISR routine so probably we can copy and paste a lot of this code right here and move this statements here the checking of the button I'm going to go ahead and cut and I'm going to paste it here all right and I'm just going to fix some of this formatting a little bit all right and so if the button to get the button State we're going to digital read the button pin and if it's been pressed turn the blue light on if it's not been pressed turn it off so that's going to be our interrupt service routine our ISR so that's already written and that looks good there now which means I don't need this button situation here now one thing that I'm going to go back to up here in setting up our variables anytime you're using an interrupt service routine that uses a variable inside like button state that is constantly changing you want to make sure you declare them in a very special way and that is using the word volatile so you want to go volatile volatile int and our volatile int is the button State and I'm not going to really you know give it a valuable a valuable a variable so why are we using the word volatile and this prevents the Arduino compiler from optimizing it and putting it in the storage register like with all your other variables it actually puts it in SRAM so it can be updated so for instance say that this is a variable that changes all the time like sensor readings button pushes by the time you interrupt that variable could have changed if it appears elsewhere in your Loop and that variable could change well your interop service routine could be running on the old value of the variable so it's important to use the word volatile the keyword volatile anytime you're using Hardware interrupts with a variable inside all right and to remember too in your interrupt service routine you don't want to use other things that also use other forms of interrupts like delays or no no Millis you know that sort of thing you want to stay away from using those and so if you need to use an interrupt that has a light going on and off well normally we do out with delays you know so how would you do that and we're going to get to that in a later example but for now all we're doing is checking button states which is not requiring any type of delays all right so we have our ISR written let's attach it so to attach it it's attach interrupt easy to remember and we are going to put in you can do Vector 0 if you want but I'm going to put in that function I'm going to like put it in here so we can use pins so digital pin to interrupt so what's the digital pin that we want to interrupt and that is our button pin or you can put a PIN number but it's good practice to use our our variables and what's the name of our ISR well up there I called it void button interrupt so it's button interrupt and you want to use the same capitalization as you did before and then after that we have those four options Rising falling change because this is a button that's like on off on off you know because I'm gonna press it a lot I want to use change so that should that should be that should be good and then our void Lube honestly doesn't really change because we want that Reda led to turn on off on off we're going to leave it at the 250. so anytime we push this button it's going to say heck hold hold up hold your horses right here we're going to quickly carry this out right there and proceed to this and it happened so fast that any type of delay is almost unnoticeable so let's see how this works and I'm going to go ahead and upload this this situation right here oh it's angry about something all right oh is equal to high and digit digital right is spelled wrong there we go hopefully that's it yes see we all set Good Vibes we all sent Good Vibes all right so now I'm going to press it and I'm getting my stance on I told you I was gonna press it a lot I'm gonna press it a ton guys it is super responsive so our Hardware interrupt totally worked crushed it so I will say that Hardware interrupts are probably one of the more popular ones that I use it doesn't require anything extra as you guys saw it's one line of code in your setup function and then you write your function and put whatever you want in there except delays Millis and anything you know that uses interrupt and that includes printing to the serial monitor yeah so you definitely don't want to use those in your isrs so software interrupts unlike the hardware interrupts depend on internal timing you know so it's not listening for anything externally and so now we get into Arduino timers now the subject of Arduino timers can get very complex but there are easy ways to implement it so we are going to look at one such beginner-friendly way to implement a a timer now this is perfect for things that you need to happen at a certain time which is a little bit different than our button press our button press is just like whenever you press me whenever you press me your software timers are great for turning lights on and off you know at a certain frequency or you know interval checking for sensor readings at a certain interval so it just keeps doing those checks and the rest of the program can run uninterrupted not interrupted uninterrupted all right so how do we Implement a software interrupt now software interrupt depends on 3 three timers your Arduino Uno has three timers timer zero you know timer zero timer one timer two and uh that's it zero one and two there's three timers I was wanting to go to timer three so timer zero is very important because that also handles your delays your Millis so one would assume that's probably not a good idea to use time or zero so that leaves timer one or two available and I'm gonna go with timer one for this lesson the easiest way to kind of manipulate this timer and make it easier for us is to use a library there's a great Library called timer one yeah that's why we're using the uh timer one of all the timers so let's install this Library first to install this Library I just go to the Arduino playground section of the site go to this first one right here and it is a Google code situation and I just go to the first one so go ahead and download that zip file or if you're watching this at a later date upload the or download the most current version once you download uh external Library like a zip file like that then you can go into sketch include library and then add a zip Library this is going to open up your browser you select the zip library and it'll install it now sometimes I find certain libraries you have to restart Arduino got to shut down and restart and then the library appears and it functions this one I didn't have to shut down it just worked right out of the box so that's good so of course with libraries in our code view here we have to attach it so let's attach our library or included I should say and that's with the pound include and let's include our timer one and I think it is a number one oh no it's not it is actually spelled out it's a spelled out one all right so we've included our library and let's see what we have to do here red LED and you know what we are going to change up this circuit because instead what we're going to do is forget the button you know I'll even unhook the button we're just going to deal with these two LEDs because again this is about timing so we're going to keep the red LED blinking at that 250 but then the blue LED why don't we blink it like once every second you know so we're just going to Blink that blue LED once every second and like the hardware interrupt this particular Library uses two special pins on the Arduino Uno and that is pin 9 and pin 10 and I believe our blue LED is indeed connected to pin 11 so I'm gonna have to move it and I'm gonna put it on 10. so I'm already going to change it in the code here I'm going to remove the button and the mouse is like well I'm moving all your code all over the place so let's do that I'm going to go and change the blue pin to pin 10 so this button is getting unhooked it kind of matters not it's doing nothing and heck I'll even unhook the ground just to really put it in the corner it's in the corner now so our blue LED in order for it to work with this Library it needs to go into nine or ten I'm putting it in 10 and again I did the No-No this thing is still plugged in should unplug it do your things plug it back in we no longer need this volatile State situation going on here all right so here we do need an ISR still you know we do need a block of code where we can say hey we want this to happen if the interrupt is triggered so let's see well we don't have a button interrupt so what's the what's this routine I'm going to call it blue blink not real Creator but you know I use my my creativity elsewhere so it's like it's gone for tonight it's gone so no cool variable names all right we don't we don't have any button situations going on so what do we want to happen in this blue blink well we want a digital write the blue oh wait a minute normally the way I would do this is digital write it high delay for a minute but not a minute a second then digital right low and delay it for a second but remember we can't use delays in this thing so or Millis so we're gonna have to get creative so here's a little trick there's two ways I've done this one way which we've seen in previous Arduino courses is to use if statements and assign a state to the LED and use a string is it on is it off and have that if statement check repeatedly if it's on if it's off and then have it do the opposite that requires no delays but here's a fun little one-line trick that I wanted to show you guys so one of the ways you can do this is digital right the blue LED and what are we going to digital write high or low why not write it by the digital read of the blue LED all right let me explain myself let me explain myself if you've guys have never seen this before so we're gonna digital write the blue LED because we want to be able to turn it on and off and this exclamation point is the word not so not this thing right here so in essence what we're doing is we're taking a digital read of the blue LED is it high or is it low so say say it's high and we need to turn it low so it's going to digital write the blue LED not hi so that leaves low right or say that it is low and we need to turn it on so it's going to digital read the blue LED and say oh it's low we need to turn it on so write that blue LED not low that means you got to write it high because we're not not writing it low not low all right you guys get the idea so so this little operator here is a very you know clever little guy in order to kind of flip-flop anytime you need to flip-flop this is kind of an interesting way to do it without using delays or you can do the if statements I think both are just fine the if statements are more lines of code you know so some frown upon that but you guys know my philosophy if it's a thousand lines to do something that takes five lines to do if it works because it works right all right so we have all that so this is our void blue blink all right so we're gonna remember what we called it and here we're getting into our void setup and we have our red LED our blue LED is outputs we no longer have a button so that can go away oh bye-bye and we don't need this attached because this has to do with Hardware interrupts and not software interrupts so we're going to use two functions that come from the library and you can read the library documentation so timer one we need to initialize it so we're going to initialize it and then put these parentheses here now what do we put in the parentheses we want to put the interval that we want at which we want this to happen in micro seconds so one second is a million microseconds so help me write this out guys it's six zeros one two six all right no commas no commas so you are going to have to basically convert your timing into microseconds all right so theoretically our blue blink is gonna run every second all right the next thing that we have to put in here and again with timer one is similarly to the way we did the hardware interrupt we have to attach it so we're going to attach this interrupt and we are going to attach it to Blue blink so you can see that this requires a lot less parameters super easy now you can manually set these timers slow down the timer using altering the chipsets using prescalers that allows you to get a lot more fine control of the timer than this method but if you just want something to happen at a regular interval this library is makes everything super super easy all right so I think I think that's what we have and then the loop kind of stays the same we really haven't touched the loop so the loop is just going to keep going it's going to keep turning on and off that red LED and then the timer is going to interrupt that sequence every second and flip flop the blue LED on or off via our one line of code and let's upload this look at what I did so we have a parenthesis here and this parentheses but this parentheses belong to this guy so we need an extra parentheses here I've been stingy with my parentheses so Arduino doesn't appreciate that parentheses and curly brackets it's like don't be stingy with that stuff all right guys look at that so we have our red LED blinking at that rate of 250 milliseconds and you can see that there's no interruption although technically there is an interruption because that's how all this is working but it's responsive I guess is what I'm trying to say and so this is kind of cool for applications where especially you are taking sensor readings because imagine making a prop or a robot that needs to respond to environmental cues putting those responses and checking of sensors and things like that in a interrupt service routine is great because it doesn't interrupt what's going on with the rest of the code and it's not going to miss any one of those cues it's also useful for animations because you can have different animations going on at the same time so there are a lot of ways to get Arduino to multitask and using these interrupts is just one of those ways there's also Millis controlling timers at a more advanced level but I wanted to gear this towards beginners where if you're just getting started with your arduinos you probably Arduino projects you probably already run into this where you're trying to do two things at once and the timing isn't just quite right it's missing buttons presses it's not sensing something on time time and these are two quick fixes to help you handle your Arduino multitasking these are shot live you can ask questions in real time I talk to you in real time if you are watching this after the fact and you are a Community member we'll post up your questions the community is open 24 7 and I'm on there every single day to help you guys out our members are awesome and helping you out and the goal is to make us better Builders Fabricators with our creative Electronics projects our animatronics and Robotics projects so guys check you out later I will include this code on my website the link is going to be in the description so kind of like scan it down and there'll be additional explanations all right guys see ya because you know somebody is just like I'm I'm done with this I'm done with interrupts and now she our dog Ripley is interrupting me and if you hear The Barking it is like multiple interrupts which I said is rude one interrupt now multiple interrupts see you guys
Info
Channel: Rachel De Barros
Views: 78,201
Rating: undefined out of 5
Keywords:
Id: SXZkX3cJqDs
Channel Id: undefined
Length: 33min 28sec (2008 seconds)
Published: Mon Mar 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.