Setting values of a longer array equal to values of a shorter array

Hello all!

It's been a little while since i've had a chance to work with my arduino, real life is a head ache like that... but i have a some time now and i'm making progress.

But i have a question that i hope isn't too difficult.

Right now i have two arrays with unequal lengths.

One array represents an LED strip, and has a size of 50 integers.
the second array represents information i want to display on the led (if the program is selected)

For example

the led strip is 50 leds long (the array is 50 integers long)

my pattern is

white white white red red red blue blue blue

which will be represented as an array of integers, making the array size 9.

I want to be able to set my led array equal to the pattern array, in a repeating fashion. I hope i'm making sense.

I've included some snippets of my code below that pertain to this, but i've also attached the full enchilada below.

struct CRGB { 
  unsigned char b;
  unsigned char r; 
  unsigned char g;  
};

struct CRGB *leds;
#define NUM_LEDS 50

//create the LED array

int colorr[] = {
  255, 255, 255, 255, 255, 255, 0, 0, 0};
int colorb[] = {
  244, 244, 244, 0, 0, 0, 255, 255, 255};
int colorg[] = {
  255, 255, 255, 0, 0, 0, 0, 0, 0};

//create the pattern arrays



void chasing() {

  unsigned long currentInterval = 200;
  unsigned long now = millis ();
  static int j;

  if (now - chasingStartTime >= currentInterval)  // if it's time to increment
  {
    patternShift(colorr, colorb, colorg, 9); //this shifts the pattern so that there can be movement at a controlled speed).
    chasingStartTime = now;  //reset timer
  }  

  for(int i; i < NUM_LEDS+4; i++){  
    if (FunctionForEachPixel (i, 7)) {
      leds[i].r = colorr[i];
      leds[i].b = colorb[i];
      leds[i].g = colorg[i];  //leds array is has a size of 50, but colorg only has a size of 9. I'd like to repeat colorg[i] 1 though 9 for all values of leds array. 
    }  
  }  
}

void patternShift(int colorr[], int colorb[], int colorg[], int arraySize) {

  int tempRed;
  int tempBlue;
  int tempGreen;


  for (int red=0; red<(arraySize -1); red++)
  {
    tempRed = colorr[arraySize-1];
    colorr[arraySize-1] = colorr[red];
    colorr[red] = tempRed;
  }

  for (int blue=0; blue<(arraySize -1); blue++)
  {
    tempBlue = colorb[arraySize-1];
    colorb[arraySize-1] = colorb[blue];
    colorb[blue] = tempBlue;
  }

  for (int green=0; green<(arraySize -1); green++)
  {
    tempGreen = colorg[arraySize-1];
    colorg[arraySize-1] = colorg[green];
    colorg[green] = tempGreen;
  }

}

_6803_LED_box2_1.ino (18.6 KB)

A for loop and some logic to handle circulating the array of 9 values while you traverse the array of 50 will do it.

I'll leave that to you or someone else, I have a different method.

You sound like you are using a lot of memory.

Here is a method you can use to repeat 9 values ( or any amount ). It uses a struct to mimic an array of unlimited size. It maps the index to the internal array of nine values. And you can use it like an array.

Try this example, it repeats 9 numbers. Open the serial monitor and change the baud rate to 115200

struct Repeat9{ 
  unsigned char operator []( uint32_t idx ){ return pattern[ ( idx % 9 ) - 1 ]; }
  unsigned char pattern[ 9 ];
};

void setup() {
  unsigned long idx = 0;
  
  Serial.begin( 115200 );
  
  Repeat9 pattern = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22 };

  do{
    Serial.print( pattern[ idx ], HEX );
    
    //Add a new line and delay every 100 chars.
    if( !( idx % 100 ) ){
       Serial.print("\r\n");
       delay( 30 );
    }
  }while( ++idx );
}

void loop() {}

Thanks for your reply.

How does that increment and where should a put it? Inside the for loop for the LED array.

I've tried incrementing a variable outside of the for loop, but that would just make all the i's equal to the pattern arrays first value, then second value and so on....

Also, what is the benefit of doing of that way?

Thanks for your help

maybe the question isn't clear, let me draw it out

ledarray[50]

colorarray[9]

color array has the following values

1, 1, 1, 2, 2, 2, 7, 7, 7

I want to do something like

Ledarray[1]=1 (which would be colorarray[1]
Ledarray[2]=1
Ledarray[3]=1
Ledarray[4]=2
Ledarray[5]=2
Ledarray[6]=2
Ledarray[7]=3
Ledarray[8]=3
Ledarray[9]=3
Ledarray[10]=1
Ledarray[11]=1
Ledarray[12]=1
Ledarray[13]=2
Ledarray[14]=2
Ledarray[15]=2
Ledarray[16]=3
Ledarray[17]=3
Ledarray[18]=3 (which would be colorarray[9]
.
.
.
Ledarray[50]=2

I realize that arrays start at zero

Your question is clear.

The benefit of my example is it uses only 9 bytes, not 50. The value is determined as you need it.

Your method is processing the data ( 9 into 50 ), then reprocessing it to use it.

Also, your code mixes the order of RGB, BRG, to avoid passing the wrong colour parameter to something, always stick with the universal RGB.

Here is a version that shows how to use your code inline:

struct CRGB { 
  unsigned char b, r, g; 
};

unsigned char colorr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
unsigned char colorg[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19};
unsigned char colorb[] = { 21, 22, 23, 24, 25, 26, 27, 28, 29};

void setup() {
  Serial.begin( 9600 );
}

void loop() { 
  Serial.println( "\r\nDISPLAY PATTERN:" );
  
  for( unsigned int i = 0 ; i < 50 ; ++i ){
    
    const unsigned int idx = ( i % 9 );
    CRGB next = { colorb[idx], colorr[idx], colorg[idx] };
    
    Serial.print( "RGB{ " );
    Serial.print( next.r );
    Serial.print( ", " );
    Serial.print( next.g );
    Serial.print( ", " );
    Serial.print( next.b );
    Serial.println( " }" );
    
    //Output led here.
  }
  delay( 1000 );
  Serial.println( "\r\nGENERATE NEXT PATTERN" );
  patternShift(colorr, colorb, colorg, 9);
  delay( 1000 );
}

void patternShift(unsigned char colorr[], unsigned char colorb[], unsigned char colorg[], unsigned char arraySize) {

  int tempRed;
  int tempBlue;
  int tempGreen;


  for (int red=0; red<(arraySize -1); red++)
  {
    tempRed = colorr[arraySize-1];
    colorr[arraySize-1] = colorr[red];
    colorr[red] = tempRed;
  }

  for (int blue=0; blue<(arraySize -1); blue++)
  {
    tempBlue = colorb[arraySize-1];
    colorb[arraySize-1] = colorb[blue];
    colorb[blue] = tempBlue;
  }

  for (int green=0; green<(arraySize -1); green++)
  {
    tempGreen = colorg[arraySize-1];
    colorg[arraySize-1] = colorg[green];
    colorg[green] = tempGreen;
  }

}

It produces:

DISPLAY PATTERN:
RGB{ 1, 11, 21 }
RGB{ 2, 12, 22 }
RGB{ 3, 13, 23 }
RGB{ 4, 14, 24 }
RGB{ 5, 15, 25 }
RGB{ 6, 16, 26 }
RGB{ 7, 17, 27 }
RGB{ 8, 18, 28 }
RGB{ 9, 19, 29 }
RGB{ 1, 11, 21 }
RGB{ 2, 12, 22 }

And will use your shift function after each set of 50.

that works beautifully.

Thanks! It's so pretty.

Now i think 98% of the work is done on this project.

Thanks again!