
The character array needs to be as large, or larger than the final output string. Let’s take a closer look at each line of code.
#Arduino serial print code#
Here are the three lines of code you’ll need: char buffer sprintf ( buffer, "The %d burritos are %s degrees F", numBurritos, tempStr ) Serial. We can print out as many variables into our string as we want, and the amount of code required always stays at a manageable three lines. So for this helpfully informative burrito update, you’d be staring down the barrel of nine lines of code.
#Arduino serial print serial#
“The 3 burritos are 147.7 degrees F, weigh 14oz, and were finished 3 minutes ago.”įor every variable you add to the output, you add two more serial prints in the code. What if you wanted to print a line with 4 variables inserted into a string like this: In fact, using Serial.print() to build a string gets even more clunky the more variables you add. But Great Auntie Gertrude’s Carbunkle, is it clunky.

Now to be clear, there’s nothing inherently wrong with using Serial.print() to build a string. Using Serial.print() you’d typically write five lines of code to print out that single line of text. In this example, consider both the number of burritos and the temperature value as variables. How would you print a string using text and variables using good old Serial.print()? Let’s say you want to print this line of text to the Serial Monitor: So here’s an Arduino 101 question for you. In this lesson, you’ll learn all the above and a little bit more.įirst, let’s revisit the clunky way to print a string If that sounds like a handy chunk of knowledge, you’re in the right place. Add that up over a whole project, and it’s a pretty big deal. What would take you nine lines of code with standard serial.print commands you could do in just three using sprintf(). I’ll get into the details shortly, but here’s the main reason you should master sprintf():

It’s your go-to function if you’re looking for a more efficient way to combine text and variables into a string for output to the Serial Monitor. Sprintf() stands for “string print format(ted). Sprintf() – Ugly to say, but incredibly useful to master. Get 10 tips every new Arduino coder should know ➜
