int ledPin0=0;
int ledPin1=1;
int ledPin2=2;
int ledPin3=3;
int ledPin4=4;
int ledPin5=5;
int ledPin6=6;
int ledPin7=7;
int ledPin8=8;
int ledPin9=9;
int ledPin10=10;
int ledPin11=11;
int ledPin12=12;
int ledPin13=13;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
with some thing like
byte ledPin[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13};
for (i=0; i<14; i=i+1)
(ledPin(i)) = [i];
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
}
I don't know if a byte array works though, or if you need an int array.
Thing is: the int ledPin0=0; int ledPin1=1M ... aren't needed. They are just an easy and human readable way to store the pin values where the leds are connected. You can easily store them in an array and acces the one you need via arrayname[position].
If at this stage you just want to set up an array with the pin numbers in it, then the first statement in your new code will do it for you. It creates an array of 14 bytes (indexed from 0 to 13) and initialises each byte to the values in the { }, which I think is what you are after.
I hadn't got as far as assigning them as inputs or outputs.
I was still assigning a variable to each digital pin.
If you put this line into a basic sketch it fails, I assume, because the variable has not yet been assigned? i.e. 'ledPin 13 = 13;'
The thing is you can't "dynamically" create variables like led1 led2... by writing led(x) or something. So you are right, you need to initialize them first. But you don't need the single variables any more, cause now you have the array (which is = led1 + led2 ... in a sense).
int ledPin0=0;
int ledPin1=1;
int ledPin2=2;
int ledPin3=3;
int ledPin4=4;
int ledPin5=5;
int ledPin6=6;
int ledPin7=7;
int ledPin8=8;
int ledPin9=9;
int ledPin10=10;
int ledPin11=11;
int ledPin12=12;
int ledPin13=13;
To recap, when you create the ledPin array, each element is going to hold the actual pin number for the nth pin.
There are two ways in C / C++ to do this (at least). Write the pin number into ledPin[ ] inside a for loop, or use the { } initialisation when you declare the ledPin array. The latter is neater and quicker, I think.
When you want to access the actual pin number for doing things like pinMode, you refer to ledPin[X], where X is a literal number in your program (e.g.) ...
Perhaps we should have asked why you wanted to have the pin numbers in variables in the first place? What is the rest of your program going to do? What stops you from just using the pin numbers in your code, like ...
digitalWrite[3, HIGH];
I'm not suggesting you have to, or should, do it this way, just asking to understand what prompted your original question. Depending on that, there may be different approaches which make your code easier to write and understand.
as a newby I am keeping to the format of being able to alter the pin assignments at the start of the program.
I have had instances in the past where I have altered to pin numbers to help with bug chasing, to see if the fault followed the pin or the variable.
I intent, eventually writing a long programme to control an indicator board for a garden railway that I am building.
The programme is liable to be long so I am investigating ways of keeping the program as short as possible.
There is another approach which will help with changing pin assignments, and should make the code easier to read.
You can define each pin to have a pin name constant at the start of your program. Two ways of doing it ...
#define CLOCK_PIN 12
or
const byte CLOCK_PIN = 12;
Note the different punctuation in each method! Each of these will then let you use the name "CLOCK_PIN" in your code. E.g.
digitalWrite(CLOCK_PIN, HIGH);
There is debate over which method is better - if you search on the Arduino website for "const" and "#define" you can find out more.
Will there be places in your code where you would ideally use a for loop to do the same thing to a series of pins? If so, there is a more complicated alternative to these two which gives the user-friendly name and the ability to loop through them. I can post details if it helps.
nothing I suppose, it just seems odd not to assign the pins with a variable. I suppose this would be even shorter than assigning them with a for loop ^_^.
Generally, you assign a pin number a name for two reasons: first to call it something that reflects its function and second, as you observe, to make it simple to alter pins with a change in one place.
Ledpin1 doesn't really work for the first reason. If you're going to be able to work with your pins in a loop, better to set them up in an array, as shown. If, as seems more likely in this case, each indicator has a specific function, you may be better off giving them individual names that reflect that.
is it possible to replace ledPin assigning with a for statement?
Yes, but you need to change your original code:
// A "typeless" way to find out number of elements in array X
#define ElementsInArray(x) (sizeof(x) / sizeof(x[0]))
byte ledPin[14]; // give me 14, 0-13
void setup() {
// put your setup code here, to run once:
int i;
for (i = 0; i < ElementsInArray(ledPin); i++) {
ledPin[i] = i;
}
} // End setup()
void loop() {
// put your main code here, to run repeatedly:
}
The ElementsInArray() looks like a function, but is actually what's called a preprocessor macro. This gives you a "typeless" way to find out how many elements have been allocated to an array, regardless of its type. That is, it works with everything from a byte array to a float array. Note that you cannot have executable statements outside of a function block. Since your version had a for statement outside of either setup() or loop(), it draws an error message from the compiler.
There is a major difference between a #define and using const:
#define MYNUMBER 1
const int MYNUMBER = 1;
A #define is simply a textual replacement, so wherever your source code has MYNUMBER, the compiler replaces it with 1. Because it is a simple textual replacement, it does not appear in the symbol table as a variable. Therefore, no type checking is performed on the substitution. However, using the const variation also replaces every occurrence of MYNUMBERwith 1, but because it is a int variable, the compiler does add it to the symbol table and, hence, can perform type-checking on it. If the Arduino had a symbolic debugger in its IDE, you would only be able to debug statements using the const variation as the #define would not be allocated space in the symbolic debugger. Since that is not the case at the moment, use #define when you want typeless data transforms and const when you want the compiler to do type checking on the expression.