i 'm trying to write a program that triger my food dispensing rig every four hours.but for the love of god ,the programming just keep on giving me error at the the first few line at #define
error: 'voip' does not name a type
this is the program #define Rled 13 #define Gled 12 #define feeder 11
int mins = 0;
int hours = 0;
voip setup()
{
pinMODE (RLed,OUTPUT);
pinMODE (Gled,OUTPUT);
pinMODE (Feeder,OUTPUT);
If you specify the datatype, the compiler can do typechecking for you.
For me, the best argument agains using #define directive for defining variables is that there is no scope with a #define.
Say, for instance, that you have a #define called Rled but later in your project, you want to be able to get, say the current on led, and you want to store this as Rled, you cant. Because the preprocessing will replace Rled by a constant and you can not do 13 = 12;
If you include a header with a define, you can not use that name as an identifier. [identifier being name and parameters]
If you, however use a specified variable, you have implicated file scope (unless declared extern). Your compiler will warn you about multiple definitions of a certain identifier (function overloading is somewhat an exception of this).
Additionally you can pass consts by reference if you'd like. #define Rled 13
Rled has no adress.
In my field of programming (game programming) I often see this use of #define :
#define PLAYER (gameEngine->getPlayArea()->getPlayer())
Why not use Player* player = gameEngine->getPlayArea()->getPlayer(); instead?
That way you are guaranteed to have a valid identifier.
The compiler (often even the IDE) can tell you what type player is.
#define is a bad way to loose control of your variables and dataset.
/me could write more but feel as if I might be a bore
It is my understanding that variables defined as const are stored in RAM, while a literal like "13" does not occupy RAM.
That is not correct. Variables declared as const are as efficient as values that are #defined.
These two constructs produce the same code, the value of outputPin is not stored in RAM:
const int outputPin = 3;
pinMode(outputPin, OUTPUT);
or #define outputPin 3
pinMode(outputPin, OUTPUT);
The following (without the const) does store outputPin in two bytes of RAM . It also uses a couple of bytes of flash to for the instructions need to get the value from RAM to pass to the pinMode function
int outputPin = 3;
pinMode(outputPin, OUTPUT);
A few extra bytes usually don't make any difference so my advice is to use the construct that is most comfortable. But if you are concerned about every byte of RAM, the using const is better than #define because the compiler can check that the data types are correct. And its easer for human readers of your code to see what data types you intend.
Another good reason, besides type checking and scope checking, to use consts instead of #defines is semantic checking of the code by the compiler.
Consider this example:
const int x = 14;
int y = 15;
void setup () {
y = 22;
x = 22;
}
void loop () {}
The compiler knows that a const should be read-only and that you can't assign a value to it. Compiling this code will give an error:
In function 'void setup()':
error: assignment of read-only variable 'x'
This helps prevent logic errors in your code well before you ever upload and run it and then are trying to track down a weird behavior.
This example is trivial, of course, but imagine a far more complicated program. The purpose of adopting good programming practices is to protect you from yourself when things get complex!
The compiler also does the semantic check with the value substituted by #define, but the error message is much more cryptic.
If youcome across a message like:
error: lvalue required as left operand of assignment
the compiler is asying that you are trying to change the value of a constant.