Search code examples
c++for-looparduinoled

How can I combine these two for loop statements?


Hi I'm using an Arduino to have a column of 4 leds streak across from left to right then back across on a 4x16 led panel. Currently I am using two for loop statements to make this happen but I'm wondering if there is way to combine the two loops or something so that left to right to left is all encompassed in one statement. Thanks.

int row1 = 0;
int row2 = 0;
int row3 = 0;
int row4 = 0;
int row1r = 0;
int row2r = 0;
int row3r = 0;
int row4r = 0;

void setup() {
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  Serial.begin(9600);
  while (! Serial);
}

void loop() {
  // Left to Right
  for (row1 = 0, row2 = 31, row3 = 32, row4 = 63; row1 <= 15 && row2 >= 16 && row3 <= 47 && row4 >= 48; row1++, row2--, row3++, row4--) {
    leds[row1] = CRGB::Blue;
    leds[row2] = CRGB::Blue;
    leds[row3] = CRGB::Blue;
    leds[row4] = CRGB::Blue;
    FastLED.show();
    delay(30);
    leds[row1] = CRGB::Black;
    leds[row2] = CRGB::Black;
    leds[row3] = CRGB::Black;
    leds[row4] = CRGB::Black;
  }
  // Right to Left
  for (row1r = 15, row2r = 16, row3r = 47, row4r = 48; row1r >= 0 && row2r <= 31 && row3r >= 32 && row4r <= 63; row1r--, row2r++, row3r--, row4r++) {
    leds[row1r] = CRGB::Blue;
    leds[row2r] = CRGB::Blue;
    leds[row3r] = CRGB::Blue;
    leds[row4r] = CRGB::Blue;
    FastLED.show();
    delay(30);
    leds[row1r] = CRGB::Black;
    leds[row2r] = CRGB::Black;
    leds[row3r] = CRGB::Black;
    leds[row4r] = CRGB::Black;
  }
}

Solution

  • I think a better algorithm approach for this requirement would be treating the led matrix as an array of 16 column then defining a function that takes column index and illuminates leds correspondent to that column , each led shall be defined as function of the variable columnIndex then looping over the 16 columns in the void loop .

    1)

    enter image description here

    void illuminateColumn(columnIndex)
    {
        //illuminate leds[4*columnIndex]
        //illuminate leds[4*columnIndex+1]
        //illuminate leds[4*columnIndex+2]
        //illuminate leds[4*columnIndex+3]
    
        delay(30)
    
        //blacken leds[4*columnIndex]
        //blacken leds[4*columnIndex+1]
        //blacken leds[4*columnIndex+2]
        //blacken leds[4*columnIndex+3]
    
    }
    
    void loop()
    {
      for(int i=0;i<15;i++)
       {
         illuminateColumn(i);
       }
    
      for(int j=15;j>0;j--)
       {
         illuminateColumn(i);
       }
    
    }
    

    2)

    enter image description here

    void illuminateColumn(columnIndex)
    {
    //illuminate leds[63-columnIndex]
    //illuminate leds[47-columnIndex]
    //illuminate leds[31-columnIndex]
    //illuminate leds[15-columnIndex]
    
    delay(30)
    
    //blacken leds[63-columnIndex]
    //blacken leds[47-columnIndex]
    //blacken leds[31-columnIndex]
    //blacken leds[15-columnIndex]
    
     }
    
     void loop()
      {
        for(int i=0;i<15;i++)
         {
           illuminateColumn(i);
         }
    
        for(int j=15;j>0;j--)
         {
          illuminateColumn(i);
         }
    
       }