Arduino Tutorial 11: Understanding the Arduino Serial Port and Print Commands

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 11 and our marvelous series of new tutorials on the Arduino microcontroller what I'm going to need you to do is pour yourself a nice big mug of iced coffee ah that is delicious no sugar no cream none needed it is very refreshing and delicious on a day like today what I also need you to get is your a Lego super starter kit for the Uno r3 if you don't have this already look in the link down below click on it get it 35 bucks all the lessons in the series are going to be using components from this and so this will keep us busy for a long long time ok what we are going to do today is we are going to learn how to print from the arduino now we've done some pretty cool things already we can control the pins on the led we can send values to them we can read values from them but sometimes we need to have the arduino to send us messages or to send us information so we need to know how to get information that we can read out of the arduino and we do that with the print commands and so that is what we will be doing today you got a little preview of this in lesson number 10 where we were reading some voltages from the arduino and we needed to print them out so we got a little sneak preview there but we're going to kind of dive a lot deeper into that topic today and so what i need to do is i need to move over to a window that you will be able to see and then we need to jump right in you can see that I have my arduino aidid e up and running and so let's jump in and let's learn all about printing so first of all i need to declare some variables i am going to declare our variable j and so it is just a counting variable so i'm going to call it an int for an integer I just need an integer I'm going to call it J and I'll start with equal to one so I declare the variable J it's an integer and I set it to one now if we are going to print in the void setup you have to set up the serial monitor you only need to do it once so where do you do it you do it in the void setup and you go capital S serial turns that happy little orange color serial dot begin and then there's only one parameter and that is the baud rate we'll talk a little bit more about pod rate but we'll set it at a bawd rate of 9600 in our command with a colon and then what we're going to do down here to get started we're going to say the serial dot print what are we going to print we're going to print J in this case it would use just print 1 1 1 1 1 that's not very interesting so we'll say J is equal to J plus 1 so it's going to print J and then it's going to increment J by 1 and then if we just ran this it just means it would go too fast so we need to slow it down a little bit so I'm going to say delay let's say 3/4 of a second 750 milliseconds right no no you don't use numbers down here that's bad programming you set up a variable we're gonna come up here and say int what is our int D let Lai or let's call it wait T okay and let's set that to 750 and now we will use the variable weight T anytime we want to have a pause and that is better than using constants down in the program so we're going to print J we're gonna increment J and then we are going to wait now I'm going to download the program we got the happy little green bar and now we need to look at the serial monitor so I need to hit this button to pop up the serial monitor and then I need to try to put it over here where you can see it a little better and it's not covering other things okay and I need to get smaller here to get out of your way all right so it is not working right the program ran but nothing is printing out what could possibly be wrong hmm let's see am i out of your way enough I think I'll be out of your way right we said operate the serial monitor at what baud rate 9600 if you look down here okay if you look down here at our serial monitor what is that set to that is set to allow a hundred and fifteen thousand two hundred guys it doesn't matter what baud rate you use but the baud rate that you use here in the Arduino program has to be the same as the baud rate that the serial monitor is set to so since we said 9600 let's set this to 9600 let's download it again it's happy and let's see oh look at that one two three four five six seven so it is counting and it is printing what is the problem now the problem is it's going across the page and as you see that now it's really hard to see that would be called very poor formatting and I need to scoot that just a little bit okay that's very poor formatting the problem is if you just use a print command it just prints it out and it will just go across the page forever so what we need to do is we need to tell it to go to a noon line with that instead of saying print here we say print L in in that sort of that says print and then go to a new line so let's download that and see what happens boom one two three all nice down the page and so that is very nicely formatted and going through so we've made a little bit of a counter here and we have achieved our first little success in printing okay but let's see if we can do something more interesting I am going to make a new variable hmm and I am going to call it a string okay it is a round number an integer like minus 1 minus 2 0 1 2 numbers like that a string is a string of characters and so I'm gonna have my string this is the name of the variable and I'm going to set it equal to and I put in quotes J equal space so J space equal space and then put a colon so now I have a new variable the new variable is called my string what type of variable is it it is a string and then what value is put in it well to put a value in it you have to put the quotes around the value and then it puts what's in those quotes in your bucket in your bucket is named my string so now let's do this what does that do absolutely nothing quite because I set the string up I set the variable up but I didn't use it so we're going to come down here and before this print statement we're gonna put another print statement and that is going to be cereal dot PR int L in and this time what am I going to print my string and then I'm going to end it with a semicolon and let's download that and see what happens ok J you see it says J equal 1 J equal to J equal 3 on and on do you like that no that looks kind of goofy so how would be a better way to do that instead of saying cereal dot print line of my string we go back and say print and so it will print my string which is J equals and then on that same line it will print what the J is in it will go to the new line J equal one J equal to boom exactly what we wanted doesn't that look good okay so you've learned two commands you've learned serial dot print you've learned serial dot print line you've learned that you have to turn the serial monitor on before you use it hey what if we did 115200 115 200 that's fine as long as we come over here and set it to 115 200 so that would be a much faster baud rate you would be pushing the data much faster and we're not going to notice anything because we got that long delay in there but you know you can use higher baud rates to make it run faster J equal 1 2 3 4 5 6 7 very good most excellent let's play around a little more we'll make a few more variables let's say that I have an int and I have X and X is going to be equal to 3 and I'm going to have another int and we're going to call it Y and Y is equal to 7 and then we are going to have another int and it's going to be Z and I'm just going to declare Z I'm not going to put a value in it yet all right so X is 3 y is 7 and then Z is just waiting for us to tell it something to do let me get let me get this out of here I'm just kind of giving you giving you a different example now so let's say that I say Z is equal to X plus y and now I can serial dot print L in I can print Z like that and let's look at that this is going to be kind of boring but then we'll make it more interesting so what's it going to print X is 3 y is 7 and C is X plus wives so it is going to print 10 over and over and over because Z is 10 well the problem is we have no idea what this what is this is a 10 business that it's telling us and so let's make a better print statement something more more formatted and so let's say serial dot print and then I'm going to print X and then I'm gonna stay on the same one so I'm gonna build this print across the line so I'm gonna print it X and then serial dot print now I could declare a new string up here and then I could just print the string like I could call like I could call my string I could call it space I could call it space plus space yeah let's do that so I'm gonna print my string and then I'm going to serial dot print I'm going to print Y and then I'm going to serial dot print and then I'm gonna print start quote space equal space alright so you see two different ways that I am printing a bit or finish that before I forget it okay do you see two different ways that we're printing a string in one case we're declaring a variable which is a string variable and we're putting in it what's inside the quotes space plus space another way we can print is just make a string directly without using a variable and that's done by whatever you put in quotes and so I want you to think what is this going to print what's it gonna be like all right so let's see let's download it okay three plus seven equal 10 so you notice when I said serial dot print X it did not put the character X it put the value that's inside of it which is three then when I said serial dot print my string it didn't write the word my string it wrote what was inside my string which is space plus space space plus space do you see that that's kind of messed up space plus space okay and then when I printed why it didn't print the character why it printed what was inside of Y well then when I printed this it printed that exactly because that is the string inside of quotes so what you got to see is you got to start understanding the difference between the name of the variable versus the value that's inside the variable and so here you say 3 plus 7 is 10 that's true that is true but if I come in and I put quotes around the X and if I put quotes around the Y what do you think is going to happen now okay now it's saying X plus y equal 10 which is true but when I put the quotes around the exit prints the character X or the string X when I just say X just print X it will print the value that's inside of X all right so let's try this what if I say 12 and 11 let's see if this still works 12 and 11 X plus y is 23 12 and 11 is 23 so that's really working I hope you're seeing how this works let's play around a little more because you're kind of beginning to get a little bit familiar and comfortable with variables so what type of variables have we used here intz ants are integers those are like - 3 - 2 - 1 0 1 2 3 it's the round numbers we also have floating-point numbers so what if I was going to say pi is equal to 3.14 could that be an int know because 3.14 is not a round number it's one of the in-between numbers so I need to say float pi is equal to 3 point 1 4 e alright what if I then say I'm going to also have a float anima say R is equal to two the radius is equal to two and then float area and I'm not going to put a value in area I'm just going to declare it as a float and then we are going to come down here and we're going to calculate areas so I'm going to take a lot of this stuff out and I am going to in my void loop say area is equal to PI which I defined times R squared and the easiest thing for this case is just times R times R and then cereal dot print L in and then what am I gonna print I'm gonna print area area okay and then let's see what happens there we should get a float out of this thing right oh my goodness what did I forget here okay why is this unhappy why the ugly little orange yelling at me complaining it says it expected a comma or a semicolon before float well here's float what did I forget to do on the line before semicolon let's try again oh why does it not what R is equal to oh I did it on the PI's well hopefully you guys caught it hopefully you were yelling at me through the screen and throwing things at me let's hope it works at something okay it almost worked ah man I have just completely blown it on my semicolons haven't I but really what I'm doing I'm letting you see what the error is it is saying expected a semi condolence : before cereal so you look at cereal it expected it before it and so sometimes the error is on the line behind the one that is being highlighted so let's try now oh it's gonna be happy it's gonna be happy and what is it printing uh seems to be pro twelve point five six so the radius is twelve point five six is this nicely formatted or poorly formatted it is poorly formatted so let's do a better job format and so what we are going to do is we're again going to start printing across the page and so we are going to say serial dot print and we're going to say in quotes a circle with radius space so that is a string it's going to print the string a circle with radius and then I'm going to serial dot print and then what is the radius well it's our it's the variable R so no quotes and then serial dot print has and I should not yeah has an error e of okay and then what we will do is then print the area and just to be proper then what would be good I'm gonna do it Syrio Forel in and then I'm gonna put a period on the end and that would be a string okay so now what is the print being that's no good print going now what ah okay print Ellen what's the problem this is going to send it to a new line and I don't want it to only want it to go to a new line after I printed my whole sentence and so let's download that oh I need to make this bigger huh okay let's make this bigger a circle with radius 2 has an area of 12.5 6 what is not good the two I can't put a space after the two because that's just a number I need to put the space before the H and after the F before the H and after the F you guys would on grading students work in looking at things nothing makes me more upset well I guess there are things that make me more upset but one of the things that really makes me upset is if you don't format nicely so now you see a circle has a radius a circle with radius two has an area of 12.5 six I think that is pretty darn good let's uh let's make it a little interesting let's say now after we do this we say our R is equal to R plus 0.5 because who wants to just sit and read all day what the right what the area of a radius of circle two is so let's do this forgot it man I'm doing terrible with semi-colons day all right so now let's call this up open it up a circle with radius two has an area of twelve point five six a circle with radius two point five as an area of nineteen point six two do you see something neat that's kind of happening here you could actually start generating like tables of values where you're stepping through different radiuses in your calculating what the different areas are you could print this out and have kind of a lookup table where a person could look up the radius that they're interested in and then could see what the area of that circle is I think that that is pretty darn neat okay I think that that's pretty good for today's lesson we've learned that you can define string variables and then you can use those down here actually also let me just say let me do it one other way let me take this and let me cut it and then up here let me say string I'll say this one for message 1 is equal to that not going to forget my semicolon this time okay then I come down here and a circle with radius here then we put ms 1:no quotes because i want that this is a variable and I want the valuable that is contained within that variable and then similar here we will put a cut and then up here we will say where was that that is so hard to see okay there it is string message to is equal to it has an area of and then down here I would print mess to and then in fact even our humble little period we will call that mess three so we will define a new variable a string mess 3 equals control V and don't forget the semicolon and then I will come here and I will say yes three is this making sense how we're using variables to our advantage okay look at that a circle with radius 2 has an area of 12 5 6 and so boom that is working okay guys I hope that you've gotten a little bit better feel for the different types of variables you have ents you have floats and you have strings or the ones that we're working with so far and then you can print or you can print line and then what you also have learned is you've learned that the baud rate the baud rate on your serial monitor has to be the same as the baud rate that you set in the program and I need to tell you one more very very important thing you see here you have the option of selecting no line ending newline carriage return or newline and carriage return that is when you print something that is the invisible characters that it puts on the end of the line and you want to leave that on no line ending because if you're putting invisible character on the end of your prints it can cause unanticipated problems in programs that you write and so just make sure one of the biggest things I see with students that have a program that doesn't work a lot of times it's this is somehow gotten set to something else and you don't want to do that okay guys this is Palmer quarter from top tech boy comm I am hoping that you guys will think about giving me a thumbs up think about subscribing to the channel also I'm getting a lot of emails from people saying that they're not getting notifications of my new videos on the subscribe button make sure that you touch the bill make sure you ring the bill hit the bill and then you will get notifications when new videos come out Palmer quarter from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 151,508
Rating: 4.977356 out of 5
Keywords: Arduino, STEM, Serial Monitor, Print
Id: b5kndEtAKl8
Channel Id: undefined
Length: 25min 7sec (1507 seconds)
Published: Fri Jul 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.