Hello, I just began messing around with the Photon Particle and decided that I wanted to flash 3 LED’s in sequence. D3, D5, & D7. My code is the following. When I try to turn on the LED’s individually they turn on fine but when I try the following code, it doesn’t work. Also the only LED to turn on is D3, which turns on when it cycles through the loop. In the serial monitor it is outputting the values I expect it to output. Which is 3, 5 & 7. Please let me know what could be going on, thank you.
int led1[] = {D3,D5,D7};
int i = 0;
void setup() {
pinMode(led1[i], OUTPUT);
}
void loop() {
int i = 0;
while(i < 3) {
digitalWrite(led1[i], HIGH);
Serial.println(led1[i]);
delay(1000);
digitalWrite(led1[i], LOW);
delay(1000);
i++;
}
}
Oh snap you’re so right
The reason I used a loop was to make the code shorter.
Thank you very much. I’m going to try it once I get home and put an update!
I'd tend to argue against the very in "a very good reason" although that's interpretation.
A good enough reason for going the array route for me would be: As soon the tasks for more than just a couple of pins are similar enough to unify them into loops and functions - go for arrays preferably in combination with enums for readable indexes.
With just three LEDs and such a short sketch the advantage of having an array is rather limited, but adopting that style from simple projects on will keep your code more maintainable and scalable IMO.
With that approach - executed correctly - adding a few more LEDs will only be a matter of adding the extra pins to the array and you're done - no re-visit to the active code required.
I would be concerned that using highly flexible code like this is easier to abuse by the programmer and will more quickly go awry during a crash. Unlike most of the rest of code, GPIO and physical interface code is more likely to cause hardware damage if it’s used incorrectly.