Using For Loop on LED Matrix

Hi I'm fairly new to Arduino and C++ and i'm trying to have a column of 4 leds streak across a 4x16 led panel.
Currently I am using a For loop to do the first swipe from right to left and then another for left to right.
I'm wondering if there is way to combine the two loops or soemthing. Any advice? Thanks!

#include <FastLED.h>
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 68
#define DATA_PIN 3
#define BRIGHTNESS 130
CRGB leds[NUM_LEDS];

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;
  }
}

I start by noticing that each for loop has 16 iterations. So just set up one for loop that goes through 16 iterations and increment/decrement your index variables accordingly. Of course, this would give you four LEDs starting at each end going the opposite direction at the same time...if that is what you want.

If you want to reduce the code even further, you can use math to calculate indexes for the led array and have only 1 index variable.

ToddL1962:
I start by noticing that each for loop has 16 iterations. So just set up one for loop that goes through 16 iterations and increment/decrement your index variables accordingly. Of course, this would give you four LEDs starting at each end going the opposite direction at the same time...if that is what you want.

If you want to reduce the code even further, you can use math to calculate indexes for the led array and have only 1 index variable.

I actually need the column of 4 leds to start on the left and go to the right then come back. It's the basis for a game where you would stop the 4 leds on a target. Thanks anyway :slight_smile:

RK303:
I actually need the column of 4 leds to start on the left and go to the right then come back. It's the basis for a game where you would stop the 4 leds on a target. Thanks anyway :slight_smile:

pseudocode

for index = 0 to number of LEDS times 2
{
if index < number of LEDS
  {
  position = index
  }
else
  {
  position = (number of LEDs times 2) - index
  }
}

I might have messed up the offsets but hopefully you get the idea...

I have found with these problems the trick is to have the 'increment' as a variable. Travelling in one direction, the increment is +1, travelling in the other direction the increment is -1.

You track the index (column number?) and use logic that is something like this

void loop()
}
  static int inc = 1;
  static int idx = 0;

  if (its time to animate the LEDs)
 {
    set the idx LED column to OFF
    idx += inc;
    if (idx == MAX_COLUMN-1 || idx == 0) inc = -inc;
    idx += inc;
    set the idx LED column to ON
  }
}

Hopefully this gets the idea across.