Built in and out LED effect - direction help :)

HI.
I have this code. It builds in the LEDs well and bricks them out well. However, I want either the building effect from the other direction or the brick-out effect would happen to another direction. So it would create a "snake effect" if you get me. I am not a programmer, but I tried many different changes in the code - a little bit of understanding the "for" loop, but I am not able to change the direction in either side of the code.
Thanks for any help.


#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_fill_in_out();

  }

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

  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin, HIGH);
  delay(t);
  digitalWrite(pin+1, LOW);
  delay(t);
  }
  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+1, HIGH);
  delay(t);
  digitalWrite(pin+2, LOW);
  delay(t);
  }
  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+2, HIGH);
  delay(t);
  digitalWrite(pin+3, LOW);
  delay(t);
  }
  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+3, HIGH);
  delay(t);
  digitalWrite(pin+4, LOW);
  delay(t);
  }
    for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+4, HIGH);
  delay(t);
  digitalWrite(pin+5, LOW);
  delay(t);
  }
  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+5, HIGH);
  delay(t);
  digitalWrite(pin+6, LOW);
  delay(t);
  }
  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+6, HIGH);
  delay(t);
  digitalWrite(pin+7, LOW);
  delay(t);
  }
   for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+7, HIGH);
  delay(t);
  digitalWrite(pin+8, LOW);
  delay(t);
  }
  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+8, HIGH);
  delay(t);
  digitalWrite(pin+9, LOW);
  delay(t);
  }
    for(int pin =11; pin >= 2; pin--)
  {
  digitalWrite(pin+9, HIGH);
  delay(t);
  digitalWrite(pin+10, LOW);
  delay(t);
  }
  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+10, HIGH);
  delay(t);
  digitalWrite(pin+11, LOW);
  delay(t);
  }
  for(int pin = 11; pin >= 2; pin--)
  {
  digitalWrite(pin+11, HIGH);
  delay(t2);
  }
*
// ************ BUILT OUT ************
// However, direction is not the same! 
// here is my problem. LEDs should be dropeed out to the same direction, so it would be much more nice.

for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin, HIGH);
  delay(t);
  digitalWrite(pin+11, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+11, HIGH);
  delay(t);
  digitalWrite(pin+10, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+10, HIGH);
  delay(t);
  digitalWrite(pin+9, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+9, HIGH);
  delay(t);
  digitalWrite(pin+8, LOW);
  delay(t);
  }
    for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+8, HIGH);
  delay(t);
  digitalWrite(pin+7, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+7, HIGH);
  delay(t);
  digitalWrite(pin+6, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+6, HIGH);
  delay(t);
  digitalWrite(pin+5, LOW);
  delay(t);
  }
   for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+5, HIGH);
  delay(t);
  digitalWrite(pin+4, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+4, HIGH);
  delay(t);
  digitalWrite(pin+3, LOW);
  delay(t);
  }
    for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+3, HIGH);
  delay(t);
  digitalWrite(pin+2, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+2, HIGH);
  delay(t);
  digitalWrite(pin+1, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+11, LOW);
  delay(t2);
  }

}

I'm not quite getting the pattern you are looking for. You fill in the "bricks" then un-fill them?

Something about a snake?

It does look like code Jack would write. Should have asked Carter, or maybe McCay.

-jim lee

You will need to explain what you mean by these terms, they are not commonly understood. How are they different from "turn on" and "turn off"?

Here, you are writing to pin 12, which you have not set to be an output. Is there an led attached to pin 12?

and here you are writing to pin 22. What type of arduino are you using?

I am sorry I didn't express myself well and clearly :frowning: Here is a short video, if you watch it you will understand my little issue.

As you can see, LEDs build in from right to left, then with the same "effect" they "drop-out" to right again. What I want to see is drop them out to the left side, the same way ( use the same dropping-out effect, or I am not sure what should I call it :smiley: ) It is a dropping-out effect or similar to the "rainbow effect" I am not sure. Doesn't matter, the point is, I want to change the second part of the code, to reach the same effect just in a different direction.
I hope that is now a good explanation. :slight_smile:
Thank you so much!
(also I want to understand because I was trying a lot of change, I reach so many different result, but not that I wanted :smiley: )

You may mean this, they all march on from the left, and march off to the right. The dark boxes are where you might want a pause for dramatic purposes.

That will be a cool effect. I'm afraid you'll just have to keep thinking about it if no one solves it otherwise. This is general for loop hacking of the best kind.

a7

Yes, I guess, that is what I want. I have basically, just as I mentioned not the right direction :smiley:
If you see my video, you can see LEDs march on from right to left, which is OK, but unfortunately, they march off back to right. I just need a little help with how to write the second part of the code, to march off the LEDs to the left.
I have tried playing with the code a lot... as I mentioned, I have got a couple of different effects :smiley: but I couldn't reach them when they marched off to the left :smiley: well, one time they did, LEDs were OFF after each other, but the chasing effect started from right :smiley: so the marching-off effect didn't happen. :smiley:

