sprintf() with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- [Instructor] Are you trying to figure out S print F with Arduino or maybe you just want to display multiple variables on the serial monitor without having to use a bunch of separate serial print statements. If so, you are in the right place. In this lesson you'll learn exactly how to use S print F, stay tuned. Subscribe or YouTube channel to get more videos like this. Are you learning Arduino programming? Check out our membership program to learn the software and hardware skills, you'll need to build your own projects. You will get an all access pass to our high quality video training that covers everything from the basics of programming, like variables and control structures up to using interrupts, arrays and more. Follow the link in the description to sign up today. All right, well, let's say you want to print this line of text to the serial monitor, where the number of burritos and the temperature value are both variables. Using the serial print function, would take like five lines of code just to print out this single line of text. In fact, for every variable we add to the output string, we have to add two more serial prints in the code. So, if we wanted to print something with four variables inserted into our string, it'd take like nine lines of code. This is where S print F comes in-handy. We can print out as many variables into our string as we want. In the amount of code required stays right at about three lines. Here are the three lines of code you'll need. First, you need a character array to save the output string into. Then you need the S print F function, which is gonna take some texts and variables and combine it into a single stream. Finally, we'll use the serial print function to display the formatted string. So, let's take a closer look at each of these lines of code. So, the first thing is a character, buffer character array. The character rate needs to be large or larger than the final outfit string. So, you need to count the characters you plan to store in that string and make sure that the buffer is at least that large. The next line of code is the actual S print F function. S print F stands for String Print Formatted. The function takes a minimum of two arguments. The first argument is where you plan to store the string that S print F will be making for you. This is where we use that character buffer that we just created on the previous line. So, the string that S print F formats is going to be stored in this character buffer. The next argument is the string that you want to create. It's gonna be filled in with format specifiers where you want to insert your variables. The format specifier starts with the percent sign and the letter following the percent sign is called the format character. And it tells, S print F what data type is gonna be used for that variable. So, in this example, we have two format specifiers. This means that we want two variables inserted into the output string. Now, these character specifiers are a little weird at first. They're just these letters that stand for the kind of data type that's gonna be inserted. And once you learn what each letter means it starts to make a little more sense. But until you figure that out it's kind of like, what does this mean? So, here are some of the common character specifiers, a D or an I is a signed decimal integer. A U is an unsigned decimal integer and an S is a string of characters. So here, when we see this percent sign D, we are telling S print F to format the inserted variable as assigned decimal integer. Now, if you're wondering like, what the heck is assigned decimal integer? Well, here's a scoop, signed means that it can be positive or negative, decimal means that we want it to show up in decimal form, instead of like formatted as an octal or a hexadecimal or something like that. Integer means that it's just a whole number that is there aren't any decimal points in it. In this example, we're also using the S character specifier. This specifies a string of characters. So, where does S print F actually find the variables to insert? Well, we actually don't have to look too far, because those are the arguments added right after the string. For every format specifier, you must pass a matching value. These values are added as additional arguments to S print F, each one separated by a comma. In this example, we have two format specifiers. Therefore, we have two arguments at the end. The first one, numBurritos will get inserted at that first format specifier. The second one, tempStr will get inserted at the second format specifier. If we add more format specifiers in our string, we'd need to add more arguments to the end of S print F, hopefully this whole S print F thing is kind of making sense so far. But maybe you've got this little nagging question right now, you're like, "Hey, wait a second, I thought you said that the S character formator was for a string of characters, but the temperature in Fahrenheit is a floating point value, what gives? Well, here's the deal, S print F with Arduino cannot handle floating point values. So, if you have to print something that has a decimal point, like 3.14 or 156.7, then you need to convert that float value to a character string first, and then you can print the string. A handy way to do that is with D to string F, which converts a floating point value to string. We won't get into that now, but be sure to check out our other video on using D to string F with Arduino. Okay, the final line of code in our trifecta here, is the good old serial print. And what we pass as an argument is the character buffer where S print F stored are formatted string. You'll notice that S print F isn't returning the string itself, it saves that string into the character buffer we had specified, which is why all we have to do is print the buffers content to display the string. Now it's worth noting that S print F does return a value, if you choose to use it. Which is the total number of characters that have been stored into the buffer. This return value excludes the null terminating character, that's also added by S print F. So, now with these three lines of code, we can open up the serial monitor and see that the string has been inserted with the variables showing up pretty nicely. So, using just these three lines of code, we can insert a bunch of variables into a single string and print it out to the serial monitor. And it comes up nicely formatted. If we ever want to go back and add more variables to the string, all we have to do is add the appropriate format specifiers and then the corresponding values to the S print F function. And hey, we're good to go. Okay, let's do a quick review of what we've learned about S print F. The first value that the S print F function expects is a character buffer. This is where the formatted string will be stored. The second value in S print F is the string you want to format with any format specifiers. The final arguments are the values you want to replace the format specifiers with. Now, believe it or not there's actually a ton more stuff that you can do with S print F, in fact between the percent sign in the character specifier, you can insert what are called sub specifiers. In these things we'll do everything from left justify the inserted values to add leading zeros and more. It's actually pretty cool. Let us know in the comments, if you're interested in a following lesson, using more of these optional S print F sub specifiers and we'll see what we can do. Thanks a lot and I hope you have a great day. Bye.
Info
Channel: Programming Electronics Academy
Views: 7,456
Rating: 4.9712229 out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, Programming Electronics Academy
Id: wCfH9VlJKrY
Channel Id: undefined
Length: 8min 34sec (514 seconds)
Published: Fri Jun 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.