#define vs const

i have used #define in this old code:

#define SAMPLE_PERIOD  (double)0.0004 // 0.4ms sample period



// DEBUG_SERIAL statements will only print if DEBUG is nonzero
#define DEBUG 0
#define DEBUG_SERIAL if(DEBUG) Serial

#define DEBUG_PIN 13

// Kinematics
#define HANDLE_LENGTH (double)0.07 //70.00 mm
#define SECTOR_RADIUS (double)0.0757 //calipers
#define MOTOR_RADIUS (double)0.00837 //m - calipers

// Select simulation
#define BUMP_MIN           (char)'A'
#define BUMP_MED           (char)'B'
#define BUMP_MAX           (char)'C'

//defaults to
#define ENV_TYPE           BUMP_MED

I can replace it like this ?

const float SAMPLE_PERIOD=0.0004 // 0.4ms sample period  now also with float



// DEBUG_SERIAL statements will only print if DEBUG is nonzero
const int DEBUG = 0
const int DEBUG_SERIAL if(DEBUG) Serial  //veeery unsure about this

const DEBUG_PIN =13

// Kinematics
const double HANDLE_LENGTH =0.07 //70.00 mm
const double SECTOR_RADIUS =0.0757 //calipers
const double MOTOR_RADIUS =0.00837 //m - calipers

// Select simulation
const char BUMP_MIN = 'A'
const char BUMP_MED ='B'
const char BUMP_MAX  ='C'

//defaults to
const char  ENV_TYPE = BUMP_MED

and why use const instead of define?

Yes, you can use "const float", but double and float are not the same.

For AVR-based Arduinos, double and float are treated as equivalent, 32 bits in each case.

#define is a text preprocessor command, and does not reserve any space for or define a variable.

p3l4h0:
and why use const instead of define?

Some background reading here;

Arduino and why use const instead of define

Moderator edit: spelling corrected (hint: it's on every page on this site)

Yes. (but)
Const [variable] is preferred over #define [variable] because const tells the compiler that the variable will not change and it gets put in memory set aside by the compiler as "read only". Very helpful if your program is running low on RAM for variables.

#define is simply a precompiler instruction to replace stuff. So

#define abc "This is a long string that I don't want to type over and over"

means that the precompiler replaces all occurrences of "abc" with "This is a long string that I don't want to type over and over". Taking 65 bytes of RAM. Fortunately the Arduino compiler is smart enough to only make one copy of the string in RAM.

Now, the but...
#define is a precompiler instruction
const is a compile instruction and must be terminated with a semicolon.

Not every #define can become a const.
// DEBUG_SERIAL statements will only print if DEBUG is nonzero

#define DEBUG 0
#define DEBUG_SERIAL if(DEBUG) Serial

This is a macro, not a variable.

But you can just replace if with something like:

const bool DEBUG = false;

void debugPrint(char * t){
  if(DEBUG){
    Serial.print(t);
  }
}

And let the optimizer take care of it.

Actually, you already kind of use that with

#define DEBUG_SERIAL if(DEBUG) Serial

because that if() is also not a pre-processor statement.

And might even be easier to make a DebugPrint class and a NoDebugClass which have the same interface as Print and of which the last simply does nothing :smiley: (project idea....)

SteveMann:
Yes. (but)
Const [variable] is preferred over #define [variable] because const tells the compiler that the variable will not change and it gets put in memory set aside by the compiler as "read only". Very helpful if your program is running low on RAM for variables.

The compiler is clever enough to optimize away const variables. They basically get hard-coded into the program, just like the rest of your code. This makes

const int variable=13;
Serial.println(variable);

take up the same amount of space as (RAM and PROGMEM)

Serial.println((int)13);