arduino float to string
This is going to be a short lesson, but an important lesson in lesson: number five. We will be using the same circuit that we have used in the last couple of lessons. Specifically, that is this one here you can see down in the lower left. We already have it hooked up, but if you need to take a few minutes to get it hooked up, hopefully you already have it hooked up because we’ve been working with it for a while, but if you don’t, you can pause here and hook it up. If you need help and hooking it up in lesson, number three watch the video. I go through it step by step about how to get it hooked up, but hopefully you’re getting comfortable with it at this point and that you can just kind of hook. These things up yourself, we will be using this schematic for this exercise and then also, hopefully in lesson number three and lesson number four. You still have your code. If you don’t have your code, I have it here, go to top tech boy. Comm go to lesson! Number five and you can get the code if you want to get the code, you can get it right off with this right off of this posting I’ve said this before and I’ve said this again. I really hope that you’re not just going in and copying and pasting my code, because if you do that you’re going to be able to mirror image what I’ve done, but you’re never going to be able to do anything on your own.
I put it on here. So that, if you get stuck, you can come and look at it. You can see the way I did things it can help. You think about things, but you’ve got to get to the point that you’re writing your own code, because you will never be a programmer if you’re just sitting and copying and pasting other people’s code. So I put it up here as a resource for you. I put it up here to help you if you get stuck, but I just really caution you that the the guys that I have known that just have gone in and sort of packed things up by copying and pasting other people’s code. They never get to. The point that they can do anything on their own and so what your goal is. Your goal is not to just get through these lessons or to get through this class. Your goal is to become a programmer and, if you’re going to be a programmer, you’re going to need to do a lot of this stuff on your own. So just just hope, you’ll. Think of think about that a little bit but in case you’re, just jumping in here and get the code here on lesson number five and you can take that code and you can put it into your Arduino and when you put it into your Arduino and download It you should get something that you see like this down in the lower left, where the le the red LED blinks and then the yellow, LED points.
Let’S just give a quick recap of this code. The last thing that we talked about was let’s see we set everything up here. We declare our variables and assign our variables and two of the key things are the number of time that the yellow, LED blinks is set to five the number of times the red LED blinks is set to five, and then we have two for loops. We we loop through this for loop blinking, the red five times we blink through this. We loop through this one five times to blink the yellow one five times, and so we’ve got two for loops. By changing these two numbers, we can change how many times these respectively blink and by setting these various time parameters up here, we can control how long each one is on or off so by playing around with this code up here, we can make these LEDs do Whatever we want them to do, okay, what we talked about in Lesson four is: is that you get to the point, though, that you want to be able to interact with the outside world, so we do that over the serial port and your void set up. You turn your serial port on with a serial begin. I’Ve set the baud rate here to 115200. If we look at the serial monitor by clicking here, you with a little look – and I need to maybe pop this up here – what you can see is the things that we are saying to print here are printing out on our serial monitor.
Also, if you look, the serial monitor, is in sync with a circuit over here. So it says: blinking red as it’s blinking red it says. Blinking, yellow is it’s, blinking, yellow, so everything’s kind of starting to work together, live and so that’s sort of that’s sort of exciting we’re kind of getting a lot of things going a lot of different directions here, but one of the things I want to talk to You a little bit about here today is using strings in our lessons up in this point. We’Ve used a lot of variables, but we have always used variables of type int for integer, which means everything that we’ve done so far. That’S been a variable we could handle with an integer and an integer. Basically, the round numbers it’s like minus one, zero one, two, three four it’s the positive and the negative round numbers with also including zero and because we’re doing things like blinking a certain number of times or we’re. Talking about a certain pin. All of those things can be represented by round numbers if you think I’m going to have a pen, 9 or I’m going to have pen 10, but I would never have pen 9 and a half, so I can use integers on the amount of time that I’m Going to leave the LED on in milliseconds 250, you know we would in in milliseconds. We probably wouldn’t want a fraction of a millisecond, so we can use integers there now if we want the in between numbers or what you learned in math classes, the real numbers.
What we would do to get the in between numbers or the real numbers? We would declare these things as floats. So once we start doing, calculations like let’s, say we’re going to do a temperature sensor and we’re going to measure temperature and then we’re going to put the temperature that temperature reading in a variable that we called temperature. We probably want that to be a float, because you might want to know that it’s 87 and 12 degrees it’s supposed to 87 point three degrees. And so, when we start doing more precise calculations we’ll go to float. But the reason for using intz ints intz is integers is that they take a lot less memory in the microcontroller. Now, when we’re doing, Windows, machines and we’re writing programs, you don’t think about you’ve got just so much memory in your big machines a lot of times. You don’t think that much about how much memory a float versus an int would use, but when we get down here to the Arduino, you can actually run out of memory on board. An Arduino and I’ve had several programs that have gotten complicated enough that actually ran out of memory, and so, when we’re working on a microcontroller that’s got a more limited amount of memory. We do want to be mindful about using intz versus using floats, but what I’m really going to talk to you today about in this videos? It’S, not about floats it’s about a different type of variable, called a string and a string is really it’s.
A string of characters. Okay, so if I want to say the word hello, the word hello would be a string of characters, and so I would declare that as a string and if you look, I sort of already started using strings this last program. It’S, just we didn’t declare them a variable. We just plugged the string in we didn’t put the string in a variable. We just use the string directly and so, for instance, in this line here, where we say serial dot print line, the red LED is blinking. The red LED is blinking, those are inside, quotes the double quotes, and what that’s telling Arduino is everything between those double quotes? Is a string and it’s saying hey print everything between the double quotes and what’s between the double quotes is the string, but what I want to show you is there’s a different way to do that that we can actually take that string and we can put it Into a variable, what type of variable would we need a string variable if we’re going to use a variable in Arduino? We need to declare the variable remember that if we do things up here, they are global variables, meaning anything we declare up here everywhere in the program is going to know it and so we’re going to go ahead and make this a we’re going to go ahead And make this a global variable, and so what type of variable should it be? It should be a string, and it makes me happy that it turns orange, which means I type it in right, because it recognized it sometimes it’s kind of funny.
If you look at int, you declare an int with the lowercase I, but if I try to declare string with a lowercase s, it doesn’t work string, needs to be a capital list and that’s. Why it’s kind of nice in these integrated development environments, like we are using here and maybe a third time, will be a charm there? It is, is to watch when something doesn’t, color it’s sort of a flag that it hasn’t been recognized in. So you see string so we’re going to declare a variable in the variable that I’m going to declare is red message: okay and read messages the variable it’s, not a it’s it’s, a variable of top string, it’s a variable now that I can assign a string to And what do I want to assign to read message? Well, my read message was down here: the red LED is blinking, the red LED is blinking blinking, okay, and we will close that it turns blue, which means hey. I recognize it so that always gives us a little warm fuzzy feeling when these things turn colors. It means that it’s recognizing what we’re doing and then what we are doing is we are declaring a stream variable. Okay, the variable is red message. That’S, like the bucket, the bucket is called red message. The message in the bucket is the red LED, is blinking, so you’ve got to get your mind around the difference between the name of the variable that’s, like the label that’s on the can, and then the message in the can is the red LED is blinking.
Now the cool thing about this is when we come down here. Instead of printing, the red LED is blinking. What we can print is just red message: okay, I’m, going to go ahead and run this and make sure I haven’t made a mistake already, so they can me feel good, seeing a lot of green, a lot of green no orange. Sometimes it takes a second time to download this. I don’t know I might need a better serial. Cable I’ve got a really long, ass stuff. That I’ve got a really long serial cable, and it might be that it is just not working perfectly so let’s see here. Okay, I still did not like at that time. I’Ll. Try it one more time. Sometimes, if you don’t have a really good serial cable, it will kind of goof up a little bit okay. It took it that time now, if we come and look at our serial monitor by clicking there, it says the red LED is blinking. You are on blink number one. Two, three four five, the yellow LED is blinking. Okay. Now, what I want you to see so that actually worked up here is where I told it red message: is this string and then down here I just serial dot print line, the variable name and when you do a variable name, you don’t put the quotes around It okay you’re, saying print what’s in the bucket called read message: okay, you don’t come in here and say print the string read message, because if we did that what’s going to happen, it’s not going to be pretty you see you don’t want to do that.
It says read message you just said print that exactly you don’t want to print the string. Read message. What you want to print is: there is a bucket named a bucket. A variable named, read message: what message is in that bucket? The red LED is blinking, so we don’t put quotes around it. If you want to directly print a string, you put the quotes around. If you want to print the variable that contains the string you don’t put, quotes on it, and the reason I kind of go into this is: is that it as I’m teaching students? It seems to me this is something that they stumble on. You stumble on for a little while, but once you kind of get familiar with it, it’s really pretty it’s, really pretty easy, and so just think of the variable as a bucket and the variable name is the label on the bucket and then the string or the Number or whatever it is, what value you put you put in that bucket. So just like, we said, read message here down here. We should probably set this to two yellow message: okay, but if we’re going to set this to yellow message, we’ve got to declare the variable. You can never use a variable without declaring it. So we come down here. What type is it going to be it’s going to be a strong it’s going to be yellow message, and so we just made a new bucket that new bucket is labeled, yellow message.
And what message are we going to put in the yellow bucket we’re going to say the yellow LED is King end closed everything out, don’t forget the semicolon, be a good boy and put a comment. We are declaring a stream very edible. Okay, so now let’s see – and now we come down here and instead of printing the string directly we’re going to print we’re going to tell it to print the variable yellow message print the print. The contents of that variable is what we’re going to say. Oh wow did I do wrong because I am NOT so there is no in prepares no in parentheses needed there. Okay. Sometimes I make mistakes on purpose just to see if you’re paying attention that one was just a good old fashioned real mistake. So let’s look here: okay, so the red LED is blinking. That is red message. Red message is the red LED is blinking, yellow LED is blinking that’s, yellow message, but yellow message is set up here and so that’s just kind of neat that’s, a that’s sort of a that’s sort of a handy thing that you cannot just have numbers or numerical Values in variables, but you can also have strings and texts and there’s also things that you can do like let’s come down here. I don’t have to declare my variables up here: okay, I’m, going to make a local variable I’m going to make a local variable and I’m going to make it in my void setup, which means the only buddy that knows about this.
This variable is going to be void setup if I’m outside the void setup it’s not going to know about it’s a local variable, local to void setup but I’m, going to make a string, I’m going to say string does not like it because I said sting string Is going to be equal to I’m, going to say hello or I’m going to say, welcome to and welcome to all right, be good and put a comment, declare a string variable and assign a value so I’m declaring the string. I forgot to put the name here: I’m going to say: welcome message: 1, WM, 1 we’re going to declare the string variable and the name of the bucket is WM 1 and its content is welcome to remember anything that I put out here after these slashes it’s. Just a comment: Arduino doesn’t read it but it’s just for me and anybody that might be working with me to know a little bit more it’s like what am I thinking why don’t I put in that line in there. Okay now let’s make another string. Another variable string welcome message: 2 is going to be equal to and I’m gon na say my program, ok and then close that, and what did I forget up here? Why didn’t you warn me why didn’t somebody stop me inside in your semicolon: okay and then I’m gon na cite slash, slash, Eclair screen, variable wm2 and assign a value. Okay, so I’ve made two new string variables.
The first one is labeled WM, 1 and that’s. Welcome to the second variable, the second bucket is labeled WN 2 what’s inside that bucket the words my program, okay, and actually I need to do that after I turn on the serial monitor. So I like that sorry about that, need to get that serial, monitor on because I’m going to try to print something here and then let’s say that I’m now going to do a serial dot print line – and I just say: WM: 1, okay and I’m, going to Close that and let’s see what happens when we do that and this we’ll just do it one time it won’t loop, because it’s in the void, setup, you’ll just print it one time, okay and now let’s see what I have here welcome to all right. Why did it print the rest of it because I didn’t tell it to print the rest of it? I just said serial print line, W m1 okay. So if we want the rest of it, let’s make it and you’ll see why I’m doing this in a minute. You’Ll see I’m kind of trying to show you some new stuff you can do, but it will make sense admit what I’m doing this way: okay, serial dot print line and what’s the next thing that I want well, I want them to be 1 2, that 2nd String variable I’m going to print that now, let’s download that okay now it says, welcome to my program.
Well, it kind of printed the two things but it’s printing one on one line: the next on the next slide. How could I get this all to print on? One line: do you remember that this print line says go to the next line? Well, on this first one, we want it to stay on that line, so we just want to do a print if I just do a print now, I can come over here. Okay and I can wait for it and then it says – welcome to my program now what’s wrong with this look at this welcome to in my program, but I didn’t put a space in there anywhere. I guess I could make a third variable, which was a space and print that, or I could just add a space here and now, if I do that, it’ll print that space as far as as part of the first one let’s see what happens. Okay, welcome to my program: okay, that’s, looking pretty good that’s, looking pretty good, but I did it with two variables. Okay, I want to show you something that you can actually kind of almost do math or it’s called concatenation. What, if I want to bring those two strings together, make a new string, so let’s make a new string. My new string is going to be wm3, okay and then what’s wm3 going to be equal to and this what are we’ll show you is. I just declared the variable I did not assign a value to it.
You can just declare a variable without assigning anything to it. I’M, just gon na say I declare a string variable wm3 that’s, just a play so we’re, saying later on I’m, going to use a variable called wm3 it’s, a string don’t panic I’ve told you that I’m going to use it. Okay, I’ve told you I’m going to use it so I’ve declared it, but you can’t use it until you declare it now I’m going to come down here and now I’m going to assign a value to it. Wm 3 is equal to w m1 plus w m2, and what this is called is this is called concatenation. It puts the two strings together and it puts them in a new bucket called WN 3, which I declare it in the step above and so let’s come up here and say that we assign aa, WM, 1 plus or basically, what we’re doing is. We are concatenate in and I’m, not even going to try to spell that right: concatenative WM, 1 WM 2 and into WM 3. So I take WM 2 WM 1 WM to put them together and then put it in a new variable called WM 3. And now what I am going to do here is I’m going to print WM 3 and with a little luck let’s see what happens, looks like it’s going to compile. That is always good news, and then we look and look at that. Welcome to my program and it’s, okay, I see what’s happening you see.
I did. I need to do a print line in this case, because I didn’t advance to the next line. I hadn’t thought about this, but this one actually needs to be a print line so that I don’t end up with that next print statement on the same thing. So if I do that and come down print line, W 3 w m3 is w m1 concatenated with WN 2 and so I’ve combined those two strings and look at this. Welcome to my program, the red LED is blinking 1, 2, 3, 4, 5. The yellow LED is blinking, 1, 2, 3, 4, 5, and so you see look at the scenario start to war. Kind of with with strings of text is it’s things I can plug together and move around, and then the user here, like the human being that’s using this it’s sort of making sense that that this thing, that’s printing, is all synced up with what you see happening Down here, while this yellow is blinking it’s talking about yellow, while the yellow is blinking it’s talking about yellow when the red is blinking, it’s talking about red and we’re sort of moving this text around and if a person came in and was working with this, do You see it’s kind of something that a human being could understand, like maybe a normal human being isn’t going to understand all of this, but just your general person off the streets going to understand this it’s.
Only the red LED blink is blinking it’s I mean the yellow LED, is blinking everything sort of working together, and so one of the things that we have to do as a programmers make our program work in the real world, make it work with real people who Might not be programmers or might not be engineers, and so we’ve got to sort of wrap our code and stuff that’s, very usable and very intuitive for people to to use and right now we just start by by trying to make our text well, formatted and and Document our code and make things where it’s sort of easy to understand what is going on. Okay we’ve had a little bit of fun with strings. I appreciate you tuning in for lesson number five. Please come back for lesson number six and lesson number six we’re going to be seeing. How do you get information we’ve seen how to get information from the Arduino to the screen? How do we get information from the screen down into the Arduino? We want to start sending data the other way on the serial port, and that is a really really important thing and that’s what we will be doing in lesson. Six again on Palma, quarter from top tech boy.
arduino float to string Video
arduino float to string news
Posted on Friday May 05, 2017Using Modern C++ Techniques With Arduino Hackaday … Continue Reading »
Posted on Saturday June 23, 2018Arduino And Pidgin C++ Hackaday … Continue Reading »
Posted on Friday March 02, 2018Unionize Your Variables – An Introduction To Advanced Data Types In C Hackaday … Continue Reading »
arduino float to string Social
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr