3

I'm new to Arduino so this may be a really silly doubt but I'm not finding an explanation for it anywhere online. I first uploaded this code (to blink 5 LEDs) on my Arduino Uno :

 void setup(){
 pinMode(12, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(8, OUTPUT);
}

void loop(){
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(1000);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(1000);
}

The 5 LEDs glowed brightly. But when I uploaded the following code (using an if statement to reduce the size of the previous one), the LEDs glowed dimly.

int LEDpin = 13;
void setup(){
pinMode(LEDpin, OUTPUT);
}

void loop(){
digitalWrite(LEDpin, HIGH);
delay(1000);
digitalWrite(LEDpin,LOW);
delay(1000);
if(LEDpin >= 8){
LEDpin--;
} else {
  LEDpin = 8;
  }
}

Also, when I just typed the if statement without the else part, nothing happened after the LED at pin 8 had glowed on and off. Can someone please tell me why the LEDs glowed dimly in the second one and why nothing glowed after LED 8 when I skipped the else part?

migrated from electronics.stackexchange.com Sep 16 '15 at 16:36

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

0

The problem with your code is that:

you have not initialised all the LED pins as output. Only pin 13 is initialised as output.

So, initialise all the pins to output. A correct code would look like this:

int LEDpin = 13;
void setup(){
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}

void loop(){
digitalWrite(LEDpin, HIGH);
delay(1000);
digitalWrite(LEDpin,LOW);
delay(1000);
if(LEDpin >= 8){
LEDpin--;
} else {
  LEDpin = 12;
  }
}
  • LEDpin needs to be assigned to 12 in both the initial declaration and in the else statement (and not 13 and 8 respectively). Otherwise, the first loop will try to write to pin 13 which is not initialised as an OUTPUT in setup(), and finally will only write to pin 8, instead of continuing to cycle from 12 to 8... – Greenonline Sep 16 '15 at 13:10
  • I see that you have (correctly) edited your answer for the else statement, however, the initial declaration of LEDpin is still set to 13, when it should be 12. So, the first line should be int LEDpin = 12; What will happen the first time the loop() is executed? – Greenonline Sep 16 '15 at 15:48
5

As Abhishek mentioned in his answer is correct, but the better way is to use for loops while dealing with many led's.

Let's say you have led's connected from pin 2 to 8.

int pin = 2;
void setup() 
{
   for (Pin = 2; Pin < 8; Pin++) 
   {
     pinMode(Pin, OUTPUT);
   } 
}
void loop() 
{
   for (int Pin = 2; Pin < 8; Pin++) 
   {
     digitalWrite(Pin, HIGH);
     delay(1000);
     digitalWrite(Pin, LOW);
     delay(1000);
    }
}
1

As Abhishek Jain explained, the pin's need to be initialised for OUTPUT.

However there is another error.

The code in loop() counts down to 8, once, flashing the LEDs, but then only lights an LED on pin 8 because it always goes through the else which maintains LEDpin's value as 8. LEDpin needs to be reset to 12, after it passes 8.

Better code might be

int LEDpin = 12;
void setup(){
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop(){
  digitalWrite(LEDpin, HIGH);
  delay(1000);
  digitalWrite(LEDpin,LOW);
  delay(1000);
  if(LEDpin >= 8){
    LEDpin--;
  } else {
    LEDpin = 12;   // <---- start with the first LED again
  }
}

For what its worth, I wouldn't do the code this way. I would write something using for loops, like:

const int firstLED = 8;
const int lastLED = 12;
int LEDpin;

void setup() {
  for (LEDpin = firstLED; LEDpin <= lastLED; ++LEDpin) {
     pinMode(LEDpin, OUTPUT);
  }
}

void loop() {
  for (LEDpin = firstLED; LEDpin <= lastLED; ++LEDpin) {
    digitalWrite(LEDpin, HIGH);
    delay(1000);
    digitalWrite(LEDpin,LOW);
    delay(1000);
  }
}
  • Initializing all the pins to OUTPUT solved my problem. Thank you! – ayerhs7 Sep 16 '15 at 6:59
  • 1
    @ayerhs7 - Initializing all the pins to OUTPUT is necessary, but not sufficient. If you want all of the LEDs to blink repeatedy, and not just once when the Arduino is reset, LEDpin must be reset to 13, which is missing from the answer that you have accepted. IMHO, for clarity, you should either update your answer to reflect the fact that you only need the LED on pin 8 to blink, or comment that the answer you have accepted does not solve the problem, or maybe explain why you accepted it. – gbulmer Sep 16 '15 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.