Sending Multiple Pieces of Data through the Serial Port :: Viewer Question #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to this video tutorial talking about one of the viewer questions so somebody just like you send in a question and we're going to try to address it as best as we can and actually I'm going to pull a little cop out this week because the question is a really good question I'm not going to really address it directly but I am going to address kind of the Arduino side of it so gentleman sent in a basically project he's working on he's got three strain gauges and he's got him hooked up to the analog inputs of his Arduino and he wants to get that analog input from the Arduino over to a program on his computer like some type of computational program that can then kind of crunch those numbers for him and his concern is sending information over the serial port you know he recognizes that you can only send information one byte at a time and his concern is that there's going to be a lag and so really what we're going to talk about is kind of sending multiple pieces of data over the serial port and how you might handle that on the on the computer side when you when you want to get all those pieces at the same time so we kind of address it I mean we'll see we'll find out from him whether or not you know he's satisfied with this solution but so what we're gonna do is we're gonna start off we got this little graphic here we're just going to kind of talk about the serial port alright so when you think of the serial port think of you know like the USB you've got your USB hooked up to your computer right and information one byte at a time can flow from your Arduino down this serial port and right onto the computer and also information can flow from the computer right down back into the Arduino and you know it's really awesome I mean that's really cool you can communicate back and forth so in order to kind of make that communication happen and to make it nice and in sync you've got to do a couple things so and this is like kind of a best practice because you can do it a little sloppier than this but this is one good way to do it so you do what's called a handshake all right so the first thing we do is we we establish an initial contact so basically we're going to have Arduino he's gonna send him he's gonna basically call the computer and he's going to say hey I'm ready man I'm so he sends an initial contact and in this case we're going to send just the character a and we're going to send it via the serial port alright we're going to say hey computer I'm ready are you ready all right so that's initial contact and then what the computer is going to say once it receives that a because the software on the computer side is waiting for the a it's going to say yep it's going to send something back I'm ready man bring it so now what happens so this is where we kind of get into the question hand we've got three sensors out here alright three sensors in case in his case they're strain gauges we're going to just use we're gonna use one button two potentiometers as our example and he's got you know it's collecting all school data out here and he wants to get this information over yonder so how do we do that how do we keep those three samples together and then get them over to the computer so here's one way of doing it so and again yeah I think it bears the quote this question might bear talking about why not you know do the computation here and then send it over well because you're add we know is kind of like a Minnie Mouse you know when it comes to computation so you'd rather have those numbers get crunched on your computer for sure okay so enough about that all right so what we're going to do is we've established our initial contact now what we're going to do is we're going to sample these one at a time so here's sample 1 sample 2 in sample 3 alright and now what we're going to do is we're going to send each one into the serial port one at a time and they're going to go across the serial port and they're going to go into the computer and now on the computer side what we're going to do is we're going to create an array and hopefully you know what an array is if you there's an if you don't know big deal there's plenty of you know I've got other videos on arrays but it would help to understand what an array is to understand this concept here so we're going to on the program side on the on your computer we're going to create an array and we're going to store information in this array one at a time so we're going to say hey here's that first piece of data here's that second piece of data here's that third piece of data and then it's going to say hey once I've received three pieces of data then perform a computation right you know like you know a plus B plus C or whatever or a plus C plus B perform that computation so now we've gotten those three sensor values we send them over this collects them it waits for its array to be full right it's a three element array and then it performs the computation so everybody's happy right well mostly so then what the computer is going to do it's going to clear the serial port so it's going to just get rid of all the stuff that was in that serial port and then it's going to send a little message over there dwee notes can say hey man I just got all your samples thank you I computed them and now the serial ports empty I'm ready for some more bring it and they word we know is going to be waiting it's going to be waiting for that acknowledgement that the come the the program receive the information it's happy and now what it's going to do is it's going to sample again so it's going to one two three take it three samples and it's going to send them back over the serial port and the whole process is you're going to repeat over and over and over again basically we sample this is going to happen really quick you know we're talking on the scale of milliseconds if not microseconds we're sampling we're sending we compute we clear the port we ask for some more the Arduino samples sends compute ask for more sample send compute ask for more all right so let's go ahead and jump into your dueño program so we can take a look at how this is going to get pulled off and you know let me know on the computer side we're going to use a program called processing now it's really not a computational program it's more of a graphic art kind of program it helps you visualize things on your computer you draw programs that make pictures but for the most part the the basis on your dueño side stays the same so you know he might be using octave or MATLAB or something like that to solve this problem the arduino side is pretty much going to be the same regardless of what program runs on your computer to grab information from the c report okay well man that was a long stinkin explanation let's go ahead and jump into the code alright so here we are in Arduino we're going to go up to file examples communication serial call response and you know this is what I love about Arduino there's so many good examples already built in you don't even need to look on the internet it's just like right in the integrated development environment are all these good examples and I mean this example is so good I mean you hardly have to do anything to really probably make it work for this specific project you know we'll just note up top it kind of talks about what this program is going to do and we've talked about that already you know we can thank Greg and Scott for making improvements on this program it talks about the circuit and so what we're going to do is we've got two potentiometers they're attached to a 0 and a 1 and then we've got a push button attached to digital pin 2 this was created by Tom thank you Tom and they modified it back in 2012 Tom and Scott did and I think they did that for the to adjust for the Ilia Nardo because there's some Leonardo specific code in here but just to be clear you don't need an arduino leonardo to do this it'll work on any of the arduino z' okay and then check out this tutorial it's for this specific example on the Arduino website it's really good I would definitely recommend it so let's jump into this first P of code so what's at the top of the sketch the top of the sketch we in declare and initialize variables so we've got our three sensors you know we just talked about hey we're going to we're going to have two potentiometers in one button so we're going to hate let's declare these set them equal to zero and then we've got a variable here and what this variable is going to do is it's going to store that one byte that comes from the computer that's going to be the byte that says hey I'm ready for more information that's what this in byte is going to store and it's an integer okay so those are the variables let's move down into setup so the first thing we do is start serial communications nothing big they're just 9600 baud rate and then this piece of code right here we're not going to talk about it too much it's specific for the Leonardo but again it's just for sitting up serial communications with your computer and down here this we set the mode of a pin so we're sitting digital pin 2 as an input now you might recall actually all the all of the pins on your Arduino are set default as input so you might be wondering well why does he why do they explicitly set this digital pin as an input and not say the analog ones well that's a good question that's just preference that's it that's all I can think of so let's move on here we've got this next line of code it says establish contact and then it's got these parentheses so this is a user-defined function and he's defined it at the end of this program so what it's going to do is it says it sends a byte to establish contact until the receiver responds so remember we said the Arduino is going to send information over the computers can say hey man are you ready for this this again this is the first time it go it goes off and that's why it only has to happen once and that's why it's in setup so let's go ahead let's take a quick look at this user-defined function it's the bottom of the sketch again user find functions are going to be defined after the loop ok so here it is right here here's our user find function and let's take a look at what's inside here so there's a while statement so here's the while statement and so while this condition is met in here this code will get executed so what is the condition well it says serial dot available less than or equal to 0 so the available function from the serial library that's going to do is it is going to return the number of bytes at the serial port so basically if there's nothing at serial port then this this program is going to start sending an a the character a it's going to print that to the serial port and then it's going to delay 300 milliseconds if something is at the serial port then this is not going to happen so again this is first contact so this is the Arduino if the computer side has done nothing the Arduino just starts screaming ay-ay-ay-ay-ay it's waiting for that other program say okay I'm ready and again it's only going to happen once in our program when we run it because once we've established communication then we'll see the handshaking happens just a little differently but for the first time this is going to make that happen okay so again the condition is is anything at the serial port because this is saying less than or equal to zero so hey if nothing is at the serial port then let's we need to establish first contact okay I beat that into the ground so we established first contact we are now done with setup so now we're ready the now we're ready to start sampling data and sending it over to the serial port so here we are at void loop we start with an if statement and it says serial dot available greater than zero so again here's this available function from the serial library it's saying is any information over at the serial port and then it's going to read that value in to invite and this is now this you might be wondering oh hey didn't we just why are we dealing with this incoming byte anymore you know didn't we already just take care of that well the deal is we have to continually check for bytes at the serial port because remember what we're doing is we're waiting for the response from the computer side in our case processing to say hey now I'm ready for the next byte so that's what this is for alright so we're going to store that information in byte so we're going to read the serial port we're going to store it an invite now what are we going to do well now all we do is just some really straightforward stuff we just read the sensor values okay so first sensor is equal to analog read at pin a zero now I want you to just ignore this divided by four for a second and just think of this part this is I mean you've seen this about a hundred times we're reading information from the analog pin pin a zero and we're reading it into first sensor now if you recall analog reads going to return a value between zero and a thousand and twenty four and all this divided by four is doing is it's doing a quick reduction of that value so what we want to do is we want to return a value between 0 and 255 so that's that's how we do it so if you take a thousand 24 so if the max value is a thousand of 24 you divide it by 4 you're going to get 255 and when we look at the processing side that'll make a little more sense and then we do it again here's the second sensor read the value divide it by 4 and you notice there's just a 1 there well throw in an A so that a 1 is going to make for an explicit you know if more explicit explicitly says hey look at analog 1 all right and then we divide it by 4 again for the same reason and then the third sensor and this is just a little peculiar I will say this is a little peculiar but it works great all right now in google krishna's case he's going to have an analog another analog pin here but here we've got a digital pin and so let's just so what we're doing is we're mapping some information so if you you know if you've looked at the previous tutorials you understand the map function but what we're doing is we're taking whatever digital read 2 returns all right so we know digital read if we've got a button hooked up to it it's if if we're pressing the button we're going to get a 1 and if we're not pressing the button we're going to get a 0 all right so the range these first two numbers this range is 0 and 1 and then the mapped output though is going to be 0 and 255 all right so basically what we're doing is we're making this return either 0 or 255 instead of 0 or 1 so I'm not going to spend too much time on this you got to be familiar if you've seen some of previous tutorials that will make sense but what I'm going to do though is I'm going to change this to about 120 120 because you'll see on the other end 120 is a little better so we're going to either send if if we're not pressing the button we're going to send 120 if we are pressing the button we're going to send 255 and basically these values equate to different colors on processing okay and then what do we do well we're going to use the right function we're going to write to the serial port the first sensor value then we're going to write to the serial port the second sensor value then we're going to write to the to report the third sensor value and that's it okay so we've done that we've written all these values to the serial port they're walking over so now what where do they go okay well they're going to go to whatever that program is on the other side in our case processing well let's take a look at the processing code okay and handily they've stuck it right at the bottom of this sketch is that awesome or what so I'm going to do is copy all of this this is processing code this is not our dueño code this is basically Java all right so it's a different language so we're not going to get too in-depth into this you've basically seen all you need to see on the Arduino side but let's go ahead and open processing and we'll paste all this code in here okay so now we're processing side enema again I'm not going to go too in depth on the processing code but let's just kind of walk through some of the highlighting highlighted things right here okay so the first thing is I want to point out look at this they're creating an array right here so this array serial-in array and it holds three elements well those three elements it holds are the values that you just passed over the serial port all right then we've got another one serial count equal to zero this is basically counting this variable is going to count the number of bytes and then when the number of bytes is equal to three it's going to reset and it's going to let the ORD we know know hey I just read my three bytes you can send some more to me okay so here again I'm like I spend too much time here but here is where we're basically setting up the program and the variables or rather we're initializing and declaring some variables kind of getting things set up and then but here's the official set up the only thing you really got to worry about in here is setting up your serial port list and again if you've watched those other processing programs or programs you'll this will kind of make sense so I'm just going to go ahead and set this to five because I know that the fifth serial port works on my computer that's the setup we get into void draw there's really not much here but this exposition in this y-position and then this FG color all of this is going to get set from our sensor data and that happens down here in this serial event okay so this is a function they've created it's a serial event and what happens is when it receives information so it's going to say hey if I receive a so this is that first contact remember that it's going to clear the port so now the port is clear it's going to say hey first contact is now equal to true and what it's going to do is it's going to write a to the serial port and so this is this initial handshake okay so hey did I receive anything yep I receive something okay clear that serial port now set first contact to true so we've established this first contract contact and now I'm going to write a back so I received an A now I'm going to eight write an a back now let's go I just want to jump back to this Arduino code so remember here back here in this loop so here we down here an establish contact we sent the a so processing received the a and then it's sent back another a and when it's sent back that other a now there's something available at the serial port so because serial available is going to return a value greater than zero it's going to return one because a is at the serial port which is one byte and so now all this stuff is going to happen all right okay hope this isn't too crazy first contacts been established we're happy so now if first contact is done so again this only happens once when that was false but now first contract is true first contact is true so now next time we come through this this is not going to happen but this is going to happen this else statement so what do we start doing we start writing that byte that we got into our array one at a time we increment serial count and then if serial count is greater than two so again arrays are zero indexed so if there's three elements now saved into our an array let's go ahead and write this information set actually set these values right here equal to the information that came and over the serial port okay and then it's going to print this information out down here and then it's going to write another a to the serial port and it's going to reset serial count okay so this is going to say hey Arduino I'm ready for some more and then this resets the count okay so I know that was kind of really fast and furious through this processing code I don't want to spend too much time on Java because I'm or you know again this side of it isn't going to help you too much I mean if you're gonna be using octave or some other type of program this is kind of basically you know what needs to happen but it's going to be written in that programs code so let's go ahead and run this code okay so you can see hey there's that little ball there and then when I press my button it turns white so I'm pressing my button and it turns white and now I'm going to turn my my particular my potentiometers look at that the ball goes up and down and the other potentiometer goes left and right and then if I move them at the same time you're really tricky on us here see it can move around the screen and if I'm really tricky I can press with the button at the same time okay see that so what's going on here well the first potentiometer is the up and down alright again this is just a value being sent across the serial port and this this is analog right so up here is zero and you can see down at the bottom of the screen the value that gets that's getting received over the serial port so there's zero down here is 255 okay so up here again this is zero there's 255 and then here look at this value that's zero and when I come all the way over right that's 255 and then the button notice I'm not pressing the button the values I'm gonna move this back in the center here the bat the it's 120 now when I press the button its 255 okay and this 120 and 255 those those are just designating a color so like 255 is y 0 is black and 1 20s like somewhere in between okay so I think you get the kind of idea here so this might be one way to solve your your deal you're going to set you're going to write that information one byte at a time over the serial port again that's going to happen really fast all right we're talking on the order of microseconds it's going to write that data send it over to the computer the computer is going to collect each of those bytes or the pot you know processing basically or octave or whatever program computational program you use it's going to reap those bytes putting and put them in an array and then it's going to display that information you know do its computation or whatever all right so hey I hope that was helpful I don't know if I really hit the dot for you or not but that was it man all right take it easy bye oh you know one more thing if you have a question I would love for you to send it in I'm going to try to do these on a weekly basis so send in your question please make them easier than this one my gosh what do you think I got like freaking all day to do this so no please send me your question I would love to talk to you know try to work with yours too so thanks a ton for watching take it easy bye you you
Info
Channel: Programming Electronics Academy
Views: 68,133
Rating: 4.8322058 out of 5
Keywords: Arduino, Processing (Programming Language), serial communications, serial port, Arduino Course for Absolute Beginners, how to, internet of things, multiple arduino, arduino tutorial, serial communication, how to program arduino, how to use arduino
Id: BnjMIPOn8IQ
Channel Id: undefined
Length: 23min 15sec (1395 seconds)
Published: Wed Feb 19 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.