LESSON 12: Simple and Easy Way to Read Strings Floats and Ints over Arduino Serial Port

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello goddess this is Palma quarter from top tech boy calm and we are here today on Arduino lesson number 12 and what we're going to focus on today is we're going to look and see how to easily and simply read data off of the serial port on the Arduino and this is something that we really went over a little bit in Lesson number six but it's such an important topic and something that we're going to really be need to be able to do for the remainder of the lessons it's it's something that you really need to do for all types of different projects and also the reason I want to go over it is that most of the tutorials that I see on the web make this unduly complicated there's a lot of very tedious ways of doing it there's a lot of very difficult ways of doing it but what I want to do is I want to show you the easy way okay I just want to show you the easy way that will simply work and you're welcome to go in and learn the more complicated or the more in-depth ways but I just want something that will work okay for almost every project that you're going to do you can do the project with base by utilizing basically three different data types there's a lot more data types than the three but almost anything that I want to do on the Arduino I can do if I use one or all of these three data types okay there are other data types out there but I want to kind of have the minimum set that will meet almost all of the requirements that I'm going to run into the three data types that will get you through almost all of your projects are int for integer float for floating-point number and remember in the world of programming we call them floating-point numbers in math class you would call them real numbers it's all those in-between numbers that are between the integers and the third thing you need is a string and basically a string is a string of text and so if you can have a variable that's a string for a string of text an int for your round integer numbers and a float for the in between numbers you can do almost everything now it's true that if you get more sophisticated you maybe could do things a little more efficiently or you can do things more elegantly but if you learn those three data types and how to get them across the serial port you'll be able to do just about everything okay so if you're going to have a project one of the first thing that you've got to do is you've got to kind of identify all of those variables that you're going to need you need to identify all the variables that you're going to be using and not just kind of like the variable name remember a variable it's like a bucket in the name of the variable is the label on the bucket you need to kind of figure out the bucket in the label that you're going to put on the bucket the name of the variable and then you need to figure out whether that variable should be an int a float or a string okay now it's true that you could probably just make everything either a float or a string all right that you know a float could probably do just about anything that an int could but it is good to use ents when you can get by with them integers because they take so much less memory than a float you can make your programs more efficient if you only use floats when you really need them and use its when you can get out when you can get by with it so for instance if I'm going to declare a pin and I'm going to be using pin 9 there's no reason to make pin 9 you know a pin number to be a float because your pins are always going to be round numbers and so it's just natural to declare your variables that you're going to use associated with pins it's just natural to declare those ends let's think of other things that might be yes if you said I made a program to average grades and you said how many grades do you want to input well the number of grades that you would have would always be round numbers I have 5 grades or I have 14 grades so it would make sense to make that number and but now I'm inputting the grades the grades themselves you might have in between grades maybe someone made a 97 and a half so in that case the variables the grade itself should probably be a float so you see you need to look through here and kind of decide on your project what your variable names are going to be in whether they should be against so votes or strings okay if you think about a program where you're going to input a person's grades and average them you might want the person's name as a starting point well the person's name would logically be a string and so you've got to kind of scope things out with what your variables are when you start when you start a project so what I want to do in in just a little review how to read int floats in strings over the serial port and so let's just do a little project let's say that we're going to have a project where we're going to in we're going to prompt the user for his name we're going to read the name in and then we're going to ask the person how old he is and we're going to read in the age and then we're going to ask the person how tall he is and we're going to read that in so let's see if we can just write a little program that will do that okay so let's start what do we what did we say that we always want to kind of start with we want to start with our variables and declaring our variables and so let's come up here and think of the variables that we are going to be needing for this out for this project well what was the first thing that we were going to prompt the person for for their name well what type of variable would that be we'll the name would obviously be a string and so we want to declare that a string STR I and G remember it starts with a B with an uppercase s and we're going to declare it a string now as far as what the name of the variable is that's just the label on the bucket and so you can call it whatever you want I'm going to call it my name okay we're not initializing this to any specific value we're just declaring it as a variable so we don't have to put it equal to anything just telling Arduino hey this is a variable we're going to be using so we're going to declare D Claire a string variable thing to hold name alright what were we going to ask for next how old you are so when you ask a person for their age how do they usually answer all that usually say I'm seventeen around 15 or I'm 56 those are usually answered in round numbers and so what cut the type of variable could we use that's right you would call it an int so we're going to call we're going to declare it an int and that's going to be a lowercase okay that's going to be a lowercase and I think I'll just call it something simple age okay declare a int variable to hold hold your page okay float I guess I shouldn't give it away yet the third thing we're going to do was height in feet well think about that would you want to say I'm four feet around five feet well most people are sort of in-between and so you probably would want to give that as a decimal number so we would probably want to make that a float so float for height API hgq high ph t''i okay declare float variable to hold your height okay I think those are the variables we need so we come down here to our void set up now in this case we're not doing anything with a circuit we're not sending anything out to the pins or reading anything to them so we don't have to do our pin modes but we do have to turn our serial port on so a little bit with serial dot begin 9600 that is what we are doing is turning on serial port at 9600 baud you can use whatever baud rate you want but it needs to match how you have your serial port down in the law your serial monitor down in the lower right those two have to match I think that's all we need to do on the serial on the serial port I mean on the void soda alright so what do we want to do on the on the program we want to prompt the user to begin with for his name well to read his name over the serial port to read anything over the serial port there are three things you need to do the first is you've got a prompt the user for the data so you have to ask him for the data so on the serial monitor a little message comes up letting the net user know that you're waiting for input so we've got a prompt M so we're going to say serial dot dot print ill in print line and then what is the prompt well we're just going to type it in as a string because it's a string it needs to go in quotes and we're going to say please enter your name okay and then this is and remember if the comments come after the slash slash the Arduino doesn't read those it's just a person looks at your code or you come back later it'll make sense what you're doing so we're going to prompt the user for his name okay and then so we prompt the user for input then what do we do we got to wait all right because the computer can go a lot faster than a person can so if you say please input something you can't say please let's put something read something you got to say please input something and wait for him to put it in okay so how did we do that remember the way we do that is we create a empty while loop it just hangs at that while loop waiting for data when it sees that something's been put on the serial port then it'll go read it but we've got to put this this kind of empty while loop in to just wait so we say while serial dot available equal equal zero what's that saying while there's nothing at the serial port that means while no one has entered anything as long as no one's entered anything hang up at this point okay so basically that starts the while loop to open curly braket that in the while loop closed curly bracket and what do we do in the while loop absolutely nothing we just sit there and we go around and around and around in this while loop waiting for serial dot available to not be zero when the person enters the number and hits indoor hits enter then all of a sudden serial dot available will be equal to one and it'll drop out of this loop but until then it's going to sit here and wait so we prompt the user for input okay then we read it in I mean we prompt the user for input then we wait for it and then as soon as it's there we read it in okay now the first thing that we are going to do is we are going to read the string okay so we've said that you can do almost everything with a string a float run int so the first thing we need to review here is how do you read a string over the serial port well first of all you have to put it inside of a variable which variable were we going to put the person's name and we're going to put it in my name and my name is going to be equal to what well we're going to do a serial dot we're going to read the name over the serial port what's the command for reading the name let's serial dot read string and you got to do it just like this serial uppercase s dot read string uppercase s okay and you see when you do it right it turns orange okay opening closed parenthesis and then close that off alright my name is equal to serial dot read string so what this is saying is tell the person to input their name wait for them to input it and then when they input it read it how do we read a string we read a string with serial dot read string and that will bring the whole string in the whole string comes into the variable my name so this is the easy way to read a string over the Arduino serial port okay and let's continue to be good boys and girls and put our comments in so what are we doing here we are waiting for user waiting for user input what are we doing here read piece the screen baby string okay okay so now we have done the first thing we have asked them for their name and we've read it in okay I don't want to type this whole thing in every time because the series is going to be the same some food I didn't mean to do that and paste it back in and come in to copy it save us a little tapping here and we'll just edit it next time around so this time we're now want there H and so what should we prompt them for okay we should say please enter your H or how about if I say let me make it a little nicer how old are you okay and now we're prompting the user for his age okay now what do we do we'd ask him for his age what do we do we wait so while cereal available is zero while we're waiting for him to put it in there we just circle along here then what we do we read it what's the variable we're going to read we're going to read the variable age okay now how do we read an int over the serial monitor we wouldn't read it with read string and we wouldn't read it with read int it's a different command its serial dot parse int okay see it turns orange because it recognizes it okay so now we have the person's name and now we have the person's age so what's the next thing we want we want their height so again I'm going to paste those three lines disabled it'll work we're going to prompt them for their weight and so what we're going to say is how how much do you weigh so we are prompting the user for his weight then what do we do we wait and then when something has been entered we drop out and we say wait because that's I'm sorry height how much I messed up help tall are you we were doing height not weight how tall are you prompt the user for his height weight for it and then read it into which variable it's a variable that we called height HT okay now we are not going to read string because height is not a string we are not going to parse int because height is not an int then we are going to parse float because it is a floating point number so basically if you look at this code right here these are the commands that you need to do just about anything to get just about anything you're ever going to need to off of the serial monitor if it is a string you use the command serial dot read string if it is an int you use the command serial dot parse int with a capital I uppercase I okay and then if it is a float you use the command serial dot parse float simple as that prompt them for the input wait for it with the while loop the empty while loop and then go out and read it and just keep track of whether you're going to use a read string a parse int or a parsefloat command okay let's practice our printing a little bit so now let's come over here and let's let's sort of print these things out so let's send the person a gradient greeting back and let's say Siri dot print and let's say hello to them aqui ello oh and then I put a space now why did I not put print L in print light because I want to string together several of these print statements so it makes a nice sentence and I don't want it to go to the next line because I want to say hello and then the person's name in order to say hello and the person thing on the same line I need to make sure that I don't advance to the next line so I say serial dot print line hey I mean serial dot print okay and I'm just going to put all my comment here for all the next thing produce nicely formatted output okay now I'll just call that comment good for the rest of the things so now what put is the person's name I'm going to say serial dot rent okay okay and what is their name their name is a string and the variable name is my name okay so my name okay now this is something very important and this is something where students get messed up all the time if you're just going to print out verbatim the word hello it's not a variable it's the string hello and these string hello you tell it that it's a string by putting it in the quotes but if you're going to print a variable my name is a string variable you want to say print line a serial print my name you don't put quotes around it because it's a variable if you put quotes around it it would put print out my name as opposed to the person's name but because we want the the value in the bucket not the label on the bucket we don't put quotes so hello we'll print hello serial dot print my name we'll put thing whatever the content of that bucket is whatever the person told you that the name was no quotes in that case okay now you need to give him a message okay serial dot print B we're going to say comma you are we're going to tell them how old he is notice that I have to put that space after our because if I don't put that space after our it's going to come in and put their age crammed up against the e here you have to put that space and it doesn't know to put that space in now a serial dot P dot print now what is their age their age is H you are H again age is a variable so we don't put the quotes on it we want them to tell us the number we're going to tell me the contents of that bucket H you are age and serial print and then we want to put space so that it doesn't run up against the end of the age space geo print years old period okay that's probably enough for one line so let's get all of that let's go to print l in and now it'll go to the next slide but all on the same line you'll say like hello Jo you are 14 years old that'll all be on one line and then we're going to say serial serial dot print and you are instance that's a specific string and you are spaced the age serial dot print serial dot print and here we are going to put on and you are height so we're going to put HC IG HT the variable serial dot n dot print space 8 tall period okay and then we'll wrap it up serial dot print well n so that this will finish off that line and that did finish off let's see we should finish off the line there with that Ln okay so it'll say hello Joe you are 15 years old if he entered 15 and then on the next slide and you are four and a half feet tall and then what I'm going to do here is I'm just going to print a blank line just quote quote plant a print a blank character and that will print a blank blank line so I get some vertical spacing in there that'll look nice ok so just remember the things that you all want to go on one line you just have cereal print serial.print cereal print and then when you want to finish that line and go to the next line you put serial.println also notice that I've been careful to put my white spaces in and one of the things that really I count off on what I come around and grade your work and other people if you're taking this online other people are not going to like it if you don't if you're not careful about formatting your output where it's easy to read and that make means make sure that you put your white spaces in like if you say on 33 years old you don't want to say I am 33 and then the three and the Y without a space you've got to put that space in there or your formats not going to look good your formatted output it's not going to look good and also sometimes just printing blank blank lines makes it easier to read as well and so nice formatting is actually something that I agreed on I want to see something that it's nice and on nice and easy to read now I've done a lot of typing here is this program going to run is this program going to run let's see everything's green looks happy looks happy alright I think this is going to run ok so let's get this up here please enter your name I am the sturm order okay also guys make sure that you put no line ending here because if you put a new line or a carriage return that can confuse things and so it's just good to put no line ending there some will say mr. McWhirter sind okay how old are you I am 53 okay how tall are you I'm going to say 5.9 feet when you going to make sure I'm in the box they're 5.9 feet let's say sind okay it says hello mister recorder you are 53 years old and you are 5.9 feet tall it says please enter your name okay one thing I don't like is I would have liked to have had a space between here where it's asking me for stuff and when it's giving me the output that would have made it more readable so after the how tall are you I really want to go in I want to put another just print a blank line in there okay so after I read that last thing that last input I'm just going to do a print a blank line here at C rot right dellynne I get a blank line by just poped quote with nothing in it and then close that off I think that will look a little bit nicer I always I kind of do the best I can the first time through and then I come back a second time and just clean things up a little bit so now let's look at this please enter your name I am mister reporter okay how old are you I am 53 how tall are you I am 5.9 feet tall now let's see hello mr. McWhirter you are 53 years old and you're 5.9 feet tall because I said and here that should not have been a period that should have been a comma so let's come here and say comma so like I say you do the best you can and then you look at any sort of tweak things to make it a little better we did not like that let's try it again again I've got this long USB cable so that my thing will reach over here and it think it might have taken at that time let's see and mr. Porter I am 53 years old I am 5.9 feet tall okay hello mr. McWhirter you are 53 years old comma and you are 5.9 feet tall okay and how let's let's say that it was mrs. Smith mrs. Smith okay mrs. Smith is going to be 32 years old how tall is mrs. Smith she's 5.2 feet hello mrs. Smith you were 32 years old and you're a 5.2 feet tall okay very good so what you can see that we've done here is is that we have read the types of data that we need from the serial port we can read int we can read floats and we can read strings and with that we can do just about anything that we would ever want to do okay now what you see is is that I just almost wrote this program for you or I did write this program for you I hope you at least went through and typed it in okay I hope that you typed it in and got it working but you need to be able to do something completely by yourself and so I'm going to start giving you more programming now that you've got to do on your own if you follow along with me on this program you should be able to do the assignment but here's the assignment that's due tomorrow and what that is is that I want you to input a user for their name and then input a input from the user eat their weight in pounds so you sort of say like what is your name you read that in and then how much do you weigh you read that okay now on this one I'm going to recommend that on the way you go ahead and make it a float because maybe the person weighs you know maybe the person weighs 112 and a half pounds or maybe like if they were talking about a baby you know usually you make baby like you know eight and a half pounds since they don't weigh very much you go ahead and give it in in the decimal and the decimal number so input for the person's name then input ask them prompt them for their wake so you're going to ask them what their name is and you're going to ask them how much they weigh you input those two things then what I want you to do is I want you to calculate their weight in ounces so you ask them for their weight in pounds I want you to calculate their weight in ounces and then I want you to calculate their weight in grams and then I want you to calculate their weight and carrots that see AR et like a diamond like a diamond is one carat that's a weight not a carat not a CA RR ot like you eat but a carat like a diamond or a gemstone see AR 80 so calculate the person's weight in carats and then give them their weights back to them and nicely formatted output and so let me show you what it should look like okay please enter your name I am mr. McCourt er okay how much do you weigh in pounds I weigh 190 pounds ok and then it should come back and say hello mr. McWhirter you weigh 190 pounds you weigh 3040 ounces you weigh eighty six thousand one hundred and eighty two point four nine grams and you weigh four hundred and thirty thousand nine hundred and twelve point seven five carats okay and this is what it should look like and then you can put your name and your weight in there but you can test it with my number to make sure that you've done the conversions now I'm not going to tell you the conversion factors you're going to have to go in and do a little research on the internet to figure out how to do those conversions but when you figure out how to do those conversions then you can program them into your Arduino and I will come around for a grade tomorrow and I will be looking for nicely formatted text where you have the right you have white spaces where you need white spaces and you have spaces along the line where you need spaces along the line and that is what I will greet you on tomorrow thank you very much guys this has been Paul McCourt at top tech boy calm and tomorrow we will hopefully see you again for lesson number 13 thanks a lot
Info
Channel: Paul McWhorter
Views: 157,087
Rating: 4.8416424 out of 5
Keywords: Arduino, STEM, High School Science, Tutorial, Lessons, Programming, Engineering, Serial Port, Strings, Serial Communication
Id: VIa_QZWIonQ
Channel Id: undefined
Length: 30min 54sec (1854 seconds)
Published: Wed Jul 09 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.