Different script to do same thing. Why?

Hi there everyone. I'm new on here, and new to Arduino. I'm working my way through basic sketching and trying not to ask loads of questions and figure it out for myself (the spirit of Arduino, no?)

But, I do have a question today.
These two following bits of code do the same thing, yet they use different syntax.
Can anyone explain the difference between: '#define' and 'int', when it comes to setting a pin as an input/output?
I understand that int is a variable, but I don't understand how that is used to set a pins mode. And why do they both work?

Many thanks in advance.
Sim

Sketch One:

// Project 1 - LED Flasher

int ledPin = 10;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}

Sketch Two:

// Example 01 : Blinking LED

#define LED 13 // LED connected to
// digital pin 13

void setup()
{
pinMode(LED, OUTPUT); // sets the digital
// pin as output
}

void loop()
{
digitalWrite(LED, HIGH); // turns the LED on
delay(1000); // waits for a second
digitalWrite(LED, LOW); // turns the LED off
delay(1000); // waits for a second
}

The difference is that using #define, the pin number is stored in flash memory along with the sketch. Using the int method, its a variable that is stored in RAM. For a small sketch its not important, but if you start writing large sketches that use a lot of RAM, saving a bit of memory might mean the difference between success and failure.

Thanks for the reply Pluggy. One more thing. When does Arduino know that the variable ledPin is actually a hardware connection and not just a number. Is it here?

int ledPin = 10

or here?

pinMode(ledPin, OUTPUT);

It is always just a number - it isn't even a number the AVR processor would recognise as a pin number.
software - it's all smoke and mirrors.

Thanks AWOL.
I'm mot totally clear on this, but I figure it's not super important.
I've learnt to speak and write French to a high level, so I know I can do this as well.
:slight_smile:

Donc, c'est bien facile!
Je vais traduire.

Seriously, the number is just that.
When you pass the number to "pinMode", it is the "pinMode" function that knows that the first number it is given is to be interpreted as a hardware pin number, but it isn't the number that the processor knows that resource by.
It knows that the second number it is given represents something that should be done to that hardware pin.
Internally "pinMode" translates that number into a description of a hardware resource that the processor can understand.

Bon, je comprends. J'avais pensée que c'était pinMode qui fait ça.
Merci encore!
s