Arduino Tutorial 18: Reading Numbers from the Serial Monitor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy calm and are you ready to rumble nope just kidding we're not gonna rumble we're not gonna rumble I actually said that to a high school class once big mistake because they rumbled all right so we're not going to Rumble but we are going to do lesson number 18 in our most excellent new and improved Arduino tutorial series and today I need you to pour yourself a nice big mug of iced coffee that is black strong coffee over ice no sugar none needed and I need you to get out your most excellent a lego super starter kit for the you know our three projects what you don't have one look down in the description click on the link order 135 bucks hook a brother up all of our lessons are based on this a Lego kit and it has got a boatload of components and therefore we can really go in and learn a lot all right what I need you to do is for lesson number 18 let me tell you what lesson number 18 is lesson number 18 is how to read data from the serial monitor if you think of what we've done so far we've like read values from a potentiometer and then done something with an LED but a lot of times what you might need is you might need a number from the keyboard like how bright it you might want to know like how bright do you want the LED or you might want to know how many times you want to blink the LED you need to get data from the user you do that through the serial monitor and then the Arduino can go off and do something based on what you told it to do so the Arduino can listen to commands that you get from the serial monitor and so we are going to do that to start with I'll show you what you need out of your kit I will get out of your way you need your breadboard you need your Arduino you need the red LED red you can if you want substitute yellow use blue don't use green those are for special occasions and then a 330 ohm 330 ohm current limiting resistor alright now we are going to come over here and we are going to start coding let's look at this window here I kind of like and so how do we get input from the serial monitor well we got to start by setting up our variables so what I want to do is I want to get a number from the user I want to get a number so I've got to set up a variable I'll just call it my number and then I'm not going to put a value in it why am I not going to put a value in it I am NOT going to put a value in it because you're gonna read that from the serial monitor I'm just setting it up and then I'm not using any pens right now so I've just I'm just getting a number my number will come back in a minute and we'll add more variables as we need them but what do we know we're gonna do well if we're gonna read from the serial port we've got to turn it on so serial dot begin and our most trusty old standby 9600 and then you know old guys like me we use 9600 as a pod rate because back in the day you were always like you know you had flaky connections he had to kind of go slow or things would mess up so us old guys kind of use these lured numbers your new guys you just go right to the 515,000 200 or whatever we go a little bit slower just because we can okay well so we start the serial monitor now what is it that we want to do we want to read a number from the serial monitor in order to do that you've got to do three things the first thing is you have to ask for it once you've asked for it you have to wait and then after you've waited you have to read and so I'm going to say this over and over and over to get data from the serial port you got to do three things what three things ask wait read so let's start with ask we're gonna need a message so I'm gonna say string I'm going to create not staying string I'm going to create a string variable called msg for message and what is it it is going to be the message we send them please enter your number and then end it with a quote in a semicolon all right so what three things do we do to get input from the user through the serial monitor ask wait read the first one is asked so we are going to do a serial dot print serial dot print serial dot print L n that will send a message to the user what message do we want to send the one we just came up with msg all right so what three things do we do ask wait read how many of those have we done one we have asked okay this is the problem guys the computer is so fast if I just do ask and then read it's gonna be a screed and you didn't give a person a time to put their answer in so you say well put a delay in there all right if you ask grandpa to enter a number it might take him five minutes because he might be looking for his reading glasses or something so you say okay we need to put a delay of five minutes no because jr. he can enter it real quick like a second cc some people could input things quickly some people could input things slowly so the problem is how long do you want to wait well the answer is you want to wait until there's data on the serial port so how do we do that well we create a while loop so we're gonna Wow we're gonna loop we're gonna loop on what condition well while serial serial dot available and then open close parenthesis while serial dot available equal equals zero remember in a conditional we have to use two equal sign if we just said while serial dot available equals zero we would be writing a zero we would be making serial dot available equals zero we don't want to do that we to check and see so what this is saying is while serial dot available equal equal zero what do we want to do while serial dot available equal equals zero while there is no data on the serial port we should do absolutely nothing so we don't put anything in this well the purpose of this while loop is just to hang your program until the person enters the data and sends it and then when they send it then serial dot available is no longer zero and then you will drop out of the while loop and come right here so to read data what three things do we do we ask we did that here we what we wait and then we do what we read this is how you read what was our variable up here our variable was my number my number is equal to now we want to go out and read the number off of the serial port we do that with the most excellent command serial dot parse int open closed parenthesis that will go out once the data is there we waited in the while loop then once it sees data it drops down here and it goes and gets the numbers so my number is equal to serial dot parse int now what do we do well we should print it probably and we should probably say serial dot print fell in and then we should do some nice thing here we can say string and we could call it message two and message two is going to be your number is like that okay and we will do a print instead of a print line because we want this all on one line so it will say serial dot print message to like that and then serial dot print l in why because after this one we want to go to the next line and so we will print what my number like that all right it's a very very important part I need you all to hold your breath so hopefully this will compile all right here we go one two three boom look at that alright it compiled I need to go to a different view that will allow you to see the serial monitor I believe this will allow you to see the serial monitor and then I'm going to pop open that serial monitor it's asking me for my number what number do I want 23 do you see how I type 23 in the little window and then you can either click send or you can click or you can click enter on the keyboard or you can click send you see the program's not doing anything what is it's doing it's waiting on me it's waiting on the data it is waiting for me to come up and click send and then look your number is 23 how marvelous and now it's ready to play again what is your number my number is 12 enter your number is 12 that is really exciting do you guys see the power that this gives us this gives us the power to get our wishes into the yard we know so you don't have to program it in you don't have to go and change the program and put it in the program because if a user is using this he's not going to know how to program you want him to interact with the program through the serial monitor all right guys let's build our circuit really quick and I will come over here so you can see the circuit bigger I just need you to hook up this red LED really quick you should be good at this now so I am going to go quickly I'm going to put my current limiting resistor from column 20 or column 10 to column 15 then I'm going to connect the left leg my left I'm never sure what it is to you but the left leg to me of the current limiting resistor into pin 12 this time pin 12 and then the long leg of the resistor is connected to the right the long leg of the LED is connected to the right leg of the resistor long leg always needs to be going back towards the positive and sometimes these things don't want to go in okay the LED is in now I need to ground the led by hooking to the right leg of the LED and we go back to G in D right and that was pin 12 okay so let's look at a practical example of how we would want to do this let me come back over to here ah most excellent actually I think I can use this window all right so let's keep this code that we just did and let's just modify it what I want is I want to blink the LED but I don't want to program in it how many times to blink I want the user to select how many times he wants to blink the LED and so on message I'm going to change it to how many links do you want all right and then I probably don't need this message number two and now on my number I want a better variable than that I'm going to say num blinks num links okay now would I want to get from the user how many times he wants to blink in order to do that I need to do what three things what three things ask wait read so I'm going to ask by serial print lining message and that is how many blinks do you want now what do I do I wait I wait until when I wait until he answers the question this is going to be numb blinks that I am going to read from the serial monitor and now I'm not going to print a message what am I going to do I am going to blink the LED and we are going to do that without what kind of loop a for loop so we are excuse me I'm becoming somewhat parched okay so we're gonna put a for loop for and I am going to need a little loop counter up here so I'm gonna say hint J like this and then I will come down for J equal one and I keep looping as long as J is less than or equal to num blinks and then not yet and then J equal J plus one every time through I will increment J and then I open that clause hit enter and it automatically puts the closed clause in and now what do I want to do in here I want to blink the LED I better come up with another variable up here int and then I'll just make it easy BT blink time is equal to let's say 500 like that and then how do I blink well one blink would be let me do a digit toll right up I better put a pin in and I am working on pin 12 so I better come up and put a variable in int is red red pin is equal to 12 because the red LED is hooked to that so I'm going to digital write red pin high okay now what am I going to do I'm going to delay delay delay how long blink time and then I'm going to digital write me red pin and then I'm going to go low and then I'm going to delay in time like that yes so every time through the loop it's going to turn the red LED on it's going to wait turn it off and wait and then how many times does it blink numb blanks where does nothing come from none blinks comes from the user how do we get data from the user we ask wait read does this make sense okay so this is I will need you all to hold your breath to hope that this works I will need to come back over here so you can see the serial monitor but remember hold your breath here we go oh darn it what did I do wrong Oh me and I hope you guys were screaming at me you don't use a comma you use a semicolon to separate these things okay now the real reason is somebody didn't hold their breath so let's hold our breath this time mmm it worked well at least it compiled so now let's open up the serial monitor and see what happens how many blinks do you want alright I want four blinks I put in four what is Arduino doing it's waiting for me to hit Send you see I'll have a cup of call it'll sip of coffee I'll stretch and now grandpa will hit Send and be watching this over here okay I'll hit Send and one two three ah the LED is barely blinking it is so good I just spilled my coffee look at this it so dim you can barely see it let me try this again how many blinks do you want I want four blinks do you see that it is so dim you can barely see it ha this is called troubleshooting something isn't working in our program the LED is barely blinking so what are some things that could be wrong well one thing that could be wrong is the LED could be bad so let's troubleshoot I'm going to take the LED out and I'm going to try putting in a yellow LED and then let's see what happens okay let's put in a yellow LED and then let's try it again I'll call up the serial monitor and I want four blinks and then I click send okay you see you can't even see it you can't even see it so what do you think let me try putting this in again and I do have it in the right direction though the long leg is going towards the top the long leg is going towards the positive voltage and so let me try it again and see if it's gonna work I'm gonna put it in for you see it's so dim okay I wonder if the LED is bad well what I could do is I could take this and I could hook it directly up to five volts and look at that it's very bright so what did I just say I just figured out that it is not the LED the LED is working if i hook it to five volts it's working what all does that tell me is working that tells me that LED is working the resistor is working the wires are good and all of my connections are good and so why is it blinking so dim if it's not the hardware it must be the software hmm I come over here and look and I look at void setup and what did we forget to do we didn't do the pin modes haha man and I just felt all right we didn't do the pin modes right so I need to do pin mode and then what do I need to do red pin is a what output did you guys catch that you know sometimes I do these things on purpose just to make sure that you are paying attention just to make sure that you were paying attention so let's download here what do we need to do hold your breath alright it worked okay it worked so let's call up our serial monitor alright okay how many times would you like to blink I think I would like to blink four times now keep your eye on the LED while I hit enter and we get one two three I don't see did that work let me see before let me watch it only blinked two times what is wrong here J is equal to one num blank oh look at that did you catch that I said J is equal to J plus 2 it should have been 1 J is equal to J plus 1 do you notice how sometimes I put mistakes in just to see if you're paying attention and also then you can see how we debug you see it wasn't blinking the right number of times so where did we go we went to the for loop and we see that so we are going to download ok now we are going to open our serial monitor where did the serial monitor go it's back there okay how many times do you want to blink for I'm gonna click enter and then I've got one two three four and then what does it do it comes back and ask me again how many times you want to blink want to blink three times this time one two three I am in control of my destiny it is up to me how many times I want to blink to LED okay does this does this makes us let's do one more because we're on a roll here okay and this one won't have this one we won't use the we won't use the led on this one so what we're going to do though is I and I'll get rid of this so what I'm gonna let's calculate I mean let's just do something different let's calculate the area of a circle all right and so all the sudden now what variable do I need well one is the radius is the radius gonna be a round number know the radius could be anything so I'm gonna call it a float because a radius could be an in-between number so I'm gonna say R ad us and then do I put a value in here no because I'm gonna get that from the user and then I'm going to say what is the the radius of your circle sir Cole what is the radius of your circle and then I'm going to need a float for area because we're gonna calculate area and that could be an in-between number so that needs to be a float so message is what is the radius of your circle and then I need another string and that is going to be message two which is going to be equal to the message to that I'll sent your circle has area of and then like that and then I'll put a space so that the space will be there and make a nice a nice sense so I think that's good so I'm not going to need this pinmode this time and then what I'm going to do is serial print line message what is message what is the radius of your circle and then what do I need to read I need to get rid of this for loop all right now what do I need to read I need to read radius okay but what you need to see here is you need to see that radius is a float so if radius is a float do I want to do serials serial dot parse int no I want to do what serial dot parse can you guess float and what it gets the happy little orange color meaning that it recognizes it and then what can I do I can say area is equal to let's be good and let's make a new variable up here of float pi equals 3.14 all right and then down here area is equal to pi times radius times radius okay and then serial dot print and we're going to print message two and then we're going to say serial print and we are going to print area all right so let's see if this works please hold your breath this time oh darn it float ah right here I didn't put the what : and you didn't hold your breath so everyone really I need everyone to hold their breath yes okay so now we will open the serial monitor and then it says what what is the area of your circle my circle is area of - look at that I probably did a print where I needed a print line you see how I ran those two together so let's look oh yeah I did that I printed message two and then I printed area where that should have been print L end okay I'm we don't have to hold our breath this time I know it's gonna compel I only do that when I'm not sure all right so now we're going to bring the serial monitor back what is the area of your circle - okay your circle has an area of 12.5 6 well 2 times 2 is 4 times 3 something that looks about right but look I can say 2.1 and then it will say your circle has an area of 13 this is working okay what did we learn today we learn to debug our code we learned that if you don't do your pinmode your LED is not going to light right we learned how to read integers from the serial monitor we learned how to read floats from the serial monitor pulmicort or top tech boy comm think about giving us a thumbs up leave some comments down below okay come on give me some feedback are you guys following along am i rambling is this too long is this about right give me some feedback and then think about subscribing to the channel if you want notifications make sure you ring the bell buy notifications if the little bill Palmer quarter from top tech boy calm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 114,110
Rating: 4.9717875 out of 5
Keywords: Arduino, Serial Monitor, Serial Port, Tutorial, Lessons
Id: 7aP5KL5clWA
Channel Id: undefined
Length: 27min 44sec (1664 seconds)
Published: Thu Jul 25 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.