I have a xmas light controller I made. It originally flipped a set of 8 relays on and off to make light patterns. Like so:
void setup() {
for(int i = 1; i <= 8; i++) {
pinMode(i, OUTPUT);
}
}
void loop()
{
// wave up
for(int x = 1; x<=4; x++) {
for(int i = 1; i <= 8; i++) {
digitalWrite(i, HIGH);
delay(500);
}
for(int i = 0; i <= 8; i++) {
digitalWrite(i, LOW);
delay(500);
}
delay(2000);
}
etc, more patterns
Problem is relays 1 and 7 have burned out. I could go in and rewire the thing. I could just run it as is and not use 1 and 7.
What I want to know is if there is some sort of alias I can assign in my set up to just use pins 2, 3, 4, 5, 6, and 8 without having to drastically rewrite everything. All the patterns would then run as x<= 3 and i < 6 is my idea.
In the setup can I do something like
pin1= 2
pinMode(pin1, OUTPUT)
and then in the loops do something like
digitalWrite("pin" + i, OUTPUT)
??
and then in the loops do something like
digitalWrite("pin" + i, OUTPUT)
??
Not like that. Names do not exist in the compiled code, so you can't set the mode based on the name.
Using an array of pin numbers, and iterating in the loops over the pins in the array, based on the sizeof the array means that when you replace the burned out relay (how DID that happen?), you just add another pin number to the array, and no other changes are needed.