What is the best practice when using variables to store pin names, to make it easier to change pins in future versions of the program"?
In the DigtalRead example, like all of the examples I have seen, pins are always assigned to an integer variable. So, rather than saying A1, it simply is stored as 1. But, in this case, the 1 is interpreted as an analog pin #, only when it is used in an analog related command.
I recall seeing one posting, indicating that using the "A1" name was better. I have just spent an hour trying to find it again, but have not succeeded.
So, I though it might be best to assign "A1" to a string variable, to make it clear this is an analog pin. But, none of the examples I see on the Arduino web page use that method. Here is an example from the "DigitalRead" page;
int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
So, should I take this as a hint that this is the best practice?
C/C++ is very flexible. It can differ from case to case.
You could try the #define command for this need.
#define MY_LED_PIN 13
digitalRead(MY_LED_PIN);
Definition is at one place, no problem to change. No overhead in program. Compiler cares about it. It replaces the constant value to the statement exactly like digitalRead(13);.
I recall seeing one posting, indicating that using the "A1" name was better. I have just spent an hour trying to find it again, but have not succeeded.
The analogue input pins, marked on the board as A0 to A5 on a Uno can be used as either analogue inputs or digital inputs/outputs. Personally I always use the Ax names, which are predefined for you, in both cases to make it clear which pins are being used. Note that the Ax pins can also be referred to by number (14, 15, 16 etc) but that just adds confusion as they are not labelled as such on the board.
analogRead(0);
analogRead(A0);
analogRead(14);
Are all equivalent on a Uno
Naming of pins is a a different thing. Using variables for pin names or #defines is a matter for debate but either way the names should be easily understood and relate to the function that the pin is being used for.
So
const byte potPin = A0; //value will never change so make it const
analogRead(potPin);
would be equivalent to the 3 statements above but would be easier to understand when reading the program.
Why did I post the link to a Google search? So people who are too lazy to search on their own can easily find detailed material explaining why const is a better choice than #define.
And you don't need to use a 16 bit "int" to hold a pin number, an 8 bit "byte" will hold a number up to 255, saves a byte of precious SRAM, I like to put "Pin" in pin names to remind me what they do.
const byte potPin = A0,
tempPin = A1,
button1Pin = 2,
relay2Pin = 8;
// and so forth
No, pins are always integers on Arduino. The reason is that there are more numbers than there are pins. There are some special constants such as NO_PIN that are defined in the range of integers that can't be represented as a byte. Every time you call digitalRead() your byte will be upgraded to an int.
90% of the time, const int will be inlined by the compiler so that it doesn't use any SRAM storage. Using byte saves no storage and slows down the program execution.
Some Arduinos have other labels on the board. Things like MOSI and SCL. Those constants are defined in the pins_arduino.h file so they will work wherever that pin is named on every Arduino.
By definition that must be true as you can't have fractional pin numbers. Furthermore, it seems obvious that they cannot be negative either. However, any value that fits in a byte is also an integer, although there may be a mathematical argument that zero is not actually a number.
90% of the time, const int will be inlined by the compiler so that it doesn't use any SRAM storage. Using byte saves no storage and slows down the program execution.
OK, I should have said "pins are always ints on Arduino." Not mathematical integers but 2-byte or 4-byte signed ints (as defined by the specific Arduino core for your specific chip.)
MorganS:
OK, I should have said "pins are always ints on Arduino." Not mathematical integers but 2-byte or 4-byte signed ints (as defined by the specific Arduino core for your specific chip.)
But if you move your code to many of the other Arduino models they aren't equivalent. The value of A0 is different on different boards. That is one reason to use A0 instead of 14.
Using the names will also protect you from using nonexistent analog pins if, for example, you move your code from a Nano (eight analog inputs) to an UNO (six analog inputs). If you wrote 6 or 20 when you meant A6 the code would compile but not work. Using A6 on an UNO causes the compiler to tell you that A6 is not defined.
Using the name also allow you to mix analogRead() with digitalRead() on your analog pins. You can use:
I tend to use 'const int' instead of 'const byte' but I should probably switch.
I try to use ALL_CAPS (indicating a constant) and ending in _PIN for names, but at a minimum InitialCap (indicating a global) and ending in Pin.
I always use the names of the analog input pins: A0, A1, A2... If you use numbers for both analog inputs and digital I/O pins it is very confusing to see the "same pin" be given two different names when it is not clear that the intent is to always use one name for the digital pin and the other name for the analog pin.
If more than a few pins are used I like to declare the names in pin number order. It makes it much easier to detect a pin conflict if the declarations are not spread around. For special purpose pins (Serial, SPI, etc) it is handy to add comments indicating what devices are using those pins. A common conflict is someone connecting a switch to Pin 11 or Pin 12 on an UNO when they are using an Ethernet or WiFi shield that uses pins 10, 11, 12, and 13 for SPI.
Oh! So you you come out with some sweeping opinion and its up to the reader to do the research to prove your right? Really? Thanks for showing us where land of google lies.
Not even close. For at least two decades many others have done a wonderful job writing about the subject. Me paraphrasing here is both an injustice and a waste of time.
I was only passing on things I've learned here on the forum, @johnwasser is correct so that makes me wonder why so many noobs have been scolded for using "int" instead of "byte" for pin numbers.