0
votes

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

  • set led on high but all others on low?

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);
1
What if you turned them all off first, then turned on the one you want?Kittsil
The one I want lights up, then I want to switch on the other lamp, but the one I switched on before stays on as long as I'm not changing it to low.Sebastian
I know. But you are asking, "How do I turn off all but one without making my code really long?" The answer is, "Turn them all off, then turn the one you want back on."Kittsil

1 Answers

2
votes

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);
    }
}