Загрузка страницы

sprintf() with Arduino

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

SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------
Get your Free Trial of Altium PCB design Software
https://www.altium.com/yt/programmingelectronicsacademy
We use Rev Captions for our subtitles
https://bit.ly/39trLeB
Arduino UNO R3:
Amazon: https://amzn.to/37eP4ra
Newegg: https://bit.ly/3fahas8

Budget Arduino Kits:
Amazon:https://amzn.to/3C0VqsH
Newegg:https://bit.ly/3j4tISX

Multimeter Options:
Amazon: https://amzn.to/3rRo3E0
Newegg: https://bit.ly/3rJoekA

Helping Hands:
Amazon: https://amzn.to/3C8IYXZ
Newegg: https://bit.ly/3fb03X1

Soldering Stations:
Amazon: https://amzn.to/2VawmP4
Newegg: https://bit.ly/3BZ6oio

AFFILIATES & REFERRALS
---------------------------------------------------
►Audible Plus Free trial: https://amzn.to/3j5IGrV

►Join Honey- Save Money https://bit.ly/3xmj7rH
►Download Glasswire for Free:https://bit.ly/3iv1fql
►Download Glasswire for Free:https://bit.ly/3iv1fql

FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: https://www.facebook.com/ProgrammingElectronicsAcademy/
Twitter: https://twitter.com/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.

Видео sprintf() with Arduino канала Programming Electronics Academy
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
4 июня 2021 г. 19:30:58
00:08:34
Яндекс.Метрика