neroroxxx / RoxMux

Collection of Multiplexer Controllers for some commonly used multiplexer chips.
Other
15 stars 5 forks source link

How to setup multiple leds #7

Closed mavoIn closed 10 months ago

mavoIn commented 10 months ago

Hi, i guess its a newbie alike question. Your examples only explain setting up one led. Unfortunately i am not able to setup multiple leds.

const byte signal01Pins[] = { 11, 10, 9, 8 };  // Pins
RoxLed signalLeds[sizeof(signal01Pins)];

void setup() {
  // setup leds for switches
  for (byte weichenled = 0; weichenled < sizeof(weiche01LedPins); weichenled++) {
    weicheLeds[weichenled].begin(weiche01LedPins[weichenled]);
    weicheLeds[weichenled].setMode(ROX_DEFAULT);
  }
  weicheLeds[0].on();
}

Thank you very much for your advice.

neroroxxx commented 10 months ago

the variables are all mixed up in your code

const byte signal01Pins[] = { 11, 10, 9, 8 };  // Pins
RoxLed signalLeds[sizeof(signal01Pins)];

void setup() {
  // setup leds for switches
  for (byte i = 0; i < sizeof(signal01Pins); i++) {
    signalLeds[i].begin(signal01Pins[i]);
    signalLeds[i].setMode(ROX_DEFAULT);
  }
  signalLeds[0].on();
  signalLeds[1].on();
  // etc
}
neroroxxx commented 10 months ago

Also i would avoid the use of sizeof and instead define the number of pins

#define TOTAL_PINS 4

const byte signal01Pins[TOTAL_PINS] = { 11, 10, 9, 8 };  // Pins
RoxLed signalLeds[TOTAL_PINS];

void setup() {
  // setup leds for switches
  for (byte i = 0; i < TOTAL_PINS; i++) {
    signalLeds[i].begin(signal01Pins[i]);
    signalLeds[i].setMode(ROX_DEFAULT);
  }
  signalLeds[0].on();
  signalLeds[1].on();
  // etc
}
mavoIn commented 10 months ago

Hi, sorry for mixing two sketches... Copy and paste mistake! With your code example and some trouble shooting it works.

First i had to add a led.update() in the loop function to activate the output.

Secondly i had to invert my led on/off logic because my LOW/HIGH is inverted. Before using your lib i used MobaTools. There's an invert function while attaching the leds: MobaTools > SoftLed

Would you like to add this function to your LED as well? It makes the lib more flexible. If not, i hope ist okay to fork you lib locally?

Thanks you very much for all your help!

neroroxxx commented 10 months ago

You can fork it if you want i'll add a reverse option since it should have been added

neroroxxx commented 10 months ago

version 1.7.1 has led reverse polarity for RoxLed

mavoIn commented 10 months ago

hello neroroxxx,

thank you very much for adding reverse polarity in your last version.

unfortunaltelly its not working during setup (). Later on in the loop () it's working correctly. Here is my setup code:

  // setup leds for signal 01
  for (byte i = 0; i < SIGNAL01_TOTAL_PINS; i++) {
    signalLeds[i].begin(signal01Pins[i]);
    signalLeds[i].setMode(ROX_DEFAULT);
    signalLeds[i].reversePolarity(true);
    Serial.println(signalLeds[i].getPolarity());

    signalLeds[i].off();  // all leds off
  }
  signalLeds[0].on();  // first led on

Do you have an hint? Thx a lot. Best mavoIn

neroroxxx commented 10 months ago

version 1.7.2 has a fix for this, you could also just turn the led on the off inside the loop for now.

You can also now reverse the polarity in the begin method.

  // setup leds for signal 01
  for (byte i = 0; i < SIGNAL01_TOTAL_PINS; i++) {

    // add true after the pin number to reverse the polarity at boot
    signalLeds[i].begin(signal01Pins[i], true);

    signalLeds[i].setMode(ROX_DEFAULT);

    // reversePolarity is not longer needed during setup, however you can still
    // use it during the loop code
    // signalLeds[i].reversePolarity(true);

    Serial.println(signalLeds[i].getPolarity());

    signalLeds[i].off();  // all leds off
  }
  signalLeds[0].on();  // first led on
mavoIn commented 10 months ago

Now it works as expected. Thx