Difference between INT and DEFINE

Please see my sample code on the motion sensor/ball switch. I used this code below
If I use #DEFINE for setting the pins then I get the same result. I would like to understand better the difference between the functions and how to know when to use each. I am new to Arduino and coding and I though using INT just defined the type of variable you are inputting.

// Pin Variables
int ledPin = 8;
int buzzerPin = 9;
int tiltPin = 10;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(tiltPin, INPUT_PULLUP);

  digitalWrite(ledPin, LOW);
  digitalWrite(buzzerPin, LOW);
}

void loop() 
{
 if(digitalRead(tiltPin) == HIGH) 
 {
   digitalWrite(ledPin, HIGH);
   digitalWrite(buzzerPin, HIGH);
 }

}

It's quite subtle
A #define is a macro substitution.

#define ledPin 8

Every time the compiler's preprocessor sees the string "ledPin" in the source, it is replaced by the string "8". The code is then compiled.
The value of ledPin cannot be changed when the code runs.

If you use a variable

int ledPin = 8;

then you could simply assign a new value to the variable "ledPin" while the code is running to change what ledPin refers to.

(It's a lot more complicated, and #define in C++ is considered an unwelcome legacy of C)

So if I understand you correctly when I use the function #DEFINE it will set that PIN forever in the loop code whereas using "INT" would give me the ability to change the value for differing purposes? Meaning INT would give you greater flexibility and allow for writing much more complex code?

Uh, from my undestanding it does not replace with the string 8, it is replacing it with the number 8. For a replacement with a string
#define ledPin "8"
would be the define.

And to ensure
int ledPin = 8;
is not change afterwards, add the const qualifier:
const int ledPin = 8;

If you change the int, it is only changed in the code which is executed after the change.

Lets assume you have a pin
int pin = 8;
which you set to
pinMode(pin, INPUT);
in your setup.
And you read it in your loop using
int result = digitalRead(pin);
. If you then change the int in the loop, only the subsequent calls of digitalRead() are affected, but not the setup set of the pin mode. Means, this only leads to problems and would not help. There is no reason to change a pin define in running state.

Normally it is always a good idea to add the const qualifier to variables you do not want to be changed while running, as your pin.
const int pin = 8;
If you now change the int while running (f.e. in your loop by calling
pin = 9;
, it throws an error when compiling mentioning it.
Or you keep using #define, which is also fine.

For me as someone who is using C++ for ~10 years its understandable (and an interesting addition to my knowledge, thanks for that^^).

For someone who says he is new to coding I think at least your first answer was a bit misleading tbh. If he defines an 8, he will have a numeric value inserted in the end, while "8" will result in a char array. What a precompiler does under the hood is normally far away for such a person - come on, he asks how to use an interger properly^^.

With your second post which clarifies why in some for a beginner very rare cases it makes a difference it is not more that confusing. Especially adding the example in which cases this knowledge is of help seems very useful for me. But at least without that detailled description I think this is adding more confusion than clarification :wink:

Don´t get me wrong, it is awesome that you are helping the persons here, thanks for that!
Its just the feedback from someone who also thought about getting a bit into detail about int and memory usage and that a (u)int8_t could save some space and so on, that using auto can be an advantage or define in {} prevents narrowing conversion and so on - but for a beginner I think adding const to make the code more robust is easy to understand plus most helpful at that state, and everything else will come later on, when he is used to the "basic" data types and more fluent in coding.

Just my two cents :slight_smile:

No, the compiler sees

int x = = 2 + 2;

:wink:

(You need to lose the "=" in the #define)