using #define

what does the #define do? [I've looked at the Arduino reference page on it and it didn't help]
does it define a pin number?

#define motor1 9;
#define motor2 10;

It simply replaces the first string with the second string in the pre compiler. That is just before it compiles.

#define does textual substitution before the compiler scans the source. So if the source has:

#define three 3

int x = three;

After doing the pre-processing, the compiler will actually see:

int x = 3;

If you add arguments, they will be replaced during the expansion phase by:

#define foo(arg) ((arg) + 4)

int x = foo (5);

would become:

int x = ((5) + 4);

Note in your example:

yosler:

#define motor1 9;

#define motor2 10;

You should remove the semi-colon (';') in the define, since the compiler would add those when doing the expansion.

Another way of doing this is to use the const int declaration:

const int motor1 = 9;
const int motor2 = 10;

I tend to prefer using const int for constants over #define, since #define is just blind textual substitution.

sometimes it appears to be the #define is telling what pin number something is?
does it say that:

#define motor1 9
#define motor2 10
digitalWrite(motor1, HIGH);

means digitalWrite(pin 9, HIGH)?

That is exactly it.

#define motor1 9
digitalWrite(motor1, HIGH);

After preprocessing becomes:

digitalWrite(9, HIGH);

Which is then seen by the compiler. :slight_smile:

yosler:
sometimes it appears to be the #define is telling what pin number something is?
does it say that:

#define motor1 9

#define motor2 10
digitalWrite(motor1, HIGH);



means digitalWrite(pin 9, HIGH)?

Yes, but without the word "pin".

OP, your post has semicolons at the end of your #define.
Don't do this, or you'll be confused by the error messages you will inevitably get.

Thanx everyone! :slight_smile: XD :smiley:

TCWORLD:
That is exactly it.

#define motor1 9

digitalWrite(motor1, HIGH);




After preprocessing becomes:


digitalWrite(9, HIGH);




Which is then seen by the compiler. :)

Actually it becomes:

digitalWrite(9, 0x1);

HIGH is also a #define and will get substituted by the preprocessor before the compiler "sees" the code. :wink:
--- bill

bperrybap:
Actually it becomes:

digitalWrite(9, 0x1);

HIGH is also a #define and will get substituted by the preprocessor before the compiler "sees" the code. :wink:
--- bill

Indeed it does, I missed that, well spotted! :slight_smile:

Will the substitution(s) also be made inside any (#)included file?

only if the #define comes BEFORE the #include.

#define THISISSEENBYLIBRARY
#include "library.h"
#define THISISNOTSEENBYLIBRARY

Due to the way the IDE works, you will have trouble getting a library cpp file to see a define declared in the sketch, only the header sees it.

is there any benefit using #define rather than const int or vice versa? space usage? any differences?

A #define used to store a single constant value is pretty much equal to a global constant data type ( when reading the data anyway ).

A #define or macro can be used to make decisions on how to compile a file or generate entire blocks/classes/functions etc...

sdinnu:
is there any benefit using #define rather than const int or vice versa? space usage? any differences?

From the Arduino Cookbook:

"You will sometimes see #define used to define constants in older Arduino code, but const is a better choice than #define. This is because a const variable has a type, which enables the compiler to verify and report if the variable is being used in ways not appropriate for that type. The compiler will also respect C rules for the scope of a const variable. A #define value will affect all the code in the sketch, which may be more than you intended. Another benefit of const is that it uses familiar syntax - #define does not use the equals sign, and no semicolon is used at the end."