Reputation: 1
I need to set all LEDs to LOW except the one I want to be on HIGH. Currently, I have to send a specific request to all LEDs to ensure that the previous LED will be set on LOW. If i apply this logic to a lot of LEDs the code will be quite long.
Is there a way to say
Question
This code will switch on led.
if (readString.indexOf("?wall01_02") > 0) {
digitalWrite(led, HIGH);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
the next time I'm going to send a request to led1, but I also have to switch off the previous LED led.
if (readString.indexOf("?wall01_01") > 0) {
digitalWrite(led, LOW);
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
Upvotes: 0
Views: 193
Reputation: 111
I can't write a comment.
Why not an array of int, something like this:
#define ledArray 4
int ledPins[ledArray] = {1, 3, 7, 14};
if(readString.indexOf("?wall01_02") > 0) {
lightsOff(led);
}
if(readString.indexOf("?wall01_01") > 0) {
lightsOff(led1);
}
void lightsOff(int led) {
int i;
for(i = 0; i < ledArray; ++i) {
digitalWrite(ledPins[i], (ledPins[i] != led) ? LOW : HIGH);
}
}
Upvotes: 2