# Built in and out LED effect - direction help :)

**URL:** <https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282>\
**Category:** Programming\
**Created:** [March 1, 2024, 6:47am UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282 "2024-03-01T06:47:59Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [March 2, 2024, 7:26am UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/21 "2024-03-02T07:26:59Z")

</div>

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

Let's have another try.

```arduino
#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?

---

<div class="post-metadata">

**Author:** ![sg1oneill](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sg1oneill](https://forum.arduino.cc/u/sg1oneill)\
**Post date:** [March 2, 2024, 6:12pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/22 "2024-03-02T18:12:26Z")

</div>

That perfect! Thank you so much! 🙃

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 2, 2024, 8:42pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/23 "2024-03-02T20:42:39Z")

</div>

> [@sg1oneill](#):
>
> That perfect!

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

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

```

and you'll catch the ninth soldier exiting twice.

a7

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 2, 2024, 10:55pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/24 "2024-03-02T22:55:26Z")

</div>

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":

```arduino
.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:

```cpp
# 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. 😑

a7

---

<div class="post-metadata">

**Author:** ![sg1oneill](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sg1oneill](https://forum.arduino.cc/u/sg1oneill)\
**Post date:** [March 3, 2024, 4:48am UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/25 "2024-03-03T04:48:04Z")

</div>

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 😃 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 😃 as well 😃  
Without this, and other hardwares the project would never be alive 😃  
Thanks a lot, guys!

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 7, 2024, 3:55pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/26 "2024-03-07T15:55:37Z")

</div>

> [@sg1oneill](#):
>
> but if you would paste here a full code

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**
>
> ```arduino
> // 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

---

<div class="post-metadata">

**Author:** ![sg1oneill](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sg1oneill](https://forum.arduino.cc/u/sg1oneill)\
**Post date:** [March 15, 2024, 4:18pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/27 "2024-03-15T16:18:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 15, 2024, 4:24pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/28 "2024-03-15T16:24:38Z")

</div>

> [@sg1oneill](#):
>
> But I have extended for 10 LEDs

Hmmm, sounds impossible. :-|

Although in the last sketch provided, there is

```arduino
# 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

```arduino
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

---

<div class="post-metadata">

**Author:** ![sg1oneill](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sg1oneill](https://forum.arduino.cc/u/sg1oneill)\
**Post date:** [March 15, 2024, 5:08pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/29 "2024-03-15T17:08:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 15, 2024, 6:11pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/30 "2024-03-15T18:11:51Z")

</div>

> [@sg1oneill](#):
>
> Nothing else.

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 11, 2024, 6:12pm UTC](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282/31 "2024-09-11T18:12:01Z")

</div>

Closed on September 11, 2024, 6:12pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

[Previous page](https://forum.arduino.cc/t/built-in-and-out-led-effect-direction-help/1230282.md?page=1)
