LED Chaser with "for " loop by accident!

Hey all...

I am new to Arduino and its software and while learning to use 'int' to avoid having to write the 'pinMode' thing for every single LED on my VU meter, I accidently created an LED chaser that works really well. But I can't understand how it worked. So it would be really nice if someone could look at my code and write a bit about it.

int pins[5] = {2,3,4,5,6};

void setup() {
for (int i=0; i<5; i++) {
pinMode(pins*, OUTPUT);*
}
}
void loop() {

  • for (int i=0; i<5; i++) {*
    _ digitalWrite(pins*, 1);_
    _
    delay(50);_
    _ digitalWrite(pins, 0);
    delay(50);
    }
    }*
    It turns the LEDs on the pins 2-6 one-by-one according to the delay I set.
    I originally wanted all the LEDs to blink for a test and wrote this thing hoping not to write 'pinMode' to all my 12 LEDs of my VU Meter._

void loop() {
for (int i=0; i<5; i++) {
digitalWrite(pins, 1);
delay(50);
digitalWrite(pins, 0);
delay(50);
}
}

digitalWrite(pins, 1);

Turns on the LED on the first pin

digitalWrite(pins, 0);

Turns the same LED off after the delay

Then i increases by 1 and the process repeats for the next LED.

Added comments

int pins[5] = {2, 3, 4, 5, 6};  //array of pins with LEDs

void setup()
{
  for (int i = 0; i < 5; i++)  //should be 4 not 5
  {
    pinMode(pins, OUTPUT);
  }
}

void loop()
{
  for (int i = 0; i < 5; i++) //a counter from 0 to 5 - should be from 0 to 4
  {
    digitalWrite(pins[i], 1); //turn the the current pin HIGH
    delay(50);      //wait a bit
    digitalWrite(pins[i], 0); /turn teh current pin LOW
    delay(50);      //wait a bit
  }
} //now do it over again

Note that you have 5 pin numbers in the array numbered 0 to 4 but your for loop uses 0 to 5

If you want all of the LEDs to flash at the same time then turn them all on, wait a bit, turn them off and wait a bit. Do not wait between changing the state of each LED

Compare with :

void loop() {
  for (int i=0; i<5; i++) {
     digitalWrite(pins[i], 1);
  }
  delay(50);

  for (int i=0; i<5; i++) {
     digitalWrite(pins[i], 0);
 }
  delay(50);
}

Regards,
bidouilleelec

UKHeliBob:
Added comments

int pins[5] = {2, 3, 4, 5, 6};  //array of pins with LEDs

void setup()
{
  for (int i = 0; i < 5; i++)  //should be 4 not 5
  {
    pinMode(pins, OUTPUT);
  }
}

void loop()
{
  for (int i = 0; i < 5; i++) //a counter from 0 to 5 - should be from 0 to 4
  {
    digitalWrite(pins[i], 1); //turn the the current pin HIGH
    delay(50);      //wait a bit
    digitalWrite(pins[i], 0); /turn teh current pin LOW
    delay(50);      //wait a bit
  }
} //now do it over again




Note that you have 5 pin numbers in the array numbered 0 to 4 but your for loop uses 0 to 5

If you want all of the LEDs to flash at the same time then turn them all on, wait a bit, turn them off and wait a bit. Do not wait between changing the state of each LED

No UKHeliBob, if I change it to 4, then the last LED will be skipped and the rest will keep on chasing.

Hello UKHeliBob

UKHeliBob:
Added comments

int pins[5] = {2, 3, 4, 5, 6};  //array of pins with LEDs

void setup()
{
 for (int i = 0; i < 5; i++)  //should be 4 not 5
 {
   pinMode(pins, OUTPUT);
 }
}

void loop()
{
 for (int i = 0; i < 5; i++) //a counter from 0 to 5 - should be from 0 to 4
 {
   digitalWrite(pins[i], 1); //turn the the current pin HIGH
   delay(50);      //wait a bit
   digitalWrite(pins[i], 0); /turn teh current pin LOW
   delay(50);      //wait a bit
 }
} //now do it over again




Note that you have 5 pin numbers in the array numbered 0 to 4 but your for loop uses 0 to 5

"Note that you have 5 pin numbers in the array numbered 0 to 4 but your for loop uses 0 to 5"
?????
Regards,
bidouilleelec

Sorry for any confusion.

I am, of course wrong about the limit needing to be 4 rather than 5

May I know the best way to select many pins as an OUTPUT or INPUT without 'pinMode'ing every one of them?

Sangeeth_Satheesh:
May I know the best way to select many pins as an OUTPUT or INPUT without 'pinMode'ing every one of them?

See Port Manipulation but note that all the pins being manipulated must be on the same port

UKHeliBob:
See Port Manipulation but note that all the pins being manipulated must be on the same port

Ah..! isn't there any simple way for doing it.
Also, my pins do not blong to the same register.

isn't there any simple way for doing it.

A for loop iterating through an array of pins is the neatest way.

NOTE : pins default to INPUT so you never actually need to set the pinMode() for input pins unless you want to use INPUT_PULLUP

UKHeliBob:
A for loop iterating through an array of pins is the neatest way.

Sorry, I didn't understand that.
Could you make it clear please...

Have a look at LED exercise which is a similar but slightly different question to yours.

Sangeeth_Satheesh:
Sorry, I didn't understand that.
Could you make it clear please...

Sangeeth_Satheesh:
Sorry, I didn't understand that.
Could you make it clear please...

It is exactly what the code in the original post of this thread does

OK, so I just want to blink all the 5 LEDs together.
Could anybody suggest a suitable code for me?

Sangeeth_Satheesh:
OK, so I just want to blink all the 5 LEDs together.
Could anybody suggest a suitable code for me?

See reply #3