0

One thing that always confused me was why we use int or other commands to set the variables to have pin numbers as if those commands were always restricted to pin numbers only. For example

int ledPin = 13;

Does putting a random number in place of 13 that isn't a pin number cause an error? Right now I haven't checked yet.

I've only begun to realize that if you want to use variables in relation to pins or push buttons you have to set values first and then do associated functions like digitalRead. The commands like int are not like in other programming languages where you can set any values and do mathematical computations. I know it might seem obvious to someone who is used to Arduino, but this perspective is something that helped me grasp things better. Before that, I totally could not understand how things worked because of being confused with how int or other similar variables worked.

4
  • 1
    What is your question? Do you understand what the statement int ledPin = 13; does in C++? This actually does two things:1) It declares the variable ledPin as a variable of type "integer" and 2) It assigns the value 13 to that variable. It could also be written as two separate commands int ledPin; and ledPin = 13;. It has nothing to do with port numbers until you use the variable to refer to an IO port like with digitalRead(); or digitalWrite();.
    – StarCat
    Feb 24 at 14:07
  • what other languages do you know?
    – Juraj
    Feb 24 at 15:02
  • int is not a “command”, it's a data type. The whole line is a variable declaration: int ledPin = 13; means “let ledPin be an integer initially having the value 13”. Feb 24 at 15:31
  • Thank you for the comments. I'm just new to learning Arduino and I find it hard currently to grasp since the professor explaining does not do it in detail. That's why I didn't get how the int had completely nothing to do with the port numbers. Although off topic, I would want to ask for references that would help me grasp the correct terminologies and concepts since searching in Google generally did not help my particular understanding.
    – AndroidV11
    Feb 24 at 21:24
1

Note that this is just a C/C++ integer declaration, assigning a value to a variable, like any variable (irrelevant if it is for pin numbers).

You don't have to use a variable, there are similar variations:

Variable

int pinNumber = 13;

in begin:

pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, HIGH);

Const

Preferably I like to use a const:

const int PIN_NUMBER = 13;

Or even better, since it always is a low value:

const uint8_t PIN_NUMBER = 13;

And use it with capitals.

pinMode(PIN_NUMBER, OUTPUT);
digitalWrite(PIN_NUMBER, HIGH);

#define

#define PIN_NUMBER 13

pinMode(PIN_NUMBER, OUTPUT);
digitalWrite(PIN_NUMBER, HIGH);

The reason variables are used, is that when you want to use another pin number, you only need to change it in one place, making the sketch better maintainable/adaptable.

I don't know what happens when you give an illegal number (you can try it out yourself).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.