#224 🛑 STOP using Serial.print in your Arduino code! THIS is better.

preview_player
Показать описание

Quick link to the PCBway 4th annual PCB design contest (Aug - Nov 2021)

00:00 Welcome Back!
00:46 PCBWay 4th Annual PCB Design Contest
03.27 Program bloat
04:11 Slow execution
07:05 Code Demo starts
10:55 How we can do it differently
18:25 Compilation with and without debugging
22:18 ESP32 advanced solution
24:33 printf for the Arduino
25:49 Conclusion

► You can now support me by buying me a coffee!

► Far more code examples and an advanced example in my GitHub

► Global compiler directives (with much more on this site)

► printf for the Arduino processor
I will expand on this in a future video - just for the Arduino processor!

► printf GitHub for the code...

... based on this printf GitHub version

Far more details of the code and an advanced example in my GitHub

► List of all my videos
(Special thanks to Michael Kurt Vogel for compiling this)

If you like this video please give it a thumbs up, share it and if you're not already subscribed please consider doing so and joining me on my Arduinite (and other μControllers) journey

My channel, GitHub, and blog are here:
------------------------------------------------------------------
------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор

As I decided to have an argument with a very sharp craft knife, my typing has slowed down considerably. It may take longer to respond to your comments. Sigh.

RalphBacon
Автор

STOP using "STOP this and that" clickbaits!!!

torquebiker
Автор

I am a mech engineering student and I am doing a 99% electrical project for my research dissertation. I spent weeks trying to figure out why my sketch has been printing data every 0, 033 seconds (30 Hz) instead of 1kHz+ as it should with timer interrupt. Now I know
I really appreciate the video Mr. Bacon. Highly informative and needed!

abdoolsattarcassim
Автор

I have to say that this "simple" method of deactivating debug statements has to be a winner for all the reasons you highlighted. I will be using it big time. thank you for such a bright shining light on a very simple way round the problem. Even simpler than having all the debug Serial.print statements inside IF debug on THEN.

PaddingtonbearNZ
Автор

Genius idea! I'm pretty picky about my code but every once in a while I forget to take out one of these statements. This is a far better idea. I can even use multiple debug variables to turn different sections on and off as needed. Thank you, sir and my you have a great and prosperous day.

edwardowen
Автор

Hi Ralph,
Just want to say that I enjoy watching your videos. The content is interesting and your presentation style is entertaining.
Thank you!

Conservator.
Автор

Interesting topic! But, imho you shouldn't blame the Serial.print statements for slowing down your program when you are not doing the house keeping after debugging by removing those print commands.

billferner
Автор

This was very long winded about a standard programming method.

bknesheim
Автор

Thanks for this great tip! I have a couple of further suggestions:
1. Use all caps when you define a macro so you can tell the difference between a function call and a macro because macros are expanded before compile, and sometimes this is important to know.
2. Surround your macro definition with '"{" and "}" so that multi-statement macros will be guaranteed to behave even if you don't know they're macros. As example, a multi-statement debug macro will compile fine and yet misbehave in code that looks like this:



for(i = 0; i < 100; i++)
debug("foo");

Jeff-kfom
Автор

#Define calls the pre-compiler and does a ‘find & replace’ before compiling the code.

As a computer science student, a loooong time ago (1985), I had to write a C pre-compiler as an assignment. It wasn’t very difficult but I’ll never forget the use of pre-compilers.

Conservator.
Автор

I consider myself a beginner and i have never considered this before. I was wondering how there can be almost 30 minutes of video on this topic. But im so glad i took the time to watch it. It was very eye opening to me and informative!

MakerMike-bxms
Автор

Thanks @Ralph for this video. It's one of many differences between a beginner person and more advanced /experienced person. Pre processor directives is amazing.

prlombaard
Автор

A word of warning:

If you define a symbol to be an expression, put parentheses around the expression to make sure that they evaluate the way that you wish.

Example:

#define ratio( x, y ) x / y

if you use ratio in the following, you will not get what you intended

float a = 5.0;
float b = 10.0;

float c = ratio( a, b ) ** 2;

The expected result would be 0.25 (= 0.5 **2 ).
UNFORTUNATELY, the compiler was presented with x / y ** 2
The operator precedence of C/C++ will perform the exponentiation first and the the division. This means that c will be 0.05 ( 5 / 100 )

If instead, you change the define to be

#define ratio( x, y ) ( x / y )

then the expression seen by the compiler is ( a / b ) ** 2 which is what you wanted.

WatchesTrainsAndRockets
Автор

Thanks for the information; a useful reference. I have got into the habit of using a DEBUG definition and then wrapping each of my debugging Serial.Print commands in an #if....#endif block, but this way you showed is a much more elegant method that I hadn't thought of before. I am by no means a beginner, but never ignore information like this aimed at beginners, as sometimes I find I've been set in my ways and they aren't necessarily the best!

MrJozza
Автор

In my personal library I made a header that replaces serial.print with spr or sprl, and I can enter up to 4 different parameters. It's compact, very easy to type, and much more clear to read. Also, with one line of code or even an input wire, I can disable all serial prints at once to speed up the program

nathanvandevyver
Автор

When I first saw this video being suggested to me, I thought this was an advisement against using Serial.print commands for passing information between microcontrollers and computers, so this was a bit of a surprise.

On the other hand, I guess this is down to personal code style preferences, as I'd much, /much/ rather NOT use custom-defined preprocessor "functions", as that severely limits what you can pass into them.

Given the fact that I've been using C and C++ for years, along with the preprocessor's very powerful replacement system, I'd rather use "#ifdef DEBUG" blocks around the code specifically handling debugging. Plus, this practice highlights your debugging code more strongly than yet another apparent function call that doesn't get coloured as such by the syntax highlighting.

DarylVanHumbeck
Автор

What is amazing to me is that it has taken you all this time to figure this out. ALWAYS remove debugging code from release versions. I use a #define and #if statements to turn on/off serial.print and other debugging code.

TanjoGalbi
Автор

#if DEBUG_FLAG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif

This code snippet adds a great value for this video thank you very much, can make your mic little clear so audio is perfectly audible

LakshmikanthAyyadevara
Автор

Yes you can't use printf but you can use sprintf() and it works great just create buffer and provide this string for example to Serial.print()

natanlisowski
Автор

Thank you so much Ralph, not just for this video but your channel in general. You've taught me so much, and you have a very pleasant way of teaching things. Keep up the good work :)

JewCheese