Looks like 9 different configurations in that diagram. I think one could do it with a single loop and the % remainder operator

The static positions are not a big deal. It's the marching in or on to stage and later the marching off stage animation that makes it a bit less easy.

I think that @sg1oneill would benefit from making the much longer similar chart showing each little step, and writing down next to each "frame" which LED was lit up or extinguished, then studying that to discern a pattern.

Steps within steps suggests nested loops; maths should take care of extents and directions.

It does kinda make my brain hurt to start solving it.

a7

Is this anything like what you wanted?

#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 = 11; pinX >= 2; 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 <= 11; pin++) {
      digitalWrite(pin, HIGH);
      delay(t);
      digitalWrite(pin, LOW);
    }
  }
  delay(t2);

}

I only had nine minutes, so I hacked it to use neopixels.

I did change the timing constants (too fast!) but otherwise think I did not disturb the logic.

Play with @PaulRB's attempt here:


Wokwi_badge Marching On and Off


Hacked code
// https://wokwi.com/projects/391185057960646657

# include <Adafruit_NeoPixel.h>

# define PIN 7

Adafruit_NeoPixel strip = Adafruit_NeoPixel(9, PIN, NEO_GRB + NEO_KHZ800);

# define digitalWrite myDigitalWrite

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

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

void setup() {
  strip.begin();

  // 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 = 11; pinX >= 2; 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 <= 11; pin++) {
      digitalWrite(pin, HIGH);
      delay(t);
      digitalWrite(pin, LOW);
    }
  }
  delay(t2);

}

Two minutes to spare. Sry if I bungled it, gotta jet.

a7

Well, not. Thank you for the code, nice to learn.
However, I have shared a video, in my post with a working code. The code is doing exactly what you see in the video. The LEDs march on the way you see in the video, which is good as it is! Then march off, almost the same way. What is good again! I just have a problem with the direction of it :smiley: Nothing else.
I want the current march-off effect would take place in the other direction. Nothing else.
So LEDs march in from right to left (this is what I have and working well as you can see), then they should march off to the left. (not to the right as I have in the video and in the code).
I will be working on it myself as well. But some clue should be nice :smiley:
Thanks :smiley:

Not my code, just my hack, thank @PaulRB. I can't tell just by reading code what something will look like, so while I was able I wired it up to see.

Call it an operating document. :expressionless:

It does seem to not do the process you envision. I can only be so sure that something I did in my haste did it inadvertently damage the logic, but all I think I did was to trick it into a neopixel strip, which was faster to do than wiring, even virtually, nine real simulated LEDs.

When I get back to the lab I will wire up nine LEDs and use the code unchanged but for the timing constants.

a7

If you know the cycle length, and the relative positions of ons and offs within the cycle, some maths can take care of it:

void drfPhaseLoop(void) {
  const byte pins[] = {11,10,9,8,7,6,5,4,3,2};
  const byte numPins = sizeof(pins) / sizeof(pins[0]);
  const byte onWidth = 5;
  for (int phase = 0; phase < numPins + onWidth; ++phase) {
    int offPhase = phase - onWidth;
    if (phase < numPins) {
      Serial.print(char('A'+phase));
      digitalWrite(pins[phase], HIGH);
    }
    if (offPhase >= 0 && offPhase < numPins) {
      Serial.print(char('a'+offPhase));
      digitalWrite(pins[offPhase], LOW);
    }
    delay(1000);
  }
}

I'll take another stab at it tomorrow.

Nice effect, but the code is "not my level". Also, LEDs are very very light, looks like they are not operating fully.
Thanks to the code I saved it anyway.
Ps. I want this as well, but it is still not what I showed in the video.

You are right. I was just riffing off alto777's diagram, and I hadn't actually watched the video, One would need two loops to do that effect.

This wanted to be easier, maybe it was.

Try it here:

Wokwi_badge Table Driven Marching LEDs


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

# include <Adafruit_NeoPixel.h>

# define PIN  7
# define SIZE 7

Adafruit_NeoPixel strip = Adafruit_NeoPixel(7, PIN, NEO_GRB + NEO_KHZ800);

# define kT1  50      // travel
# define kT2  700     // arrive
# define kT3  3000    // dwell

int table[][2] =
{//  0123456
  {  1, kT1},
  {  2, kT1},
  {  4, kT1},
  {  8, kT1},
  { 16, kT1},
  { 32, kT1},
  { 64, kT2},
  { 65, kT1},
  { 66, kT1},
  { 68, kT1},
  { 72, kT1},
  { 80, kT1},
  { 96, kT2},
  { 97, kT1},
  { 98, kT1},
  {100, kT1},
  {104, kT1},
  {112, kT2},
  {113, kT1},
  {114, kT1},
  {116, kT1},
  {120, kT2},
  {121, kT1},
  {122, kT1},
  {124, kT2},
  {125, kT1},
  {126, kT2},
  {127, kT3},
  { 63, kT2},
  { 95, kT1},
  { 31, kT2},
  { 47, kT1},
  { 79, kT1},
  { 15, kT2},
  { 23, kT1},
  { 39, kT1},
  { 71, kT1},
  {  7, kT2},
  { 11, kT1},
  { 19, kT1},
  { 35, kT1},
  { 67, kT1},
  {  3, kT2},
  {  5, kT1},
  {  9, kT1},
  { 17, kT1},
  { 33, kT1},
  { 65, kT1},
  {  1, kT2},
  {  2, kT1},
  {  4, kT1},
  {  8, kT1},
  { 16, kT1},
  { 32, kT1},
  { 64, kT1},
  {  0, kT3},
};

const int nSteps = sizeof table / sizeof *table;

void setup() {
  Serial.begin(115200);
  Serial.println("table where\n");

  strip.begin();
}

int step;

void loop() {
  strip.clear();

  for (int ii = 0; ii < SIZE; ii++) {
    if (table[step][0] & (1 << ii))
      strip.setPixelColor(ii, 0xff0000);
    else
      strip.setPixelColor(ii, 0x303030);
  }

  strip.show();
  delay(table[step][1]);

  step++; if (step >= nSteps) step = 0;
}

a7

@DaveX - yes, nice. A train. There are a few ways to code that, I have a few animations that do multiple trains going one or the other direction.

Neopixels make it easier, and a slight bit of trouble choosing the colours for different traveling objects let them get along, passing each other or crashing or whatever.

Easiest: red, green and blue trains.

a7

That's it!!! :upside_down_face:
The only thing is, I need the code for dumb LEDs, as my original code shows. "in for loops" if possible. So could you please help me to change that code?
There are two reasons for that!

  1. The code you have written is not my level.
  2. I just want it for a different project, and it won't drive LEDs, it will drive a high voltage/frequency sequencer. (basically, the Arduino will drive optocouplers and triacs)

I promise you, in the next couple of months, I will show you guys, you will love it!!! That is not something commonly found! (there are some... of course, but... you'll see :smiley: )
I will share my site and my projects. Well, they will be much much more, than just "projects" :smiley:

I HAVE F DONE IT :rofl: :rofl: :rofl:
I cannot believe it :smiley:

//Test March on - off 10 Dumb LEDs effect.

#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);
  }
}
//Effects setup
void loop()
{
// ************ MARCH ON ************
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin, HIGH);
  delay(t);
  digitalWrite(pin-1, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-1, HIGH);
  delay(t);
  digitalWrite(pin-2, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-2, HIGH);
  delay(t);
  digitalWrite(pin-3, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-3, HIGH);
  delay(t);
  digitalWrite(pin-4, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-4, HIGH);
  delay(t);
  digitalWrite(pin-5, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-5, HIGH);
  delay(t);
  digitalWrite(pin-6, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-6, HIGH);
  delay(t);
  digitalWrite(pin-7, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-7, HIGH);
  delay(t);
  digitalWrite(pin-8, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-8, HIGH);
  delay(t);
  digitalWrite(pin-9, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-9, HIGH);
  delay(t);
  digitalWrite(pin-10, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-10, HIGH);
  delay(t);
  digitalWrite(pin-11, LOW);
  delay(t);
  }
for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin-11, LOW);
  delay(t);
  }

// ************ MARCH OFF ************
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin, HIGH);
  delay(t);
  digitalWrite(pin+11, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+11, HIGH);
  delay(t);
  digitalWrite(pin+10, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+10, HIGH);
  delay(t);
  digitalWrite(pin+9, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+9, HIGH);
  delay(t);
  digitalWrite(pin+8, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+8, HIGH);
  delay(t);
  digitalWrite(pin+7, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+7, HIGH);
  delay(t);
  digitalWrite(pin+6, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+6, HIGH);
  delay(t);
  digitalWrite(pin+5, LOW);
  delay(t);
  }
   for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+5, HIGH);
  delay(t);
  digitalWrite(pin+4, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+4, HIGH);
  delay(t);
  digitalWrite(pin+3, LOW);
  delay(t);
  }
    for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+3, HIGH);
  delay(t);
  digitalWrite(pin+2, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+2, HIGH);
  delay(t);
  digitalWrite(pin+1, LOW);
  delay(t);
  }
  for(int pin = 1; pin <= 11; pin++)
  {
  digitalWrite(pin+11, LOW);
  delay(t);
  } 
}