The final line of code in our trifecta is the good ol’ Serial.print(). I won’t get into that now, but be sure to check out our other video on using dtostrf() with Arduino. A handy way to do that is with dtostrf(), which converts a floating point value to a string. 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 print the string. Sprintf() with Arduino cannot handle floating point values. Now you might be like… “Wait a second now – I thought you said the “s” character formatter was for a string of characters, but the temperature in Fahrenheit is a floating point value – what gives?!” That’s what makes sprintf() such a useful function to add to your coding toolkit. However, it all goes onto that one line of code. If we had more format specifiers in our string, we’d need to add more arguments to sprint(). The second one, tempStr, is inserted at the second format specifier. The first one, numBurritos, is inserted at the first format specifier. In this example, we have two format specifiers, and therefore we have two arguments at the end. These values are added as additional arguments to sprintf(), each one separated by a comma. sprintf(buffer, "The %d burritos are %s degrees F", numBurritos, tempStr) įor every format specifier, you must pass a matching value. Now, where does sprintf() actually find the variables to insert? Well, we don’t have to look too far, because those are the arguments added right after the string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |