LESSON 30: Advanced Software Interrupt Techniques for Reading Serial Data 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 30 on using the Arduino microcontroller and what we're going to talk about today is we're going to be talking about some advanced software interrupt techniques and in lesson 28 and 29 we sort of showed you the real simple thing just to get in and get out of a function to do something like turn something on or off but sometimes you really do want to do something more sophisticated in a software interrupt and that's what we're going to talk about today so what I need you to do is pour yourself a nice big cup of coffee and get ready for some power techniques on using the Arduino this is actually a real-world problem and it's a really vexing problem for the Arduino and let me kind of set up the situation of what we're what we're trying to solve here and I've got a I've got it shown here let's say that you want to do something with the GPS so this is the Adafruit ultimate GPS which is made for the Arduino it's a really great little GPS it's pretty easy to get it set up but this is the problem when you really want to go in and start doing something with a sensor like the GPS and there's a lot of sensors like this and so I'm just using the GPS as a general demonstration of what the problem is this is the problem the GPS just sits and spits out serial data okay and it spits it out really fast just bits it out at 9600 baud for the default baud rate and it doesn't listen really it just talks so when you power up this GPS it just starts spitting out data okay and what that means is the arduino it's not a client-server relationship it's just the GPS is spitting out data so what you have to program your arduino where it's sitting there with its mid up ready to catch the data okay so it's sitting there caching the data the problem is that the data coming in is very sophisticated it's a number of different what's called NEMA sentences that have all types of different data embedded in the data stream and in the Arduino then you need to go in and you need to start parsing that data well this is the problem if you go away to parse the data guess what happens the data keeps coming in all right and so how do you set something up where you go and you catch the data because this is the other thing you catch a NEMA sentence you go out and you start parsing it you get the information that you want well what's happened the GPS has crammed a lot of data into the serial buffer it's filled it up it's overflowed it's overflowed the buffer and so now when you go in to read the buffer the data is old and it's gonna be corrupt because you are gonna have more than one nema sentence and that last one is going to be chopped off in the middle and now you're generating a nightmare of a problem to try to parse it and figure out what's good data what's bad data and so to make it even worse the GPS doesn't just sit and constantly send data it sends a big burst of data and then it waits and then a big burst of data and it waits okay so this is an example of to really deal with this you're gonna have to use interrupts now the our friends at Adafruit have written like a really nice library cuz they're like crazy good coders and stuff and so they write wrote a really nice library where you can use this with the GPS but the problem is you can use the Arduino with the GPS the problem is is that when you want to go start doing something yourself those libraries a lot of times don't work because you've got to go in and you've got to start interacting with it like imagine that you wanted to not just parse Nima's sentences collect and parse nema sentences from the GPS but you might also want to be working with a pressure sensor or something like that so you've got to know how to go in and solve this problem so we're gonna look at is how to solve this problem and and and what I'm going to show you would while I picked out the GPS to show you it would really work with any problem where you're having to deal with a sensor that's just spitting out data at you at the arduino and so this this would work not just for the GPS but it would work for other for other software as well okay first step is we need to go in and we need to get a better interrupt library okay i in Lesson 28 I had you download a simple one and as we are getting to these more sophisticated techniques we need a little bit more refined library and so where I need you to go is if you go to top tech boy calm in the link below it'll take you right to this lesson 30 and it will have this link for you but if you like to do it the old-fashioned way I am at HTTP colon slash slash github.com slash Paul stop region that's PA u LS t o FF r e GE n slash timer one timer capital T 1 capital o so time r1 and then you will get here and this has got this is a different Paul there's some great Paul's out there working on the Arduino but you'll get to this page then just click on the clone or download okay you can say download zip and then when you open it you will see this time one master you need to go to your Arduino folder okay which I think is here nope you need to go to your Arduino library folder which is here and so you can ah I won't step you through it I stepped you through it in Lesson number 28 basically what you want to do is you want to get the contents of this folder and you want to put it in your library so you want to get rid of your old-timer one library and you want to replace it with this time or one library does that make sense you take that old timer one library you throw it away and then you put in this timer one library and I showed you how to load a library in less than twenty eight so I'm not going to go through it again other than show you where to get it okay so I have done that so I now have the new timer one library what we are going to do is we are going to open we are going to open a fresh Arduino IDE okay and I'm going to show you how to solve this problem so this is going to be a longer lesson but I would really appreciate it if you would go through it with me okay so what do we need to do in order to do this and also just let me show you that I've actually done it here if you look at this I am doing one thing blinking the LED while at the same time I'm getting this data that's coming off of the GPS and this is a very slow blink so that's a long delay in there and during that blink at periodic times the GPS is spitting data at me and I have been able to get this to work so let me let me step you through this okay so let me take a sip of coffee and let's just jump right in okay there's two libraries that you are going to need you're going to need the include timer one dot H and remember this is the new one not the old one also it's probably good after you change that library out if you kill your arduino ide and then open it again just to make sure that you're getting the the latest and greatest library that you just installed okay and then we are going to include software serial dot H this software serial is what we're going to use to talk to the GPS the GPS is sending a serial data and well we will be reading that over software serial I'm gonna download this just to sorry okay I'm going to download this so you hash include timer1 H and then hash includes software serial dot H I'm sorry I was not over here as I was as I was doing this while ago okay I'm gonna download that just to make sure that it works okay to make sure that I've got that library without a problem okay it looks like that was happy just with a new library like to make sure that I can include it okay now I'm gonna need some variables alright so I'm gonna have int flag the flag is just going to be a useful thing that tells me if I have a if I've received a new batch of cereal so that will make sense later but it's just let me know if I have new data I'm blinking an LED so I'm gonna need red red LED and that is hooked to pin nine so I set that up okay I'm gonna be playing around with a timer in the debugging and so forth so let's just create a long variable timer string I will be reading the nema sentence coming from the GPS into a string variable called NEMA and I'm going to start with that empty so we will go like that to make sure that we're starting with an empty string when I read from the GPS when you're reading over software serial the the old read string function doesn't work so we've got to read it in one character at a time so we need a chart to read the string into it one character at a time so we will have a char C then we'll build NEMA from the char C's that were that we're reading in okay normally you know how I tell you not to cut and paste go to the link below in the description of this video and go to the top tech boy lesson 30 and you have my permission to copy these two lines of code because it would be hard to type them in okay and so basically I'm getting a command that I'm going to send to the GPS okay and so what I'm saying is it's a string variable called update 10 microseconds and then it is this long string and what the purpose of this is is this is a command that I'm going to send the GPS later and what this is saying is it's telling the GPS to just report every 10 seconds because I think in the default condition it's reporting really fast and we have enough problems making this work that it helps a little bit if we slow the GPS down like you really don't need data more than every 10 seconds on your location so so we're gonna slow it down a little bit and then the other thing about the GPS and its default configuration it's sending lots of different types of nemeth strings and one has one set of data another has another set of data and you're getting all of these multiple flavors of nema sentences in your data stream so our life will be easier if we just sort of get the one critical nema sentence which is the GP RMC and so when i send this command to the GPS in a moment it will tell the GPS stop the nonsense just send me the one the one nema sentence that I actually need and most of the data is in this particular Nima sentence so these are two commands that we're going to use later on okay now we are speaking to the GPS / software serial so I need to do soft wear serial and I'm going to create a object called Jeep yes sirree all okay GPS serial and I'm telling it to go to pin two and pin three okay that's where I'm gonna hook the GPS to and for you folks playing along at home I will tell you that I have TX I have TX of the GPS TX on the GPS board hooked to pin 2 and rx on the GPS board hooked to pin 3 okay and you understand how this works is TX hooks to Rx and rx hooks to TX and so what I just told you will make things work properly if you do the GPS serial 2 3 ok so now I have my object created software serial G GPS serial 2 & 3 ok so that's the stuff that I need to do at the very top and now I'm going to come down to my void setup and let's see what we need to do here well we're gonna be doing blinking the LEDs so we better do a pin mode right off the bat we're gonna be doing red LED okay and that's gonna be an output because we're gonna want to blink it okay then I'm gonna do GPS Siri all dot begin and that's 9600 okay now you can run the GPS at different baud rates but when you power it up on the Adafruit GPS it is running at 9600 so you are gonna have to talk to it at 9600 now if we wanted to after we did this serial begin we could change its baud rate and then we would have to change the change the rate at which we're talking about it so it's really easier just to use the baud rate that it's its default rate so we're just going to use GPS serial dot begin 9600 we're also going to want to give it a second so once you start it it's good to just give it us a little bit that so I'm going to put a delay in here anytime I'm talking to something like this a sensor like this the Arduino so fast I want to make sure that it heard the command and had time to think about it before I force another command down its throat so I'm gonna delay and now I am going to give it this command okay so it's gonna be GPS serial dot print so I'm sent this print is sending a string command to the GPS well what do I want I want this one here so I'll just copy that GP RMC only and that is set to this and so therefore I can just paste that and now that is sending the command to the GPS what am I saying just send me the one flavor of nema sentence okay I want to give it time to absorb that command as well so I'll give a delay of 100 okay and now I am ready to start playing with my interrupts and so I'm going to start my interrupts so I'm gonna say timer 1 dot attach interrupt capital I and attach inter up and then I'm sorry I need to initialize it in AI Li ze initialize and I'm gonna go a thousand microseconds ok a thousand microseconds the units on timer 1 initialize or microseconds so this is going out every milliseconds now if you think if the GPS is sending data at a 9600 baud if I operate at a thousand microseconds or one millisecond on the read that should go out and go get every character so while I'm doing things up here blinking in the void loop it's going to be interrupting once a millisecond to go get that next to go get that next character okay now what do we do the that's setting up an alarm clock it's gonna go off every millisecond or a thousand microseconds what do I want to do when the alarm clock goes off I want to attach interrupts and attach interrupts okay and what do I want to do I want to read the GPS okay I'm gonna read the GPS okay that is my avoid setup so I guess the first thing I better do is I better go down and say what are we gonna do in our function read GPS so I'm gonna need to create that and so that comes after the void loop and after the void loop is closed okay you got to make sure not to put this in the void loop it's got to be after the void loop close it so I'm going to say void read GPS okay and then open close on that and then open curly braket what am I gonna do okay well first of all no now understand did I send that other command yeah so I told up here I told the GP uh did I send the other command no I did not send the other command what am I thinking okay I said I skipped something up here okay so I sent it the command of only sending me one sentence one type of sentence one type of nema sentence now I need to do another GPS serial dot print and this time I want to tell it only send me a data point every 10 seconds send me a sentence every 10 seconds so I will come down and I will say update every 10 seconds okay so now I'm gonna just get one type of sentence and I'm gonna get it every 10 seconds believe me this is a hard enough problem don't be a hero you know just do this to make it work because this is this is actually pretty pretty complicated to get this set to work as it is so I'm gonna get one sentence every ten seconds okay now how often am I going to see if how often am I going to run out to see if the GPS has anything for me well we already talked about that I'm gonna do that every millisecond so what you have to understand is you're going to be coming down here to the read GPS thousands of times when there's nothing there why because when there is something there you don't want to miss it you've got to think there's not going to be anything happen for ten seconds but then when it happens even Bam Bam Bam Bam Bam Bam Bam you got to read it fast so if I said oh just check it every little bit the problem is once it starts filling up that buffer it's gonna fill it up and overflow it so I've got to be checking it every millisecond and then if there's something there I will read it in and if there's not something there I will want to do nothing but I've got to check it very frequently so most of the time when I come down here and check it you know what there is not gonna be anything there so I can't just come down here I can't just come down here and read it right I've got to say I've come down here when the alarm clock goes off but now I gotta see if there's anything there if GPS serial dot available if that's greater than zero okay if that's greater than zero that means there's something there okay if there if if it's greater than zero that means that there's something there if not what's going to happen if there's nothing there it's going to jump over this if statement it's going to go back up to the void loop so I don't want to just come down here and read I want to come down here I want to say is there any data available if there is I'm gonna read it so how would I read it I would say see I can only read a char so I remember see as a char is equal to GPS dot serial no GPS serial I'm sorry GPS serial dot read okay now I have read the character okay now why did I put two equals there were you yelling at me about that I hope so okay no I can just off that software serial I can just go grab one character at a time how do I build the Nemeth sentence I have to concatenate it onto this string so every character I've got to grab a character put it on the screen get grab a character put it on the string to build my nameís in it so here what I'm going to do is after I read the character then I'm gonna say NEMA the string can cat see okay let me explain that takes the existing NEMA and it adds to the end of its C so now the new NEMA is the old NEMA plus C does that make sense I hope so okay so I've now put that on there all right now that ends the if statement okay that ends the if statement now if I just did this it's going to just keep adding more and more characters until I end up with the NEMA sentence that is 12 miles long so what I have to understand is that the GPS sends a string of characters but when it's at the end of the NEMA sentence it sends a backslash R and that's like the period on the sentence so I've got to be looking for that back that backslash R because that will tell me you got the whole enchilada you got the whole NEMA sentence stop don't keep reading you're done okay so what I need to do then is after this if statement I need to see did I just get to the end of the of the line so what I'll do is I'll say if see if that last char equal equal and then one quote backslash R okay what do I do I've got to close that okay what am i doing close it that's got it okay so another if statement if see that last chart is the backslash R that means I now have the whole sentence so what do I want to do I want to set a flag remember that flag flag equal 1 now what does that do that tells my main void loop hey buddy you got a whole NEMA sentence do something with it run with it ok because otherwise it's going to just keep building building building building ok does that make sense I hope so so what are we gonna do every one milliseconds we're gonna jump down into read GPS the interrupts gonna go off we're gonna jump down to read GPS it's gonna say hey is there any data there if there is I'm going to read it if there is and I read it do I have a whole sentence yet if no I'm gonna go back and then I'm going to come back and get the next character notice that each time in here I only get one character Y in an interrupt you want to get in and out as quickly as you can so I'm doing the minimum and then I'm just calling it over and over building the nema sin it's and then when it's done I'll say flag is 1 you've got a nema sentence I hope that makes sense all right so now let's go back up to the void loop so what do we want to do here well let's uh let's let's go ahead and build the part that deals with the NEMA's nema sentence so so I don't want to do anything with that nema sentence because it's going to be put together one character at a time I don't want to jump in in the middle of it I only want to jump in and look at Nemeth sentence if I have a whole sentence how do I know if I have a whole sentence flag equal one so what do I do here if flag equal equal one you know what the most common right I teach engineering in high school I teach Arduino in high school you know the number one mistake that I have that it's just a head banging pull your hair out try to debug is you get sloppy and when you do your conditionals you use one equal instead of two and then it's really hard to find and so always always look at your conditionals that's a lot of times where your problem is okay so if flag equal one what am I gonna do alright now this is where you got to be a little tricky because remember I'm out there banging that interrupts every millisecond I'm out there banging that interrupts every millisecond and so I've got to be cognizant in my main loop not to be doing something that would mess up with that interrupt and so you know what I'm going to do because I'm going to print my nameís sentence out here I'm gonna stop the interrupts so that it does not interrupt this little piece of code how do I do that timer 1 dot stop I'll give you a heads up this only works if you got that latest interrupt that original interrupt from lesson 28 it doesn't have this feature ok so I'm going to stop the net interrupt now also when I got that name a sentence remember how it had that /r on the end of it I just want a good old-fashioned string so I want to take that nonsense off so I'm gonna say nema dot trim okay and that'll clean any white spaces any backslash n backslash arse off of it so you just end up with a with a cleaner string ok then I'm gonna say serial dot print L in I'm going to print my nema sentence ok and then that makes sense I'm going to my name is sentence alright now you got to think I've got my NEMA sentence and now I'm gonna turn I'll go ahead and do that now okay timer one dot restart now that I've got the slow printing done and I got all this stuff done it's time to make sure that I'm interrupting again okay got that interrupt going again so I started it so I got my NEMA sentence I printed it now what's the problem I'm going to come out of this and I'm gonna start going back looking for the next NEMA sentence but what do I already have I already have a long string what would it do it would start putting the new one on the end of that string you know on the end of the old one what do I need to do here I need to reset NEMA to an empty string that way I'm starting with fresh sentence it's like washing your plate it's like emptying the trash okay and what else do I need to do remember flag was set to one and yeah flag is set to one down there well once I've gotten that NEMA sentence what did I better do with flag I better set flag equal zero okay I better set flag equal zero and so you've got to kind of do your bookkeeping so so that it'll work fresh the next time like it did the first time I've got to empty the NEMA sentence and I've got to set the flag to zero and now what will it do it will go out and it will start hitting that GPS read again trying to build the next NEMA sentence well while that's going on I can do other things so you would really be doing things like calculation of location or creating a KMZ file you would be doing really big important things in your void loop but just to kind of show you the concept I will do something simple which we will do a slow blink okay we will do a slow blink on the led now why are we doing a slow blink on the led that takes a long time and that demonstrates that while your slow blinking the LED in the background with the timer interrupts it is hitting that GPS read and it is building that nema sentence while you are blinking the LED so let's do a did this is in the void loop let's do a little D digit all right red LED red L red LED comma hi okay deal a 1000 okay and then did digit all right red LED low and then I do you lay 1000 okay so you can see if you didn't have the interrupt this would just work horribly because while you are delaying as often is not in the middle of that delay the GPS is gonna cram aneema sentence down your serial port and then it's gonna overflow the serial port because you're sitting here twiddling your thumbs while the data is coming in okay but with the software interrupt during this delay it's out there building that next nema sentence okay now I'm just curious how many mistakes that I have made that you have found that you were screaming at me over but will likely have to do some real-time troubleshooting be patient be kind okay so let's see what happens if I try to download this Hey whoa BAM okay so it actually looks like it looks like it it looks like it downloaded so let's open up the serial port and see what happens if I can find the serial port okay me okay here it is serial port I think so hmm okay I'm not getting anything here let me close it okay we'll open it again [Music] mmm-hmm 115,000 - oh you know what I don't think I did Oh in my void setup I did not start the serial port the old-fashioned serial monitor okay we will do a serie did you all see that serial dup again how come somebody didn't tell me start your serial port okay and 115200 guys man we have enough timing problems as it is so if you're gonna press something print as fast as you can right so 115200 will work on most USB cable so let's download this again okay let's see ah alright oh look at that okay let me explain what that is I think these first two commands that I got back that was probably a response that when I did the the GPS serial print it probably sent me a response back yeah I got it and then when I sent this one it sent a response back then when I went down and started reading that was like old data that was in the that was in serial buffer okay look at this okay look at this I am getting NEMA sentences every 10 seconds now I am inside and so I don't actually have a fix and so the NEMA sentences are empty okay the NEMA sentences are empty because I am inside but this would work exactly the same way when you get a fix then between the commas you're getting things like latitude longitude elevation downrange velocity useful things elevation how high are you so you would be getting lots of interesting things alright then what else would you be doing well instead of sitting here just blinking an Ellie d you would be working with that data and doing calculations and maybe driving a display or creating a KMZ file or doing something else or sending the result to Google or if there's all types of useful things that you could be doing when I'm just sitting here blinking the LED but to show you that this works look at that boom I've got the LED blinking and as the LED is leisurely blinking I am building aneema sentence okay I am building aneema sentence so man I hope this makes sense and I hope what you see is is that this is kind of where you hit a brick wall with the Arduino and that is you know you get this really slick little thing and in ten minutes you've downloaded the Adafruit GPS library and and you just use their library and you put it on there and it's just spitting out lat/long lat/long on your screen and it's great you think oh well this is wonderful but then when you go and start trying to really do something with it you find out that you have got to have a technique to be grabbing the data coming from the sensor and then doing something else and just the standard library that comes with it doesn't do that and you find this with a lot of these sensors a lot of these sensors are not client-server they're just viewing vomitting data at you and it's all you can do to just catch it but the problem is it's the goal is not just to catch it it's to do a useful function to do a useful function you've got to do the type of thing that I've that I've shown you here okay man this is really some advanced techniques here and I'll just end kind of with a caution you've got to really really be careful when you're using these interrupts because there's a lot of ways that you can mess up like I'm imagine up here imagine up here on my how often I run this interrupt I've got to make sure like you think it only spits data at me every 10 seconds so I shouldn't be checking it every millisecond yeah but the problem is when it starts spitting data the amount of data that it spits out is so great that it will overflow your buffer and when you come around every 10 seconds to read it you're gonna have old or corrupt data and so you've got to make sure that you don't miss that first character therefore you've got to be hitting it kind of hard like every milliseconds okay and so you've got to sort of really think about how you set these timers up and it's not for the faint of heart and it's not for let's see I was on the wrong screen sorry okay it's not for the faint of heart you know on setting on setting up your timing you've got to make sure that you're going fast enough that you're not going to you're not going to end up missing it when it comes in okay palma quarter from top tech boy comm really look forward to hearing your guys's comments below think about giving us a thumbs up thinking about running over and subscribing to the channel think about sharing this with other people palma quarter top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 51,434
Rating: 4.958724 out of 5
Keywords: Interrupts, Arduiino, Timers
Id: yyaRaqViu-c
Channel Id: undefined
Length: 39min 53sec (2393 seconds)
Published: Tue Mar 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.