Led chasing for loop

hi forum. i have 10 leds. how to light it one by one start from the left most and the right most at the same time and end at the middle and go back to its origin
using for loop.

bellow the code i try, its succeed run from the left most and the right most but at different time rate.

int timer=100;

void setup() { 
for(int pin =2; pin <12; pin++){

pinMode(pin,OUTPUT);
}

}
void loop() {  

  for (int thispin =2; thispin <7; thispin ++){
    for(int pin =11; pin >6; pin --){
      digitalWrite(thispin,HIGH);
      digitalWrite(pin,HIGH);
      delay(timer);
      digitalWrite(thispin,LOW);
      digitalWrite(pin,LOW);
    }
  }
}

thank you for any help

Not sure what you mean by that?

You seem to have lost all the indentation from your code.

its succeed run from the left (pin 2) most side and the right (11) most side to the middle but from pin 11 to pin pin 7 run more faster than pin 2 to pin 7

i don't see indentation problem since the code compile/ uploaded to board properly

when you are not sure what the algorithm should look like, decompose in small steps

  1. turn on led 0 and 9 - pause - turn off led 0 and 9 - pause
  2. turn on led 1 and 8 - pause - turn off led 1 and 8 - pause
  3. turn on led 2 and 7 - pause - turn off led 2 and 7 - pause
  4. turn on led 3 and 6 - pause - turn off led 3 and 6 - pause
  5. turn on led 4 and 5 - pause - turn off led 4 and 5 - pause
  6. turn on led 3 and 6 - pause - turn off led 3 and 6 - pause
  7. turn on led 2 and 7 - pause - turn off led 2 and 7 - pause
  8. turn on led 1 and 8 - pause - turn off led 1 and 8 - pause
  9. turn on led 0 and 9 - pause - turn off led 0 and 9 - pause

so you could have a crude implementation doing all those steps "manually"

but you could notice that the sum of the leds you turn on and off is always 9, so if you were to do

for(int i = 0; i<5; i++) {
   // here you could do something on led #i and led #9-i
   // what could it be ?
}

for(int i = 3; i>=0; i--) {
   // here you could do something on led #i and led #9-i
   // what could it be ?
}
1 Like

The compiler wouldn't care if you had no whitespace at all in your code:

int timer=100;void setup(){for(int pin=2;pin<12;pin++){pinMode(pin,OUTPUT);}}void loop() {  
for(int thispin=2;thispin<7;thispin++){for(int pin=11;pin>6;pin--){digitalWrite(thispin,HIGH);
digitalWrite(pin,HIGH);delay(timer);digitalWrite(thispin,LOW);digitalWrite(pin,LOW);}}}

But, when presenting it for humans to read, proper layout really helps!

1 Like

Hello donimart

I´v made a small code review.

In general - Arrays and structs are your friends.
Don't duplicate code in your sketch. Write code once - use it multiple times.
You should not use magic numbers. The I/O pins love to have a functional name.

Do you have experience with programming in C++?

The task can easily be realised with an object.
A structured array contains all information, such as pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?

Have a nice day and enjoy coding in C++.

1 Like

This?

  for (int i = 2; i < 8; i++) {
    digitalWrite (i, HIGH);
    digitalWrite (14-i, HIGH);
    delay(timer);
    digitalWrite (i, LOW);
    digitalWrite (14-i, LOW);
  }
}
  for (int i = 2; i < 7; i++) {
    digitalWrite (i, HIGH);
    digitalWrite (13-i, HIGH);
    delay(timer);
    digitalWrite (i, LOW);
    digitalWrite (13-i, LOW);
  }

i have 10 leds from pin 2 to 11 and the middle is pin 6 and 7, above code inspired from you. thank you alot

it goes up but not down... only one way animation

thank you, i think 2 loops should do the same as yours

unsigned long timer = 100;

void setup() {
  for (int pin = 2; pin < 12; pin++) {
    pinMode(pin, OUTPUT);
  }

}
void loop() {

  for (int i = 2; i < 8; i++) {
    digitalWrite (i, HIGH);
    digitalWrite (14-i, HIGH);
    delay(timer);
    digitalWrite (i, LOW);
    digitalWrite (14-i, LOW);
  }

  for (int i = 7; i >= 2; i--) {
    digitalWrite (i, HIGH);
    digitalWrite (14-i, HIGH);
    delay(timer);
    digitalWrite (i, LOW);
    digitalWrite (14-i, LOW);
  }

}

(to inject in @xfpd's wokwi simulation)

1 Like

yours nearly the perfect, but whole my pin start from 2 to 11

The new one...

  for (int i = 2; i < 7; i++) {
    digitalWrite (i, HIGH);
    digitalWrite (13-i, HIGH);
    delay(timer);
    digitalWrite (i, LOW);
    digitalWrite (13-i, LOW);
  }
      delay(timer/8.192);
  // delay(timer/64);
   for (int i = 7; i >= 2; i--) {
    digitalWrite (i, HIGH);
    digitalWrite (13-i, HIGH);
    delay(timer);
    digitalWrite (i, LOW);
    digitalWrite (13-i, LOW);
  }

i try this to reduce delay in the middle but nothing

Change the 7 to a 6 in the second loop (see the sim)

1 Like

hi. is it possible to change it to nested for loop

for (int donimart = 1; donimart < 7; donimart++) {
    digitalWrite (donimart, HIGH);
    digitalWrite (13 - donimart, HIGH);
    delay(timer);
    digitalWrite (donimart, LOW);
    digitalWrite (13 - donimart, LOW);
  }

  for (int J_M_L = 5; J_M_L >= 2; J_M_L--) {
    digitalWrite (J_M_L, HIGH);
    digitalWrite (13 - J_M_L, HIGH);
    delay(timer);
    digitalWrite (J_M_L, LOW);
    digitalWrite (13 - J_M_L, LOW);
  }

by the way why the code is running while declaration of variable in void setup and for loop is difference

What would you want to nest ?

I don’t understand the othe questiin

Hello donimart

I´m curious.

What is this programme used for?