Arduino Tutorial 19: Reading Strings from the Serial Monitor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and I am here today with lesson number 19 and our most excellent new and improved Arduino tutorial series I'm going to need you guys to pour yourself a nice big mug of iced coffee and I'm going to need you to get out your e Lego super starter kit that you know our three project which we're using you don't have one yet look in the description click the link $35 you get the Arduino and all the accessories that we are using in this series of lessons so if you don't have one get one if you have one get it out and today what we are going to learn is we are going to learn how to read strings or read words or sentences over the serial monitor now if you think about lesson number 18 you learned how to read integers over the serial monitor and you learned how to read floating-point numbers over the serial monitor well today we're gonna learn how to read words because what would be an example of this like what if you ask what is your name then little Joey needs to be able to type in Joey so the Arduino knows who he's talking to so we learned today about how to read strings and actually you could think of something kind of interesting like imagine you have a project where you have LEDs and you want the user to indicate which LED he wants you to turn on so you see this is a pretty important thing to be able to do okay so what are we going to do here we are going to get a little bit better view which should be like maybe this one alright and then we are going to oh that is crooked I hate crooked things okay now we are going to get out of your way and we are going to learn how to read strings over the serial monitor so bring your Arduino IDE up if we are going to need us if we're going to read a string we should probably declare a string so string let's see I will say the string is my name and we're not going to put a value in it because we're gonna read that and then string stow ring we're gonna say msg the message is that you're going to send them is what is your name question mark space in that with a semicolon you know I just kind of make this stuff up as I go along and so when I get started I'm not always sure where I'm going but we are going to go ahead and say okay then we're gonna make another string which says MSG two equals hello space and then semicolon and then string message three equal comma space welcome to Arduino welcome to our dwelling point yeah all right and then close that up all right now I'm thinking I don't think we're using any pins yet so what do I need to do I need to start my serial port serial dot begin 9600 the Old Faithful 9600 now here we want to get from the person what their name is if you want input from the serial monitor remember lesson 18 what three things do you do huh what three things you ask you wait you read so let's ask so we're going to say serial dot print L in what do we want to ask well we want to send msg the message what is the message the message is what is your name also remember that when you put msg without quotes it is putting msg the variable which is this this is what's in it okay now we have asked now what we do wait so how do we wait well we do a while loop and then the condition on the while is while serial dot available open close parenthesis equal equal 2 equals pound pound equal equals 0 and then we create a clause and as I create that Clause with a curly bracket Arduino finishes it for me and what do I do I'm looping until data is available what do I do in that loop absolutely nothing what am i doing I'm waiting so I sit there as long as serial dot available as 0 and I do nothing but then when something is available when the person has responded then it will drop out of this loop and what do I do I read and what am I going to read I'm going to read my name is equal to serial dot remember how it was parse int for integers parsefloat for a float so what's it going to be for a string wrong you said par string but it's not that even though it should have been it is read string okay read string would enclose my name is equal to read string I believe that that will work okay so then what we want to do we want to do serial dot print and then what do we want to print we want to print the hello so that's message too I believe I do believe message too and then I want to serial dot print and what do I want to print message no I want to print the name write my name and then I want a serial dot print and what do I want to print I to print message three okay pause a second and see if you can figure out what this is going to do I want to print hello my name and then welcome to Arduino does that make sense okay we're going to compile this and when I compile it what do I need you to do everyone hold their breath I mean everyone because if somebody doesn't we're gonna get them their error message are you ready okay somebody didn't hold their breath who did not hold their breath ah we did not put a semicolon after read string the real problem was someone didn't hold their breath I need everyone seriously to hold your breath while we compile boom it downloaded okay so now we're going to open up the serial monitor we're going to have a sip of coughing coffee I'm getting a little parched yes delicious open up the serial monitor make sure you guys can see it oh look what is your name my name is Paul PL what's it doing it is sitting and looping and waiting in that while loop until I hit Enter when I hit enter serial dot available is no longer equal equals zero so it drops through and read so what do I do I send hello Paul welcome to Arduino oh look and then it put it there what did I do wrong what did I do wrong well I should have made this last one a print L in so it would go to the next line sometimes I do that just to see if you're paying attention I hope you were screaming at me to use a print go in there we don't have to hold our breath this time because I know it's gonna work all right what do I do what is your name Oh send hello Paul welcome to Arduino what is your name what if I was more formal and I said mr. Porter and I said send I like that better hello mr. recorder welcome to Arduino we are getting data we what can we do now we can do parse int to read integers parsefloat to read floats and read string to read strings right and this is kind of goofy right but it's serial dot rings ring okay let's do a real project now if you if all you guys were if you guys are just hit and run just trying to figure out how to read a string you got it but I think most of you guys actually play along with me and use and actually like to do projects and so what your son is I'll give you the assignment you pause the video you run and do it and then you can come back and watch me do it or do it with me but let's see if you can do it on the on your own what I want you to do is hook up a circuit with three LEDs we're going to have red and today special occasion special occasion because we learned how to read a string you can use the green in the blue normally we don't use green and blue LEDs those are saved for special occasions but today we're gonna do a red a green and a blue I want you to hook up the three LEDs and then I want you to ask the user what color LED would you like me to light up if they say red you light up the red if they say green you light up the green if they say blue you light up the blue okay pause the video boom okay guys I'm going to show you how to do this I'm gonna go quickly because by lesson number whatever we're on or what what lesson are we on man what lesson are we on we are on like lesson 19 by lesson 19 you should know how to hook LEDs up alright you should know how to hook up LEDs by now and so I'm going to go kind of quickly I'm going to put my first current limiting resistor from 10 to 15 column 10 to 15 and then my first LED better put that down 10 I know I'm gonna go from column 5 to 10 okay call them 5 to 10 do you want me to zoom in a little bit here oh you know what I can do really that would help I can move to this shot and you can see it better okay so I've got the LED between columns 5 and 10 and then I put in the red LED with the long oh that was a little disaster there I put in the long le i put the long leg of the LED in the same column as the resistor then i'm going to put my second current limiting resistor between column 15 and column 20 and i put it down here like this and cuz I hope you do kind of watch me because I try to build these things in an organized way where I don't have I don't have wires running every which way try to keep it organized on here so then the long leg the long leg of the green LED goes in the same column as the second leg of the second resistor and then I like to have things kind of where they look nice like that and then between 25 and 30 I'm going to put my third current limiting resistor and I'll put it again down there 25 to 30 all right and then you guys are not seeing what I'm doing I apologize when I'm not cognizant and kind of hiding things from you long leg of the blue LED connected to column 30 so it connects to that resistor and then I and sometimes these things when you get a new breadboard sometimes they're really tight and things don't want to go in and let me do better there the aggravating thing okay so now let me back off a little bit so you can see what I've got I've got three current limiting resistors in series with a red a green and a blue now I am going you know what's really cool is if you now color code your wires to your LEDs and it just makes it easier when you're debugging things so I'm going to put the first the resistor the red LED I'm going to go through the resistor I'm going to come over and I'm going to go pin 12 okay ten twelve so pin twelve will control the red LED the green LED I have a green wire and so that column fifteen that resistor is going to go to pin eleven and then I have a blue wire because I thought ahead and then the current limiting resistor on the blue LED is going to be connected to pin ten so I go twelve eleven ten if I did this right now I need to ground the blue and you know what the problem that I'm going to have is I will probably have too many no I have three I have three grounds and so the blue LED I will ground to the G and D that's by V in okay let me get these wires out of the way and then I will ground the bottom leg of the green LED that short leg of the green LED you guys I'm gonna go fast you if you can't figure this out go back to my LED or my breadboard one alright so there I've got a ground also by the end that's another ground that I have and now I need one more ground for the short leg of the red LED the short leg of the red LED is going to go to this jnd that's up here by pin 13 and the thing is is that I just don't I try to go directly to a ground I think you've got three grounds on that you got three grounds on the Arduino you've got three grounds on the Arduino so up to three I just go directly and now do not like how these things are kind of ugly this big ugly white wire because we want to fully experience the wonderfulness of these LEDs as they turn on okay now what I like to do is there's kind of like two reasons things don't work one reason things don't work would be if you don't hook them up right and the other reason that things don't work is because you don't code right so before I write the program like we really want it I just want to turn those LEDs on and make sure that they are hooked up right and so I can do that I can take this green wire and I can plug it into five volts and look green comes on I will put that back I will take the blue wire and I will hook it to five volts and look I get out of your way blue comes on okay and now I'm gonna take the red wire and put it to the five volt pen reg comes on so that just shows me that my LEDs are working and I've got the circuit hooked up right and so now it is just going to come down to coding to get this working and so let's come up here let's get rid of all this nonsense I'll leave the serial monitor on and then I will get rid of this stuff I'll try to use as much of this old code as I can I have to have the straight I can't stand crooked things I can't stand that wire in front of there need to get this one down out of the way you see I like to have o stay out of the way you aggravating thing okay I'm just gonna have to put my obsessive-compulsive disorder I'm gonna have to put it aside and move forward I'm going to have to move forward with this project and stop trying to get these things perfect maybe if I move that one this white one keeps getting over there okay that's just as good as it's going to get I got to quit fussing with it I got to move on I'm gonna move that down just a little bit okay now they're almost even there okay so now we're gonna move ahead so what do I need well I'm gonna need a hint for Rhett for red pin and what was my red connected to I do believe my red is connected it's supposed to be connected to 12 okay Dom messed him up again all right red pin is 12 I got to set up a variable int for green pin green pin should be 11 and then I'm gonna have an int create greed pin yeah that's what we need we do not need a greed pin we need a green pinned green pin and then it blue pin and that's gonna be equal to 10 all right so I've got those set up and then I need int my or this is gonna be a string string it's going to be a color I'm not going to put a value because we will read that now we are going to come down and in order to get input from the user what three things do we use do we do ask wait read so I'm going to ask so serial print line message is asking I need a message to send it so I'm going to say string string and it is going to be msg and that is going to be equal to what color LED all right so I'm going to ask him serial print line message what color LED then I'm going to what I'm going to wait so I sit in loop until he does something and then I'm going to read not my name I'm going to read my color oh man I've been sitting here I do not want to start all the way over I yeah I'm not going to start over on this one you guys are just gonna have to live with it and I'm gonna come back over here where I should have been all along okay so you can see I haven't done that much here and you can't even read that let's see this one I'm just having a terrible time here okay this one works okay so what I did red pin is 12 Green pin is 11 blue pin is 10 my color and then what color led a more professional person would start over and edit it but I'm tired at this point I want to start all the way over so serial print line message while serial dot available equal equals zero and then my color is equal to serial dot read string all right serial dot read string now what do I do well I've got to figure out what I'm going to do so I have to have some if statements what are the three possibilities it could be red it could be green or it could be blue so now I know what my color is so I'm going to say if my color equal equal remember two equals in a conditional if my color equal red then what do I do well I open my clause and then remember Arduino puts that one in to close it well then what do I want to do I want to digital I want to I want it to digital right okay and then how did I do these I said red pen I'm a digital right red pin a digital right right red pin what hi now you got to remember this is going to be looping and you don't know what state the other ones are in so you better make them all what they need to be because last time you might have said blue blue is still on so you better turn blue off or angry so green pin should be low and digital right blue pin what Lo and this is an example some good programming habits that you need to get into this is a good programming habit and what that good programming habit is is that don't just make red hi put the other ones where they need to be as well now we can go faster because we can copy this and we can copy it and then we can say if my color I'm going to paste it now control V if my color is equal to green then what do I want I want red to be low I want green to be high and then I want blue to be low all right now we're gonna paste this one more time control V and this red green this time Ballu okay and then this would be low and then green would be low and blue would be high so here we're gonna let a person choose the color of the LED and I'm really sorry I messed up earlier I hate it when that happens but I didn't really want to start this over okay hopefully you'll not unsubscribe and send me hate No okay so here we go so let's download this I need everyone to hold their breath at least it compiled okay so now I need to open up my serial monitor ah what color LED do you want mm-hmm special occasion I think I want a blue balloon okay hold your breath oh man and look how dim blue is blue kind of turned on but it's terribly terribly dim mm what two things could be wrong one is the led could be burned out or it's a circuit problem but wait we tested each one of those it couldn't be a circuit problem it must be a coding problem somebody tell me what my mistake was what do we not do in the void setup we didn't do our pin modes and that's how you get dim LEDs why do I do this because when I help students nine times suction my idea isn't working well did you did you do your PIN modes no do your pin mote so pin mode what red and what is that output and mode blue pin I like to go in order red red green blue blue always red green green blue RGB so green pin output pen mode red green blue Kim is an output okay we don't have to hold our breath because I didn't make any mistakes in here you see I knew it was gonna work right okay now we're gonna hit the serial monitor and what color LED well I want to balloon because it's a special occasion and we don't normally get to use the blue LED so I want to take advantage of it look at that beautiful blue LED what color do you want well we also don't use screen very much and so now I'm going to come in and I'm going to type in green ah the good news is the green came on the even better news is the blue went off and so let's go back to blue okay that works and now let's go red Oh what happened what happened red let's go in and investigate what happened here red is not working okay what is wrong here twelve its twelve eleven in ten so that's 12 11 and 10 red pin is out put green pin is out put blue pin is output read it put red pin hi it put green pin low it put blue pin low so it's red if green if blue you know a lot of times I do things deliberately just to see if I can mess you up oh you know what this is crazy mixed up this is crazy mixed up this is not grounded it was a circuit mistake okay there it is I don't think you guys should have made that mistake but I had the thing in the wrong thing so again a lot of times I deliberately make a mistake that one was a real mistake alright let's try it again where is that pesky serial monitor okay so let's go green then blue um bread boom okay that really works now watch this what if I say capital R IDI capital or let me go capital B lue capital P lue Oh doesn't work how come because case matters in these things so what do we need to do if we how do you know someone isn't going to capitalize that so what do I need to do I need to come over here and I need to do the or which is the bar bar my color equal equal and then they might type it like that blue alright and they might top it or my color equal equal green or up here on red or my color equal equal red Capitol red okay okay now if we bring the serial monitor up and now if I could do capital R IDI capital R IDI boom it works Capitol blue okay all little green it works you know I do actually like to torment my students and so you know what I'll do they'll do this and they'll get it all working and then I will come in and I will KITT I will type in caps all caps B lue all caps and it doesn't worry that they got away it just gives them practice to do this type of thing okay guys this has been kind of a fun lesson to do this hopefully you played along at home I would love to hear your comments down below I really like hearing that people are actually watching these things think about giving us a thumbs up subscribe to the channel make sure you ring the bell click the bell to get notifications about future updates from this channel think about sharing this think about enjoying some more coffee appreciate you guys tuning in this is palma quarter from top tech boy calm and I will talk to you
Info
Channel: Paul McWhorter
Views: 98,399
Rating: 4.9572673 out of 5
Keywords: Arduino, STEM, Tutorial, Lesson, Strings, Serial Monitor
Id: MAnAc_t0OrM
Channel Id: undefined
Length: 30min 18sec (1818 seconds)
Published: Tue Jul 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.