#define
HTML-код
- Опубликовано: 2 окт 2024
- #define symbolic constants (Arduino Uno Programming for Beginners)
In this video I show you how you can use symbolic constants to make your code more readable and I will use functions to refactor the traffic light project.
You can find the code here:
github.com/pla...
Thank you for the video. 👍
#define GREEN 9
int GREEN = 9;
ARE NOT SIMILAR. "#define GREEN 9" defines a macro "GREEN" and "int GREEN = 9;" defines and initializes a global variable "GREEN" 🙃
you are absolutely correct, after rewatching my video, I have to admit that my words are very misleading.
Bad advice, don't do this. Use an enum instead.
enum Color {
GREEN = 9,
YELLOW = 10,
RED = 11
};
- It allows you to use the constant when evaluating expressions in a debugger
- It respects the usual scoping rules of the C language while #define doesn't (e. g. you can declare a local variable called GREEN inside a function - using a #define will break that)
- It leads to better error messages
Another alternative with similar benefits is to just declare these as `static const int GREEN = 9;` etc.
Thank you very much for sharing!
The "break;" that was inside the manipulateLED() function would not exit the switch block inside the loop() function.
I had a very bad feeling about the break inside the function 😂 thank you for the clarification
Most of the video is nice. But I am not sure about using define here. I think just using "const" is a better coding style in this case.
Thank you for the feedback! Can you tell us more about when you would use const and when you would use #define? I‘m always thankful to learn more.
@@playduino Well, I am not an Arduino coder. I am c++ coder. Maybe it is different here.
I always use define only, if i can't code it different (for example if i want to code different lines for different CPUs or operating systems). It helps reading the source if you have a lot of defines that are used for other stuff than just doing a const. Not sure if there are also other advantages, I must rethink about it.
In the arduino reference about "const" they also wrote in the notes/warnings section: "In general const is preferred over #define for defining constants."
So if you don't know why you are using #define instead of const, then const if the better variant.
fair point, thank you very much for sharing@@Volker-Dirr
#define should be used only when necessary, e. g. if you plan to later use #if or #ifdef with that definition.
@@playduino define just copies whatever you have defined to the areas where you used it, a variable will actually be stored in memory and then be passed to the function or whatever you are doing with it.
So if you define a pin for example and then use that lets say 3 times, the pin number will actually be copied 3 times in you code. If it's a variable it will only be there once.
#define is mostly used for example for debugging where if you use #if or #ifdef that code will not be compiled and will free up memory on the arduino if the condition doesn't meet up.