Pattern Maths

Hi,

Trying to get my head around the arduino maths…

I have this;

void allleds(int cred, int cgrn, int cblu) {          //set all leds to one colour
  for(int i=0; i< NUM_LEDS; i++){                             // set i to 0; > whilst i is less than 255; increase i
    set_colour_led(i, cred, cgrn, cblu);                      // set leds colour (address number i, r, g, b)
    FastSPI_LED.show();                                          // turn on the less
    delay(1);                                                         // micropause
  }
}

what I want to know, is how to light every other leds, or groups of 4, in this fashion.

thanks

The key is the i++ statement (which means “add one to i”). changing it to i=i+2 will change every other LED. Groups of 4 are a little more complex, the simplest way would probably to use an outer loop like this :-

void allleds(int cred, int cgrn, int cblu) {          //set groups of 4 leds to one colour
  for(int j=0; j < NUM_LEDS / 8; j++)
  {
    for(int i=j*8; i< j*8+4; i++){                             // set i to start of group; > for the group of 4 increase i
      set_colour_led(i, cred, cgrn, cblu);                      // set leds colour (address number i, r, g, b)
      FastSPI_LED.show();                                          // turn on the less
      delay(1);                                                         // micropause
    }
  }
}

magic!

i did try i+1 but seemed to generate errors.

and ill try the second loop. :)

i++ is a C shortcut for writing i=i+1, it increments and saves the change. i+1 doesn't tell the compiler what you want to do with the result.

ok, so here’s the pattern i’m currently working on.

a nice and easy line around the perimeter of the matrix.

the strips are arranged in 10 strips of 32 leds, (currently testing with 4 strips… i can only make so much mess before the missus kicks off…)

I have variables set for, Number of strips, and leds per strip, giving me total number of leds.

i have worked out the 2 end codes (they were easy, just light up 2 full strips)

the rest of the code is set, so if I use 5 strips, the box pattern will still appear (this is for a modular lighting setup), hence not just using direct address’s

i have put in the 8 vertical lines as individuals, but some is commented out…

here is my issue. what I have here works fine, but if i add another line (going outside the realms of what I have physically, the strips stop responding to any programs… i understand that this is breaking the code, but i want to have this so i can change the setup without it stopping working…

…on the flip, i’d prefer to have tider code!

the address’s i want lighting for this section is

0-31
32 (strip of leds)
63,64 ((strip of leds *2) -1, (strip of leds *2))
95,96
127,128

its quite easy to visualise, but i can’t get my head around how this would tidy into 1 nice formula, or how to put into an array.

void outsidebox(int cred, int cgrn, int cblu) { //set all leds to one colour
  for(int a=0; a < LEDS_PER_STRIP; a++){ // left strip
    set_colour_led(a, cred, cgrn, cblu);}
    
//  for(int c=(NUM_LEDS - LEDS_PER_STRIP); c < NUM_LEDS; c++){ // right strip
//    set_colour_led(c, cred, cgrn, cblu);}

// bottom side
    set_colour_led(((LEDS_PER_STRIP * 2) -1), cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 2), cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 4) -1, cred, cgrn, cblu);
/*    set_colour_led((LEDS_PER_STRIP * 4), cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 6) -1, cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 6), cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 8) -1, cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 8), cred, cgrn, cblu);
    set_colour_led(((LEDS_PER_STRIP * 10) -1), cred, cgrn, cblu);

// top side
*/    set_colour_led(LEDS_PER_STRIP, cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 3) -1, cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 3), cred, cgrn, cblu);
/*    set_colour_led((LEDS_PER_STRIP * 5) -1, cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 5), cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 7) -1, cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 7), cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 9) -1, cred, cgrn, cblu);
    set_colour_led((LEDS_PER_STRIP * 9), cred, cgrn, cblu);
*/    
    FastSPI_LED.show();
    delay(1);
  
}

:slight_smile:

thanks!

The magic operator you near to learn about is “%” or modulo. What it returns is the amount left over from an integer division, so 9 % 10 is 9 and 11 % 10 is 1.
You’d end up with something like :-

void outsidebox(int cred, int cgrn, int cblu) { //set all leds to one colour
  int totalLeds = LEDS_PER_STRIP * NUMBER_OF_STRIPS;
  for(int i = 0; i < totalLeds; i++)
  {
    // check for first or last strip
    int stripNumber = i / LEDS_PER_STRIP;
    if ( stripNumber == 0 || stripNumber == NUMBER_OF_STRIPS - 1) 
      {
        set_colour_led(c, cred, cgrn, cblu)
      }
    else
      {
        // check is this the first or last LED in a strip
        int ledOnStrip = i % LEDS_PER_STRIP;
        if (ledOnStrip == 0 || ledOnStrip == LEDS_PER_STRIP - 1)
        {
          set_colour_led(c, cred, cgrn, cblu)
        }
      }
  }
  FastSPI_LED.show();
  delay(1);
}