Let's Make Robots!

Difference between #Define and INT

I have seen both used in some Arduino tutorials and I was curious as to the difference.  From what I've read #Define ledPin 13 and int LedPin = 13; do the same thing.  What dont I know?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I *THINK* that


is defining a constant.



is initialising a variable.

Difference being, I *THINK*, that you cannot change the constant later in the code. But the variable will remain, uhh, variable.



But last time I coded C, I had a total brainfart which resulted in a wifi traffic light. And I ended up frantically avoiding string variables because they are just to darn hard for my perlish mind.

For shame!

I do believe you are correct, but I haven't used C since 2003.

#define will takes up no program space and is used as a constant,

as an example that you gave 

#define ledPin 3
"The compiler will replace any mention of ledPin with the value 3 at compile time." quoted from the arduino library
 hope this helps a little bit more

The lines starting with a #, like #define and #include are handled by the preprocessor step which takes place before the compilation of the program. The result is, that the compiler will see the sourcecode with all sourcefiles from #include lines inserted and all occourences of "ledPin" replaced by "3". You can also do more advanced things like:

#define absolute_value( x ) ( ((x) < 0) ? -(x) : (x) )

Which could be handled with a function, but this method is a bit faster, since you skip a function call, and works for all datatypes (where < 0 and > 0 makes sense)
For a general description look at http://en.wikipedia.org/wiki/C_preprocessor

Actually using #define DOES usually take up *program* space (part of the ~14 kb flash memory available on the Arduino), but what it saves is RAM (of which you only have about 1kb).

Using 'int' stores that value as a variable in RAM, and as I discovered on my arm robot, when you use up all the RAM you don't get error messages, the robot just starts behaving really weird (I guess because it loops around and starts writing to the RAM at the beginning again, overwriting who knows what).

Using #define avoids putting anything in RAM. Instead, all that the #define statement does (as jka explained above) is it tells the Arduino compiler to substitute that text ('3', in your example) everywhere that it sees you write 'ledPin' in your code, BEFORE it compiles the code and sends it to the Arduino. So actually, you can enter as many #define's as you want, and that itself won't use any additional code space, because all you've done is given the compiler some instructions. But once you actually USE 'ledPin', then it'll insert the '3' instead, and that gets compiled and stored in that 14kb of flash memory.

In my arm robot, when I ran out of RAM trying to store 'poses' (combinations of six numbers indicating the position of all six joints), I switched my code to use #define's instead. So I went from something like this:

int grab_block_pose[6] = [90, 45, 75, 49, 152, 111];
int lift_block_pose[6] = [85, 50, 78, 92, 116, 115];

To something like this:

#define GRAB_BLOCK_POSE  90, 45, 75, 49, 152, 111
#define LIFT_BLOCK_POSE  85, 50, 78, 92, 116, 115

Which was the same as hardcoding those numbers everywhere in the code that I wanted to use them, which added to the program size. Eventually I ran out of that too, so now I'm looking into using EEPROM chips to store the numbers externally and load them as I need them, so in order to do that I'm going to have to rewrite all my functions BACK to how they were originally, to use arrays of integer variables rather than constants. Ah well :)


thanks for the much better explanation :)
Thank you all for clearing that up for me....