Nested Loops and Arrays

hi,

using the following code, i don't seem to get the result i'm looking for.

state is a variable, which is set outside this code. for this example, state is set to 1, which refers to the first line is sideState (4, 6, 2, 5) these are linked to addressable leds in segments of 2, numbers, (6,7) (10,11) (2,3) (8,9)

int sideLeds[NUM_LEDS_IN_SEGMENT * NUM_SIDES];

initialisation of the led array, this is based on static variables... in my current case its 2*4=8

sideLeds[k] = sideState[state - 1][i] * NUM_LEDS_IN_SEGMENT + (j - 2);

sideState [ x ] [ y ] refers to the 2D array, x is the state, y is the led cluster

#define NUM_LEDS_IN_SEGMENT 2
#define NUM_SIDES 4

int sideLeds[NUM_LEDS_IN_SEGMENT * NUM_SIDES];

byte sideState[6][4] = {
  {4, 6, 2, 5}, //  1   0,1
  {5, 1, 6, 3}, //  2   2,3
  {5, 2, 6, 4}, //  3   4,5
  {3, 6, 1, 5}, //  4   6,7
  {4, 1, 2, 3}, //  5   8,9
  {3, 2, 1, 4}  //  6   10,11
};

int state = 1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  // start serial for output

}

void loop() {
  // put your main code here, to run repeatedly:

  for (int k = 0; k < NUM_LEDS_IN_SEGMENT * NUM_SIDES; k++) {
      for (int i = 0; i < 4; i++) {
          for (int j = 0; j < NUM_LEDS_IN_SEGMENT; j++) {

        sideLeds[k] = sideState[state - 1][i] * NUM_LEDS_IN_SEGMENT + (j - 2);
        Serial.print(sideLeds[k]);
      }
    }
  }
  Serial.println();
}

my PERCEIVED output, for this instance, where the variables are shown as values

sideLeds[8] = {6,7,10,11,2,3,8,9); and the output on the serial monitor should be 6710112389

but the actual output is;
67101123896710112389671011238967101123896710112389671011238967101123896710112389

which is correct, except its repeated 8 times.

i can see why the code is repeated as the overlying for loop K repeats 8 times,
but i don't know how to input 8 single variables into the array, without it outputting the whole array 8 times.

but i don't know how to input 8 single variables into the array, without it outputting the whole array 8 times.

Move the print() statement outside of the inner loops. The value will still be assigned 8 times, but it will only be printed once.

:o i get repeated 9s (99999999) when outside... which is a ludicrous result...!

I'm guessing you moved it out of all 3 of the loops. Since you are using k as an index in the print statement, if you move it all the way out of the for( int k = ... loop then k will be maxed out when the statement runs since that is the condition which causes the k loop to end which must happen in order to get to the print which is now outside that loop. So you are essentially always calling

print( sideLeds[ NUM_LEDS_IN_SEGMENT * NUM_SIDES ] );

at that point. Make sure you leave the print statement inside the outer k loop.

i have 4 stages of for loops and these are the results;

buried right in the centre;
67101123896710112389671011238967101123896710112389671011238967101123896710112389

next;
7113971139711397113971139711397113971139

next;
99999999

next;
'k' was not declared in this scope

it doesn't really make sense, but im going to try putting the right numbers (albeit 8 copies of them) into the full code and see how it works.

next;
'k' was not declared in this scope

it doesn't really make sense,

That makes perfect sense - think about what happens at the end of a "for" statement

i understand the end error, but my formatting wasn't too clever. apologies, i've edited my post

ive put the equation into the main code. and unfortunately, the end result is

sideLeds[8]=99999999 (as values)

so i def need to fix this! :slight_smile:

I wonder if you have your nesting back to front. What would happen if you do

for (j
    for (i
       for (k
           sideLeds[k] = sideState[state - 1][i] * NUM_LEDS_IN_SEGMENT + (j - 2);
           Serial.print(sideLeds[k]);

...R

jik is sideLeds[8]=66666666101010101010101022222222888888887777777711111111111111113333333399999999 (as values)

but im going to try all the different options now...

ijk = 66666666777777771010101010101010111111111111111122222222333333338888888899999999
ikj = 67676767676767671011101110111011101110111011101123232323232323238989898989898989
kji = 61028711396102871139610287113961028711396102871139610287113961028711396102871139
kij = 67101123896710112389671011238967101123896710112389671011238967101123896710112389
jik = 66666666101010101010101022222222888888887777777711111111111111113333333399999999
jki = 61028610286102861028610286102861028610287113971139711397113971139711397113971139

none of it really makes sense lol

ahha!

i know whats going on...!

its taking the 6710112389 generated as 1 figure in the array, so instead of

sideLeds[0] = 6
sideLeds[1] = 7
sideLeds[2] = 10
sideLeds[3] = 11
etc...

its doing

sideLeds[0] = 6710112389
sideLeds[1] = 6710112389
sideLeds[2] = 6710112389
sideLeds[3] = 6710112389

so... :o

after breaking down the code, and testing every single line... i have recoded and found a solution.
its definitely not pretty.... but it works.

#define NUM_LEDS_IN_SEGMENT 2
#define NUM_SIDES 4

int sideLeds[NUM_LEDS_IN_SEGMENT * NUM_SIDES];

byte sideState[6][4] = {
  {4, 6, 2, 5}, //  1   0,1
  {5, 1, 6, 3}, //  2   2,3
  {5, 2, 6, 4}, //  3   4,5
  {3, 6, 1, 5}, //  4   6,7
  {4, 1, 2, 3}, //  5   8,9
  {3, 2, 1, 4}  //  6   10,11
};

int state = 1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  // start serial for output

}

void loop() {
      for (int i = 0; i < NUM_LEDS_IN_SEGMENT*NUM_SIDES; i+=2) {
         for (int j = 0; j < NUM_LEDS_IN_SEGMENT; j++) {

          sideLeds[i] = (sideState[state-1][i/2]*NUM_LEDS_IN_SEGMENT)+(j-3);
          sideLeds[i+1] = (sideState[state-1][i/2]*NUM_LEDS_IN_SEGMENT)+(j-2);


         }
      }
          for (int x=0; x<NUM_LEDS_IN_SEGMENT*NUM_SIDES; x++) {
          Serial.print(sideLeds[x]);
          }
          Serial.println();
}

its definitely not pretty.... but it works.

There is nothing "not pretty" about code that works.

PaulS:
There is nothing "not pretty" about code that works.

not pretty, as it will only work for led clusters of 2.

the full project is clusters of 9!

kelvinmead:
not pretty, as it will only work for led clusters of 2.

the full project is clusters of 9!

I have to say I have not properly grasped what you are trying to achieve. Perhaps if you explain that in English rather than in code someone will be able to suggest a better approach.

...R

for (int i=0; i<NUM_SIDES; i++) {
  for (int j=0; j<NUM_LEDS_IN_SEGMENT; j++) {
      sideLeds2[i]=(sideState[state-1][i]*NUM_LEDS_IN_SEGMENT)-NUM_LEDS_IN_SEGMENT;
      sideLeds2[i]+=j;   
      Serial.print(sideLeds2[i]);
      sideLeds[i]=sideLeds2[i];

    }
}

reworked code to be "pretty" now this will work with any sized led cluster!

the weird sideLeds_=sideLeds2*; is due to the weird way fastled shows leds... still working on that, but the bulk of the code is :)*_
yay!