LESSON 28: Tutorial for Programming Software Interrupts on Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 28 on using the Arduino microcontroller so I need you to pour yourself a big cup of coffee sit back and get ready to learn how to program software interrupts on the Arduino I'm really excited about this lesson because this will allow you to take your Arduino projects up to that next level now let me explain a little bit about what we are trying to do here and what we really mean by a software interrupt okay so let me get a couple of things called up here real quickly alright so this is a very simple program you can see that I have my Arduino microcontroller programmed up to blink a red LED okay I am just simply blinking a red LED and the code for that is very simple we simply come in and digitalwrite the red LED pin hi we wait for a thousand milliseconds and then we digital right the red LED pin low and so we turn on pause a second turn off pause a second and then loop back on pause off pause and therefore we get the old program probably one of the first programs you've ever you ever wrote to blink an LED very simple okay now I'm going to tell you something the next step up which sounds like it would be real simple but turns out to be incredibly hard on something like the Arduino let's say that I wanted to blink the red LED on a second off a second and at the same time be blinking the yellow LED let's say on for a quarter of a second off for a quarter of a second so blink them the yellow LED independently and blink it faster well it turns out that that is really a very hard thing to do because what you have to understand is that the L that the Arduino does one line of code at a time and then goes to the next one and then goes to the next one so we right here turn the red LED on and then right here we are delaying for a thousand the thing to understand is the program stays at this step until that thousand milliseconds is up and then it goes to the next step so while I am sitting here waiting I can't be over there turning the yellow LED on and off so even though it sounds like a very simple thing it turns out to be something that the Arduino isn't really well suited for because the Arduino is a simple microcontroller has one program running at a time and has one main loop going so it's not like Python where you can have multiple programs running at once or multiple threads running at once and so what we need to do is we need to introduce the concept of an interrupt a software interrupt and what a software interrupt is is it's something that runs in the background it's a little it's a little part of the Arduino running in the background and as your normal main loop is executing this step this this step this step there is something called an interrupt that's programmed and on a certain event or at a certain time wherever you are and whatever you're doing it will stop run over and do this other step and then come back so it's an interrupt where you interrupt your normal program flow come over do something else and then come back okay now there are two types of interrupts there are hardware interrupts where that interrupt happens based on something happening on a pin like a certain pin goes high or a certain pin goes low you see something happening on the pin you run off and do the interrupts we will do that in a future lesson today the interrupt that we're doing is a software interrupt and that is I want to every so often every certain period of time run off and do something so it's like a separate time or running that when the timer comes up runs off does something else comes back and then continues the normal program flow so today we're going to learn software interrupts next lesson we'll probably do more advanced software interrupts then we're going to do hardware interrupts and then I'll show you some of the more complicated things you can do with it now one of the things I said is is that I'm illustrating this with this simple blinking the LED program because it's easy to understand but really some really important things would require interrupts sometimes when we're working with multiple sensors with the Arduino if you have something like a GPS a lot of times the GPS is just spitting out serial data and if you're over here working on something else and that that GPS piles up a bunch of data you're gonna overflow your buffer you're gonna be reading old stale corrupt incomplete data from the serial port so you need to have something like an interrupt so that every little bit you can run out and get the data off of the game off the serial port so there's a lot of really important functions to do with it so let's jump right in and start working on this first of all if you want to build this circuit that will be doing the demonstration on you can come here and this is a picture of it we have a yellow LED the yellow LED is tied through a current limit resistor to pin 10 the red LED through a current limit resistor to pin nine the current limit resistor can be between 200 and 500 ohms it doesn't really matter okay this is the circuit we can use also go to the link in the description below and it'll take you to the top tech boy dot-com lesson where we have the code and the links and all that sort of stuff so you can just go to the description of this video clink home click on the top tech boy link and it'll take you to this lesson on the most excellent website okay so you know what to build this circuit now the thing about the Arduino interrupts is as configured it will not do these software interrupts so you need a special library to do these interrupts and down at the link top tech boy comm in the description that links to my lesson on my website you can just click to get to this page but if you want to do it the old-fashioned way you can see the URL is HTTP colon slash slash code.google.com dot-com / archive / p / arduino - timer 1 / downloads and then you will come to this page and then on this page you will want to go to zip ok it's the timer 1 - r xi dot zip timer 1 - r xi dot zip and if you click on that it will download that zip file down here in the bottom of your brow in the bottom of your browser you will be able to see that you can click on it to open it and then this is the library that allows you to do an interrupt maybe not everyone has installed the library before so i'll go a little bit slow here sorry for you experts but i just never want to leave anyone behind in the dust so let me show you how you can get to let me show you how you can get to the arduino library ok so what you want to do is you want to open your arduino program ok as such and then you want to come under file and look at preferences and then right here under sketchbook location that shows you where you can find the library folder I like to just select it ctrl C and I've copied it then that's all I need I can close that and then I come back over here to my folder and then I can just click here on my folder and ctrl V it will post it will paste that location in then enter in boom I am in my sketchbook folder ok if you guys are experts you know how to do this but sometimes people get lost and trying to find this alright so now I am in the Arduino folder my Arduino folder yours might be somewhere else but if you do what i just said that'll help you find it and then you look in libraries and you you can see that i'll already have that so i'm gonna delete it so i can show you how to install it yeah alright so now here you want to create a new folder so I create new folder and again I'm in the Arduino libraries and I want to call this timer 1 ok timer 1 now what I want to do is go back to we're gonna go back to where we had downloaded this it looks like I need to maybe click on it again ok so we're gonna click on timer 1 - R 11 zip ok it comes down here we're gonna open it and there we have it and now we come back to the folder that has the timer 1 we select all this and we drop it in timer 1 okay so I have Arduino libraries I have my timer 1 folder open timer 1 and there is the library ok so that looks good so now we're gonna see how do we write this code that would allow us to simultaneously bring blink the two LEDs and so let's come over here and get started ok so I will just start with a new empty sketch ok I will make that fullscreen I think you can see that first thing I want to do is I want to make sure that I got that library in there correctly and so to load the library you put hash and then include and then open bracket timer 1 dot H and then close bracket and I'm just going to download this and see if I can do it without getting an error and I just want to make sure that we got that library installed correctly so I download it it wants to save the program okay save it not a problem ok and it looks like it is compiling and it looks like it downloaded without a problem so what does that tell me that tells us that we have correctly installed timer interrupts library okay now we need to do some some stuff we're going to start writing the code now so we're gonna need some variables one thing is if we are going to be turning this led the second LED on and off with an interrupt we got to keep track of whether the LED is on or off and so I'm gonna put string LED status and then I'm just going to initialize it just say initially we haven't turned it on so it is off alright and so this is just a little variable LED status that's going to keep track of whether our yellow LED is on or off okay now we are blinking two LEDs so we're going to need to give variable names for their pins and so we're going to have yellow LED and that's going to be equal to ten okay and then int red LED it's going to be equal to nine all right and that's just saying I'm hooking yellow yellow LED to pin 10 and red LED to pin nine and then I think that's all the variables we need we need to do always do our void set up so we're going to come here and we're gonna do our pin up mode yellow LED is going to be an output okay pin mode a red LED it's going to be an output okay and then now is when we actually get to the real business this is where we are going to define our interrupts okay and so we're gonna start our interrupts so I'm gonna say timer one right we included timer 1 up here okay but but the actual command is timer one the number one okay timer 1 dot initialize ok now this is how long do you want to run before you set off the interrupts maybe you would want to run for a second maybe 10 seconds maybe a you know maybe a hundredth of a second I don't know why don't we set it at a second okay and the units are in microsecond so if I want one second I would need to put in a million so 1 2 3 1 2 3 6 zeros ok that's a million microseconds so what I'm saying is I'm starting it's like setting an alarm clock I'm starting an interrupt and that interrupts it's gonna go off once a second so it's kind of like this is going to be running in once a second he's gonna say over here once a second over here so no matter where your main code is this alarm is gonna go off once a second ok now we've got to tell it when the alarm goes off what do you want to do well what I would like to do is I would like to run a program called blink yellow so I'm going to say timer 1 dot attached interrupts ok and if you do it right you see how it changes color so this is every second when the interrupt goes off what do you want to do well I want to do a function called blink yellow all right so what have we done here once a second an alarm is going to go off when that alarm goes off it is going to run down and do link yellow all right and notice attached interrupt the eyes upper case and you've got a spell it just right and if you spelled it right and you included the library it should turn a color so if I'm going to call this function blink yellow I better define it so I'm going to come down after the void loop and I'm going to define this and so what I will do is I will say void which means this is a function and I'm going to define void blink yellow got a top it exactly like before but then I got to give it the open and the close the old open close and now begins curly bracket okay now so then let's make sure this is the that's opened and closed and then the void blink yellow is open and closed and the void loop is open and closed got to keep your curly brackets you got to keep your curly brackets straight so let's see that would be the this would be in between here between the open and the close curly will be what is executed when blink yellow is called so I think we're ready to go so I'm going to say if and then if LED status equals equals on okay in other words if the LED is presently on what do I want to do well I want to turn it off so to begin the if statement open curly come down so if the LED if the yellow LED is already on what do I want to do well I want to judge it did you toll right what I want a digital write yellow LED and what do I want to digital write it okay I want to write it low why because if it's on I want to turn it off okay and then if I'm going to turn it off I better adjust the LED status so it remembers that it is now what off okay so let's think about this we come down every one second the alarm goes off it calls blink yellow and then it comes down here to void blink yellow and if the LED was already on what do we want to do we got to turn it off and then we've got to update our status to remember that it's off I think that looks really good and then at that point we can return and that just means leave this function all right what's the other possibility well if LED status equals equals off okay open curly come down did you toll right yellow LED high okay and I better I forgot up here this if statement has an open curly I need to put the closed curly and then this one has an open curly so I need to put a closed curly I hope I am keeping up with these right I feel like I've got an extra one here somewhere I think I do okay so let's see this one closes the void loop this one closes that if statement this one closes that if statement and I think I still have an extra one okay so if LED status equals equals off and I got to close that I am making all kinds of mistakes if LED status equals equals off if it's off what we want to do digitalwrite yellow LED hi we want to turn it on and then now that it's on we've got to adjust LED status and we got to say that that equals on so we always keep track of where we are I am afraid to even download this thing because I think I'm going to get errors I'm just gonna see if this is gonna yeah what what did I do red LED up in mode I didn't type in mode right okay you guys were probably yelling at me when I did that okay timer one blink yellow timer one was not declared timer one with a capital T so you guys probably saw this as you were going through okay it looks like it's compiling now and so that is looking that is looking good so I just want to make sure that I kind of got the errors cleaned out before we go on so now what do I want to do in the void loop I'm going to just do the good old flat fashion blink the red LED so what are we going to do did you toll right red LED and we're gonna go high okay and then what are we gonna do delay so let's say up here we are blinking the yellow every second using the interrupts well let's blink the red every quarter second so we'll say delay and we'll say 250 milliseconds which would be a quarter of a second then digit all right red LED low okay and then delay 250 okay so let's review what this program is going to do the main loop is going to sit there turn the LED on wait a quarter-second turn the red LED off wait a quarter-second turn the red LED on wait a quarter-second turn the red LED off wait a quarter second but while that's doing that up here we have this timer that is running and every second every thousand Millis are every million microseconds no matter where the program is this timer is gonna raise an alarm and doesn't matter where you are it's gonna leave and it's going to do what blink yellow LED what does blink yellow LED do well at the yellow LED is it's gonna what turn it on if the yellow if the yellow LED is on what's it gonna do turn it off okay so these two kind of simultaneous things should make them both blink so let's download this and see what happens okay let's look here all right look at that boom we programmed an interrupt the red LED is on off on off on off and that is based on that is based on this okay turn the red on turn the red off turn the red on turn the red off but besides that the yellow one is blinking on a second off a second on a second off a second on a second off a second okay and that that my friend is because of the interrupts all right that's because of the interrupts all right let's try something different let's say that we wanted to blink let's say that we wanted to blink it the other way so here we're going to put 250,000 microseconds that's a quarter of a second 250,000 microseconds is going to be the the interrupts rate and then we're going to come down here and we're going to delay a thousand okay and a thousand okay so now let's download that okay come back over here boom look at that the red LED on a second off a second on a second off a second while the yellow is going four times as fast okay so do you see that we can have these interrupts to do some really really important things really really important things and you can see that this interrupts down here is just done really quickly so it doesn't really impact the timing okay so if you write these things right it doesn't really impact the the timing of the main loop okay guys this is how you do a software interrupt I'm hoping that with a simple example this would allow you to then go away and do the more sophisticated things you would want with like a GPS where I really find that I need that I need interrupts one is with something like a GPS where it is is this cramming data at me and if I don't have an interrupt to go over and start pulling that data off the serial port I can end up in trouble and also there's some things like a directional cosine matrix which has to be run continuously and if you get off and ignore that then it can crash on you and so an interrupt is a very very powerful tool that you can use and I hope that this this example shows you how you can you can use it I hope this will allow you to begin to take your begin to take your Arduino programs up to that next level okay guys I hope you've enjoyed this lesson if you have think about giving us a thumbs up think about sharing it maybe subscribing to this channel love to hear your comments down below let me know what you're thinking let me know if this has worked for you or if you let me know the the places that you're using interrupts or what particular interrupt problems you're trying to solve okay palma quarter from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 81,544
Rating: 4.9550982 out of 5
Keywords: Arduino, Interrupts, Programming, Tutorial
Id: QhIm6e5AH44
Channel Id: undefined
Length: 25min 13sec (1513 seconds)
Published: Tue Mar 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.