Pin Assignment

What is the proper way to assign a digital pin to be an input. I have seen a few examples (listed below) but I want to know which one is the best or is their a better one I don't know about?

const int buttonPin = 2;     // first example 
int buttonPin2 = 2;     // second example

void setup() {

  pinMode(buttonPin, INPUT);     
  pinMode(buttonPin2, INPUT);    
 
}

Thanks!

byte buttonPin3 = 2;

const is more logical I guess, you don't change the pin number.

The more efficient storage type pointed out by UKHeliBob is another improvement ( no board currently has more than 255 pins ).

There are numerous ways of representing this data, there was a thread not to long ago on the same subject ( and many others ) which searching may find.

Hi,

Actually, digital pins default to Inputs and you can use them as inputs without a pinMode statement. And that actually saves a little code that is not needed.

However it is good to make clear what pins are being used for.

Personally I like to use a template like this, on the ArduinoInfo.Info WIKI HERE: http://arduino-info.wikispaces.com/YourDuinoStarter_Blink

I suggest that the NAME of the pin should make clear it's an Input, like:

#define ButtonInput1 5
#define MotionSensorInput 8

But.. styles may vary!

OT:
@pYro_65 How did you do the cool SIG??

Thanks for the input! I think i will use "byte".

Also I did a search, however, basically all programs assign digital pins so I got so many results that after the first page I stopped looking.

I had a problem with #define in one program, I was using arrays within arrays kind of thing, had to change to const byte pinNumber instead.
I normally just use byte pinNumber, and rely on proper coding to make sure the pin number is not reassigned. Has never been a problem - just that one time where I tried to do the "right thing" and use #define and had the problem.

const int buttonPin = 2;     // first example

If you do it this way, any time you use buttonPin in your code, it gets replace with the constant 2.

int buttonPin2 = 2;     // second example

This actually defines an integer in your SRAM and copies a 2 into it during initialization. After that, every time you use buttonPin2 in your code, it pulls it out of SRAM.
If the value never changes, there's no really good reason to take up precious RAM space. And if the value does change, you could use a byte instead of an int.

I had a problem with #define in one program, I was using arrays within arrays kind of thing, had to change to const byte pinNumber instead.

I use one library (Morse Decode) that will only accept byte(parameter); for reasons I don't understand. But I'm digging more into writing libraries..

I have found that students usually immediately "get" #define where "const int" etc. is continually being re-explained. Hmmm...

Pins only go up to 255 and are not negative, so byte is better. Const is better again as that doesn't waste memory (the compiler turns it into a literal) plus you don't accidentally change it. So:

const  byte buttonPin = 2;

terryking228:
I have found that students usually immediately "get" #define where "const int" etc. is continually being re-explained. Hmmm...

Yes but they tend to do this:

#define buttonPin = 2;

That leads to obscure compiler errors.

Example:

#define buttonPin = 2;

void setup ()
  {
  pinMode (buttonPin, INPUT);
  }  // end of setup

void loop () { }

Errors:

sketch_jun06a.ino: In function 'void setup()':
sketch_jun06a:5: error: expected primary-expression before '=' token
sketch_jun06a:5: error: expected primary-expression before ',' token
sketch_jun06a:5: error: expected `;' before ')' token

Then they say "but there is no '=' sign on line 5"!

Hi Nick,

That leads to obscure compiler errors.

Sigh... Yes that's another potential problem...

I try to avoid that with real beginners by having them use a template like this:

I wonder if someone could write a PRE Pre-Processor that would catch "The Usual Rank Beginner Errors" plus "The Dumb Errors Terry makes too often"..

Could lint do anything for you?

From what I think I understand, LINT could be set up to find some of this stuff. I just have no personal experience with it (Except having some of my code NitPicked by it many years ago)..

The problem with lint (not that I've used it recently) is that it picks up so much stuff people usually don't bother with it.