Why use a global variable for pin definition?

  • Guys i am complete beginners so please don't upset from me please and happy new year my question why use
  • int pin=13
  • pinMode(pin,OUTPUT)
  • I mean I can do
  • pinMode (13,OUTPUT)
  • Why should I use global variable :o

Please don't hijack threads @123q. I have moved your post to a dedicated thread, in the correct forum board, with a suitable subject.

Repeated hijacking may result in a suspension of your forum account.

Thanks in advance for your cooperation.

“ I mean I can do
pinMode (13,OUTPUT)”

Yes you can.

Names are used so they can help document your code.

const byte heartBeatLED = 13;

Also, let’s say you have referenced pin 13 in ‘fifteen’ lines in your sketch.
Then let’s say you assigned pin 13 to the name raiseRelay.
const byte raiseRelay = 13;
Now let’s say you wanted the raise relay hardware to really be on pin 4.
All you would have to do is change ‘one’ line of code, not ‘fifteen’.
const byte raiseRelay = 4;

Why should I use global variable

Using a variable for the pin number is useful in a number of ways. For instance, suppose that you decide to use a different pin number for the LED at a later stage in your project ? Having it as a variable allows you to change the pin number in one place in the code.

A second, and very important reason is that the variable gives the pin a name, which you should ensure is sensible. Think how much easier it is to refer to redLed, greenLed and blueLed rather than having to remember pin numbers. If you declare the variable as const then a useful side effect is that if you inadvertently try to change its value in the program then the compiler will tell you so and stop it happening.

As to why the variable is global, by the nature of the use that it is put to then it is likely to be used throughout the program. There is no reason why it could not be a local variable but you would need to pass its value to any functions that use it.

If I use pin mode in every lines let say in all ten lines i have to change all ten lines and if I use int then I will change only one line am I right

In most programs you only set pinMode once. But you read or write to that pin many times.

And more selfish reason for naming pins...if/when you need to ask for help you're a lot more likely to get that help if your code has meaningful names for the pins than if it is just scattered with magic numbers everywhere. If you say things like "The relay isn't switching when it should" but the code has no reference to a relay so we have to guess which pin number relates to the relay many of us will simply lose interest.


Something else to note, when declaring a variable for a pin number, is that the compiler will often never actually create the variable, but will instead replace the variable with its value whereever it is used. So you are not actually using any memory for the variable.

Using a name for a pin also helps you avoid shooting yourself in the foot. Let's say you've chosen pin 26 for something and you have fifteen places in your code where you have used 26 to refer to it.

Then you decide to move it to pin 32. Your natural inclination will be to do find and replace and if you are foolish, do replace all.

That's great until you recall that you were using 26 for something else (iteration through an array perhaps) and now you have a bug that would never have occurred if you had used a named constant for your pin number.