Using two for loops together?

I am working on a project to control RGB pixels with DMX and need some help with the code. I would like to assign three DMX channels to each pixel for RGB. Pixel 1 will use DMX channel 1 for red, 2 for green, 3 for blue. Pixel 2 will use 4,5,6 etc. I am using the fast LED library and a DMX library.

Here is what I tried by using two for loops:

for(int pixel =0; pixel < NUM_LEDS; pixel++) //Counter for pixel number
{ 
  for(DMXchan = 0; DMXchan < totalDMXchan; DMXchan++)
leds[pixel] = CRGB(ArduinoDmx0.RxBuffer[DMXchan], ArduinoDmx0.RxBuffer[DMXchan], ArduinoDmx0.RxBuffer[DMXchan]); //assign three dmx channels per pixel for RGB
}
}
  FastLED.show(); //Sent data to pixels
  delay(50);
}

This code works but assigns every pixel to the first three DMX channels.

for(int pixel =0; pixel < NUM_LEDS; pixel++) //Counter for pixel number
{ 
leds[pixel] = CRGB(ArduinoDmx0.RxBuffer[0], ArduinoDmx0.RxBuffer[1], ArduinoDmx0.RxBuffer[2]); //assign three dmx channels per pixel for RGB
}
  FastLED.show(); //Sent data to pixels
  delay(50);
}

Without getting into exactly what you want to do, it looks like you could use two nested for loops. Here’s a generic sample. Load and run it. Observe the output. Apply it to your problem.

void setup() {
  Serial.begin(115200);

  Serial.println("i  j");

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 5; j++) {
      Serial.print (i);
      Serial.print ("  ");
      Serial.println (j);
    }
  }
}

void loop() {}

Thank you. I'm not sure I understand this. Here is what I'm trying to do:

leds[0] = CRGB(ArduinoDmx0.RxBuffer[0], ArduinoDmx0.RxBuffer[1], ArduinoDmx0.RxBuffer[2]); //set first pixel to first three dmx channels
leds[1] = CRGB(ArduinoDmx0.RxBuffer[3], ArduinoDmx0.RxBuffer[4], ArduinoDmx0.RxBuffer[5]);
leds[2] = CRGB(ArduinoDmx0.RxBuffer[6], ArduinoDmx0.RxBuffer[7], ArduinoDmx0.RxBuffer[8]);
leds[3] = CRGB(ArduinoDmx0.RxBuffer[9], ArduinoDmx0.RxBuffer[10], ArduinoDmx0.RxBuffer[11]);
  
FastLED.show(); //Sent data to pixels
  delay(50);

I'm looking for a way to combine this with possibly a for loop so I don't have to write out fifty lines.

I'm looking for a way to combine this with possibly a for loop so I don't have to write out fifty lines.

What do you mean by "combine this"? Combine it with what?

What do you mean by "combine this"? Combine it with what?

Is there a way to combine the lines of code in my previous post so I don't have to write out each line for each pixel and assign it three dmx channel numbers? I tried using two for loops. One for the pixel number(leds[x]) and one for the DMX channel number (RxBuffer[y]).

So I could certainly be wrong, but since you’re making a single call to CRGB this doesn’t seem like a nested loop problem but something more like:

const byte numberLEDs   = 100;
const byte channelWidth = 3;
byte channel;

[...]  

channel = 0;

for (byte ledNumber = 0; ledNumber < numberLEDs; ledNumber++) {
  leds[ledNumber] = CRGB(ArduinoDmx0.RxBuffer[channel], 
                         ArduinoDmx0.RxBuffer[channel + 1], 
                         ArduinoDmx0.RxBuffer[channel + 2]);
						 
   channel += channelWidth;
}  

FastLED.show(); //Sent data to pixels
delay(50);

Should get you pretty close unless I misunderstood what you were looking for.

Regards,

Brad
KF7FER

Thank you! This works perfectly.

Hi

Im doing the same thing as you. Am i able to have the competed code?

Thanks

Ian