Built in and out LED effect - direction help :)

Your code still has the problems I pointed out originally. Plus, it's very, very long and repetitive.

Let's have another try.

#define t   20
#define t1  50
#define t2  100


void setup() {
  // set up pins 2 to 11 as outputs
  for (int i = 2; i <= 11; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  effect_1_fill_in_out();
}

//******** EFFECT 1 ********
void effect_1_fill_in_out() {

  for(int pinX = 2; pinX <= 11; pinX++) {
    for(int pin = 11; pin > pinX; pin--) {
      digitalWrite(pin, HIGH);
      delay(t);
      digitalWrite(pin, LOW);
    }
    digitalWrite(pinX, HIGH);
  }
  delay(t2);

  // ************ BUILT OUT ************

  for(int pinX = 2; pinX <= 11; pinX++) {
    digitalWrite(pinX, LOW);
    for(int pin = pinX; pin >= 2; pin--) {
      digitalWrite(pin, HIGH);
      delay(t);
      digitalWrite(pin, LOW);
    }
  }
  delay(t2);

}

Any closer?

That perfect! Thank you so much! :upside_down_face:

I put the code into my hack for neopixels, and I noticed that t1 was never used. I did find a place for it where the new soldier is given a moment of his own, or the remaining soldiers are so paused over.

The time constants are small so it is hard to see what is really going on.

Turn them up

# define t   200
# define t1  500  // unsued
# define t2  2000

and you'll catch the ninth soldier exiting twice.

a7

I was thinking about how to "see" this better, and I hacked my hack to print out the current LED state.

Here's the end of the sequence, where we catch what the UA gang has called "the streaker":

.X.....XX
.......XX
X......XX
.......XX
........X
.......XX
........X
......X.X
........X
.....X..X
........X
....X...X
........X
...X....X
........X
..X.....X
........X
.X......X
........X
X.......X
........X
.........
........X
.........
.......X.
.........
......X..
.........
.....X...
.........
....X....
.........
...X.....
.........
..X......
.........
.X.......
.........
X........
.........
.........
.........
.........
........X
.........
.......X.
.........
......X..
.........
.....X...
.........
....X....
.........
...X.....
.........
..X......
.........
.X.......
.........
X........
.........
.........
.........
........X
.........

And the hack:
# define digitalWrite myDigitalWrite

void myDigitalWrite(int theLED, int theValue)
{
  strip.setPixelColor(theLED - 2, theValue ? 0xff0000 : 0x202020);
  strip.show();

  for (int ii = 0; ii < 9; ii++)
    Serial.print(strip.getPixelColor(ii) == 0xff0000 ? "X" : ".");
  
  Serial.println("");
}

I think the easiest solution is to say that is what you wanted to happen. :expressionless:

a7

Well, that doesn't say me too much, but if you would paste here a full code I just can paste it into my A_IDE I would love to try it out and think on it a bit. I love to learn :smiley: However, I have a lot of work on the hardware now, I am just testing fixing one of my circuits that gives me a hard time :smiley: as well :smiley:
Without this, and other hardwares the project would never be alive :smiley:
Thanks a lot, guys!

Please try this with 9 LEDs attached to pins 2..10.

It... does what you wanted. There is an odd partial symmetry between marching on and marching off. I had some trouble until I focused and just ran all the loops from 0 and fixed LED references accordionly.

It uses delay, so blocks, switching it to the unblocking style would be

Making it go backwards could be done by making the LED pin array a variable the functions could use, so providing an array with the LEDs in reverse order would be Presto! backwards.

Summary
// https://wokwi.com/projects/391667794741666817
// https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282

# define SIZE 9
byte pins[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};

# define t0  200
# define t1  400
# define t2  800

void setup() {
  Serial.begin(115200);
  Serial.println("for loop version N\n");

  for (int ix = 0; ix < SIZE; ix++)
    pinMode(pins[ix], OUTPUT);
}

void loop() {
  fileOnLeft();
  fileOffRight();
}

// turn on from the left starting from all off
void fileOnLeft()
{
  for (int ix = 0; ix < SIZE; ix++) {
    byte K = SIZE - 1;

    for (int iy = 0; iy < SIZE - ix; iy++) {
      digitalWrite(pins[K - iy], HIGH);
      delay(t0);
      digitalWrite(pins[K - iy], LOW);
    }

    digitalWrite(pins[ix], HIGH);
    delay(t1);
  }
  delay(t2);
}

// turn off from the right starting from all on
void fileOffRight()
{
  for (int ix = 0; ix < SIZE; ix++) {
    byte K = ix - 1;

    digitalWrite(pins[ix], LOW);

    for (int iy = 0; iy < ix; iy++) {
      digitalWrite(pins[K - iy], HIGH);
      delay(t0);
      digitalWrite(pins[K - iy], LOW);
    }

    delay(t1);
  }
  delay(t2);
}

a7

Well, I had time now and test it out. That works well!
But I have extended for 10 LEDs :smiley:
Thank you. Now I have two different codes those do what I need.

Hmmm, sounds impossible. :-|

Although in the last sketch provided, there is

# define SIZE 9
byte pins[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};

And the rest of the code makes no mention of nine, only SIZE…

@alto777 should have written

byte pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const byte SIZE = sizeof pins / sizeof *pins;

which makes no mention of any actual number. What, in that latter bit of code is making it work now for nine pins?

a7

Well, it is working nicely on my desk (LEDs physically attached my pro micro)
I have changed these: Nothing else.

define SIZE 10

byte pins[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

Very good. You are on your way.

Look at the second way of saying more or less the same thing, which lets the compiler figure out how many pins there are in the array.

a7