Paul McWhorter arduino lesson 13
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.
Paul McWhorter arduino lesson 13 Video
Paul McWhorter arduino lesson 13 news
Posted on Friday August 19, 2016Books You Should Read: Basic Electronics Hackaday … Continue Reading »
Paul McWhorter arduino lesson 13 Social