This is so much clearer than the online Arduino reference that is obviously written by well seasoned programmers and assume that you have solid grasp of the C language.
@ericBcreator Жыл бұрын
It is convenient and tidy but keep in mind this method is (a lot) slower than using a bunch of Serial.print commands, as I found out recently when I tried to clean up my new VU meter project code.
@kriscurkovic926510 ай бұрын
Yeah arrays in general are always quite slow. You have to create 40 bytes on the stack now, then sprintf the new string characters into each slot. If you deconstruct everything into machine instructions it comes out as a lot more processor cycles. Streaming bits onto the serial isn't that computationally heavy.
@NotMarkKnopfler Жыл бұрын
Everything said in this video is correct. However, a couple of things to bear in mind: sprintf is a LOT slower than Serial.print. Also, sprintf uses a lot of memory. On things like an Arduino Uno, Nano, or ATTINY85 you may struggle. What I do, to keep my code tidy, is write a function to display the things I want to display using Serial.print. You don't save on lines of code, but you can move those pesky Serial.print lines - which tend to clutter up the code - into their own functions. Which is much neater. It's also easier to remove them when you don't need them any more. So, using the burritos example from the video: void displayBurritosInfo(int numberOfBurritos, float temperature) { Serial.print("The temperature of the "); Serial.print(numberOfBurritos); Serial.print(" is "); Serial.println(temperature); }
@shadamethyst1258 Жыл бұрын
Also note that using sprintf is *very strongly* discouraged on non-embedded platforms, because of the risk of overflowing the buffer. The examples in the video could trivially be weaponized if the user had control on the `tempStr` variable
@PraveenChintha-j2t3 ай бұрын
yeah you are true rather you can use string concatenation property
@mtraven232 жыл бұрын
a good explanation...but thats still cumbersome. I prefer using the "Streaming.h" library. this allows for c++ style prints: Serial
@programmingelectronics2 жыл бұрын
Thanks for adding this Matt! I'll definitely check it out - I do wish the native Serial library just "made it easy" with out having to use the String class. If I could just do... Serial.print("I would like " + numTacos + " please"); Would that be great or what!
@jon_raymond3 жыл бұрын
The sub-specifiers are super handy and there isn't very many good explanations of them. A video detailing them would be super helpful.
@programmingelectronics3 жыл бұрын
Thanks for this Jon!
@GaboG8510 ай бұрын
The best explanation that u ever seen on KZbin. Loved it!!
@programmingelectronics10 ай бұрын
Thanks!
@iamsparkicus3 жыл бұрын
Cheers Fella, you are an amazing teacher. Love your channel. I am starting to understand things I thought were beyond my intelect! Keep 'em coming!
@programmingelectronics3 жыл бұрын
Thanks for watching!
@MN-be7sx2 жыл бұрын
please make very detailed videos series on sprintf and sub-specifiers
@John-qe3ky3 жыл бұрын
Very nicely explained, please continue with the additional functions.
@programmingelectronics3 жыл бұрын
Thank you!
@_GB843 жыл бұрын
Brilliant! Great info. I have been struggling with why it wouldn't print %.2f for a large majority of the day and now I know :D, thanks. You mention you have another vid explaining 'dtostrf' but I can't seem to find it. Can you point me in the right direction please? Thanks for your time and effort with your videos, they're super clear!
@KashifKhan-kw3ez3 жыл бұрын
itoa(), and dtostrf(), function detail explain.please
@programmingelectronics3 жыл бұрын
Thanks for the recommendation and thanks for watching!
@MrShanmunir2 жыл бұрын
great man. Really good explanation.. please do a follow up. Thx
@programmingelectronics2 жыл бұрын
Thanks for the note!
@PraveenChintha-j2t3 ай бұрын
Sir, actually no need to use sprintf rather we can directly use the string concatenation property in serial.print() function
@vikky4522 жыл бұрын
thank you sir, very good tutorial. plz make such wonderful tutorial.
@programmingelectronics2 жыл бұрын
Glad it helped!
@dalwinderssi40942 жыл бұрын
Yes , I want to buy ,lessions
@alejandrom723 жыл бұрын
Nicely explained. Thank you.
@programmingelectronics3 жыл бұрын
Glad you liked it! Thanks so much for the note and for watching!
@sombatmuenphan15552 жыл бұрын
Many thank.Excellent explain.
@programmingelectronics2 жыл бұрын
Thanks so much for watching!
@steventhehistorian3 жыл бұрын
You have the best Arduino function tutorials. Thank you so much for this content.
@programmingelectronics3 жыл бұрын
Thanks so much Steven!
@rossli8621 Жыл бұрын
Very nice video!!!
@programmingelectronics Жыл бұрын
Thank you Ross!
@user-su5sq5ib3i2 жыл бұрын
Nice explanation just what I hope will work with my microchip pic18f and C language. I'm doing a weather station
@programmingelectronics2 жыл бұрын
Thanks - best of luck in your project!
@user-su5sq5ib3i2 жыл бұрын
@@programmingelectronics just wanted to give you an update this piece of code that I wrote worked perfect thanks again
@programmingelectronics2 жыл бұрын
@@user-su5sq5ib3i So glad it helped!
@ED99LAM Жыл бұрын
very useful n informative
@programmingelectronics Жыл бұрын
Thanks!
@bobowen78613 жыл бұрын
Very excellent tutorial. You made this simple. Would be very interested in seeing an additional tutorial on using optional sub-specifiers.
@programmingelectronics3 жыл бұрын
Thanks!
@redcrafterlppa303 Жыл бұрын
This is potentially creating a buffer overrun. As it depends on how long the inserted numbers/strings are. When inserting strings you can never make the buffer big enough since you don't know the length of the dynamic strings at compile time. Meaning you would need to allocate the buffer on the heap wasting already limited heap memory on an arduino. Also this whole thing is just unnecessary. There is nothing wrong with multiple Serial print statements. In fact it's more efficient. There are cases where sprintf makes sense to use. But this isn't one.
@banditboy64443 жыл бұрын
As always, great vid
@programmingelectronics3 жыл бұрын
Appreciate that!
@christiaansteyn4088 Жыл бұрын
Please do the additional tutorials
@giorgito33132 жыл бұрын
Excellent!!
@programmingelectronics2 жыл бұрын
Thanks so much! I hope it helped.
@fernandosimonetti65952 жыл бұрын
Excelente explicación! Muchas gracias!!!
@programmingelectronics2 жыл бұрын
Thank you!
@TOMTOM-nh3nl3 жыл бұрын
Thank You
@programmingelectronics3 жыл бұрын
Thanks for watching!
@DavidRisnik2 жыл бұрын
congratulations for the class, very explanatory. I'm doing a project using a panel of leds-dot matrix to print texts read by the SD card. How can I include the temperature value read by a sensor (DB1820), in the text sent by reading the SD card? thank you if you can clarify this doubt for me
@berndeckenfels Жыл бұрын
The function is called snprintf
@МаксимАтанов-ъ9т3 жыл бұрын
thanks. I dont found a good explanation of this function in Russia KZbin. Good reason to learn English)
@programmingelectronics3 жыл бұрын
Glad it was helpful!
@chrisspencer65023 жыл бұрын
I got caught out by the same thing in Matlab
@ingenierocristian3 жыл бұрын
Love it, thanks
@programmingelectronics3 жыл бұрын
Thank you!
@joshuapitong8992 жыл бұрын
Really great.✔️ Thanks.🙌
@programmingelectronics2 жыл бұрын
Thanks!
@markadyash2 жыл бұрын
thanks lot helpful
@programmingelectronics2 жыл бұрын
Thanks for the note! Glad it helped!
@dalwinderssi40942 жыл бұрын
Want know how to read from sd card and print on tft display using arduino uno
@programmingelectronics2 жыл бұрын
Thanks for the recommendation!
@bibel2k2 жыл бұрын
I'm speechless! One of the most annoying things on arduino is to print a long message like you have shown. Mind blowing!
@electronics54492 жыл бұрын
Awesome
@programmingelectronics2 жыл бұрын
Thanks for watching!
@yugalkishor31313 жыл бұрын
Superb
@programmingelectronics3 жыл бұрын
Thanks!
@jslonisch2 жыл бұрын
Isn’t it easier just to do it all in one line with Serial.println(String(variable1 + variable2 + ...)). No messing around with a buffer or worrying about if you got the right character specifier to suit your variable.
@programmingelectronics2 жыл бұрын
Yes, it is way easier using the built in String class... www.arduino.cc/reference/en/language/variables/data-types/stringobject/ There is a fun "controversy" to read up about on whether to use/avoid the String class with Arduino... forum.arduino.cc/t/optimizing-memory-without-strings/341158 I guess I landed on the not side of this but probably not for the right reasons though... A mentor programmer of mine told me to avoid them, so I just took his word for it! I should probably do some more of my own research :)
@berndeckenfels Жыл бұрын
This will however be slow as heck as it allocates the string buffer multiple times with the potential to fail at runtime, not good for embedded projects where you need speed and reliability. Much better with a static or stack allocated buffer.
@jason_man10 ай бұрын
just use String() and + this point
@Clip7heApex3 жыл бұрын
Would this work for LCD print?
@programmingelectronics3 жыл бұрын
I believe LCD print just takes in a string, so that should work. Best of luck!
@JPN763 жыл бұрын
Is it possible to use this to send multiple variable data to another arduino? If so how would you separate it on that side and plug the data into the correct variable on the recieving arduino?
@thunderbolt9973 жыл бұрын
super!
@programmingelectronics3 жыл бұрын
Thanks for watching!
@DodgyBrothersEngineering3 жыл бұрын
This video would have come in real handy just the other day, but better late than never... Would have been nice if you covered the use of the \ as in \"%s\".
@programmingelectronics3 жыл бұрын
Thanks for the note! Great points!
@lesatkins4210 ай бұрын
or you could use Serial.printf(" .... ", var1, var2, ... varn);
@12DGJB21 Жыл бұрын
Thank you, sprintf looks very useful. Would there be any benefit of using sprintf over the following single line command to print a simple string of text with a variable (i)? For example: Serial.println((String)"Relay"+(i+1)+" OFF"); Where the variable i is a numerical value starting at 0.
@yo90bosses Жыл бұрын
I know that comment is old but Ill go ahead and reply. Using the string class is never a good idea, in short it has issues due to dynamic memory on memory constrained systems like arduino unos etc. Go ahead and search for "Why not to use string in arduino". Many good videos.
@xroppa52902 жыл бұрын
Top
@ElIngLopez3 жыл бұрын
Great! ... I'd like to print the % sign, how i do this?
@mtraven232 жыл бұрын
Serial.print("%")
@berndeckenfels Жыл бұрын
Inside printf with "%%"
@clems69893 ай бұрын
The website will not send me the email needed for signup !!
@programmingelectronics3 ай бұрын
My aplogies for the issue! Sometimes the email goes to spam
@semibiotic Жыл бұрын
It should be snprintf().
@crissd8283 Жыл бұрын
This seems inefficient. While the first example may have more lines of code, the second code certainly uses more resources. Using a character array requires the same data to be stored twice. Once in the program and variables and a second time in the character array. Then you are spending time sending that data into that memory location then sending that new memory location to print(). The first example is certainly faster and more efficient. I understand that neat looking code is desired but the second example is harder to understand and less efficient. Seems like a bad idea.