simple blink question

Hi, I'm new to ardu and in general to the micro controllers.
I've purchased Arduino Mega 2560
and I've started to play with the "Blink"
I've encountered a small problem, not sure if it is my approach to the programming,
or what....?

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
int led = 5;  //init for pin 5 connected led, leds also connected 6&7
int i = 5;     //counter for "for" loop

// the setup routine runs once when you press reset:

void setup() {                
  // initialize the digital pin as an output.
  i=5;
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  
  for(i=5;i<8;i++){
  led=i; 
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  // wait for a second
  if (i==8){
    i=5;
  }
  }  
}

what i wanted to get is 3 LED's connected to the pins 5,6,7 to light up, one after another.
And LED on pin 5 works ok, but 6 and 7 are very, very, dim. they are tested and working the same way if i'll swap their places.

Please advise.

Pins are, by default INPUT. Input pins are very dim when you connect an LED to them. Make them OUTPUT, and they will be brighter.

Note that in the for loop(), the body will not be executed when i is 8 or more, so the if test for i = 8 will never be true.

The loop function is called in an endless loop, so diddling with the for loop index that way is not a good idea.

You have to initialize pin 7 and 8 as output too:
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

@Delta:
No resistors.

@Paul:
Ahh yeah initially I've had more led'S But cut out on the elements to simplyfy and chase the error,
But if I do in the Setup loop:

pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

Should do the trick.

Thank You guys for answers.
I'll try that 2morrow, its 3:30am And its time to Sleep :slight_smile:

betelgeuze:
You have to initialize pin 7 and 8 as output too:
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

Ahh Thank You,
Been Typing when You Answered :wink:

he3r0:
@Delta:
No resistors.

Recipe for disaster.

Arrch:

he3r0:
@Delta:
No resistors.

Recipe for disaster.

care to elaborate? even in tutorial there is LED stuck in pin 13 without any resistor, I've just added couple more LED's...

even in tutorial there is LED stuck in pin 13 without any resistor

That doesn't make it right. We've been trying for years to get the team to fix that.

An LED draws as much current as it can. That can damage the Arduino. A current limiting resistor limits how much current the LED can draw, protecting the Arduino.

Use them or plan on replacing the Arduino when the pin quits working because it has been abused too often. Your choice.

Here's something to consider.

Your code only works if the LEDs are attached to sequentially numbered pins, which lacks flexibility. Here I've held the pin numbers in an array and then I can just process the array. With this approach the LEDs do not have to be on sequential pins and another LED could be added easily by adding it's pin number to the ledPin array and changing MAX_PINS to suit.

I did not compile or otherwise test the following:

byte ledPin[] = {5, 6, 7}         // array of pin numbers
define MAX_PINS 3                // change to match number of pins used

void setup(){
     for (byte i = 0; i < MAX_PINS; i++)
     {
          pinMode(ledPin[i], OUTPUT);     // set up all the pins as output
     }
}

void loop(){
     for (byte i = 0; i < MAX_PINS; i++)       // blink each led separately one after the other
     {
            digitalWrite(ledPin[i], HIGH);  
            delay(1000);               
            digitalWrite(ledPin[i], LOW);  
            delay(1000);  
     }
}

PaulS:

even in tutorial there is LED stuck in pin 13 without any resistor

That doesn't make it right. We've been trying for years to get the team to fix that.

An LED draws as much current as it can. That can damage the Arduino. A current limiting resistor limits how much current the LED can draw, protecting the Arduino.

Use them or plan on replacing the Arduino when the pin quits working because it has been abused too often. Your choice.

Sounds logical, will do. Thanks for explanation.