If you don’t learn sprintf(), your code will hate you later

Programming Electronics Academy
Programming Electronics Academy
50.3 هزار بار بازدید - 3 سال پیش - 🤩 FREE Arduino Crash Course
🤩 FREE Arduino Crash Course 👇👇
https://bit.ly/get_Arduino_skills

Want to learn more? Check out our courses!
https://bit.ly/2SMqxWC

We designed this circuit board for beginners!
Kit-On-A-Shield: https://amzn.to/3lfWClU

FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: Facebook: ProgrammingElectronicsAcademy
Twitter: Twitter: ProgElecAcademy
Website: https://www.programmingelectronics.com/

**Get the code, transcript, challenges, etc for this lesson on our website**
https://bit.ly/2Ra5zAy

SPRINTF() WITH ARDUINO | PRINT MULTIPLE VARIABLES TO THE SERIAL MONITOR
Are you trying to figure out sprintf() with Arduino?

Or maybe you want to display multiple variables on the serial monitor without having to use a bunch of separate Serial.print() statements.

If so, you’re in the right place.  In this lesson you’ll learn exactly how to use sprintf().

JUST USING SERIAL.PRINT()
Let’s say you want to print this line of text to the serial monitor:

“The 3 burritos are 147.7 degrees F”

Where the number of burritos and the temperature value are both variables. Using Serial.print() would take 5 lines of code to print out just this single line of text.

 Serial.print("The ");
 Serial.print(numBurritos);
 Serial.print(" burritos are ");
 Serial.print(tempStr);
 Serial.println(" degrees F");
In fact, for 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:

“The 3 burritos are 147.7 degrees F, weigh 14oz, and were finished 3 minutes ago.”

It would take 9 lines of code!

SPRINTF() TO THE RESCUE
This is where sprintf() comes in handy. We can print out as many variables into our string as we want, and the amount of code required stays at 3 lines.

Here the three lines of code you’ll need:

 char buffer[40];
 sprintf(buffer, "The %d burritos are %s degrees F", numBurritos, tempStr);
 Serial.println(buffer);
First you need a character array to save the output string into.
Then you need the sprintf() function, which will combine our text and variables into a string.
Finally, you use Serial.print() to display the formatted string.
Let’s take a closer look at each line of code.

 char buffer[40];
The character array needs to be as large, or larger than the final output string.
So count the characters you plan to store in that string, and make sure the buffer is at least that large.

The next line of code is the actual sprintf() function. sprintf() stands for “string print format(ted)”.

 sprintf(buffer, "The %d burritos are %s degrees F", numBurritos, tempStr);
sprintf() takes a minimum of 2 arguments. The first argument is where you plan to store the string that sprintf() will be making for you. This is where you use the character buffer that you created on the previous line.

show buffer argument in sprintf()
The next argument is the string you want to create, filled in with format specifiers where you want to insert your variables. The format specifier is the % sign. The letter following the format specifier is called the format character, and it tells sprintf() what datatype will be used for that variable.

show the second string argument for sprintf(), with format specifiers labeled
 "The 3 burritos are 147.7 degrees F"
In this example we have 2 format specifiers (%) – this means we want 2 variables inserted into the output string. The character specifiers are a little weird at first. They are simply letters that stand for the kind of data type that will be inserted – once you learn what each letter means it starts to make more sense.
3 سال پیش در تاریخ 1400/03/14 منتشر شده است.
50,314 بـار بازدید شده
... بیشتر