Initializing several WidgetLeds at once

Hi,

I’ve an 8 relay board and I’d like to read the status of the chip’s pins to know whether the relays have received or not the signal. For that I’m using the WidgetLed and, because there’re 8 of them, I thought initialize them in a loop but I just cannot get it right. Can it be actually done?

This’ the error I get: “variable-sized object ‘ledR’ may not be initialized”

    //......
    const int ledR[] = {1, 2, 3, 4, 5, 6, 7, 8};  //relay number
    const int V[] = {12, 13, 14, 15, 16, 17, 18, 19}; //virtual pins
    //WidgetLED ledR1(V12);
    //WidgetLED ledR2(V13);
    //WidgetLED ledR3(V14);
    //WidgetLED ledR4(V15);
    //WidgetLED ledR5(V16);
    //WidgetLED ledR6(V17);
    //WidgetLED ledR7(V18);
    //WidgetLED ledR8(V19);

    void initLedWidgets() {
      //init led widgets
      for (int i = 0; i < 8; i++) {
        WidgetLED ledR[i](V[i]);
      }
    }

    void setup(){
//......
    initLedWidgets();

    //get relays status to turn leds on/off
      timer.setInterval(2000L, leds);
//......
    }

    void leds() {
      for (int x = 0; x < 8; x++) {
        bool pinState = (digitalRead(R[x]) == LOW);  //EDIT: R = relay pin number
        //if signal is 0, turn off LEDx
        if (pinState) {
          ledR[x].off();
        }
        //if signal is 1, turn on LEDx
        else {
          ledR[x].on();
        }
      }
    }

BTW, you can just use Blynk.virtualWrite(vPin, 0); for OFF and Blynk.virtualWrite(vPin, 255); for ON (and inbetween for varying intensity).

That way you don’t need the WidgetLED… part at all

Thanks, I tried this code as a test to turn ON all the WidgetLeds but, no luck.

void leds() {
  for (int x = 0; x < 8; x++) {
    //    if (!digitalRead(R[x])) Blynk.virtualWrite(V[x], 0);
    //    else Blynk.virtualWrite(V[x], 1);
    Blynk.virtualWrite(V[x], 1);
  }
}

but this messy one did work:

bool pinState[8];
void leds() {

  //read relays pinout
  for (int x = 0; x < 8; x++) {
    {
      pinState[x] = (digitalRead(R[x]) == LOW);
    }

    //if signal is 0, turn off LEDx
    if (pinState[0]) ledR1.off();
    else ledR1.on();

    if (pinState[1]) ledR2.off();
    else ledR2.on();

    if (pinState[2]) ledR3.off();
    else ledR3.on();

    if (pinState[3]) ledR4.off();
    else ledR4.on();

    if (pinState[4]) ledR5.off();
    else ledR5.on();

    if (pinState[5]) ledR6.off();
    else ledR6.on();

    if (pinState[6]) ledR7.off();
    else ledR7.on();

    if (pinState[7]) ledR8.off();
    else ledR8.on();
  }
}

1 just is a small step up from OFF… you need 255 for full ON :stuck_out_tongue_winking_eye:

Thanks @Gunner, it worked!! It just didn’t sink in me @ midnight my time, that the actual WidgetLED’s LED in the app can be dimmed :rofl: