4

I made some arduino code for leds that blink down a row and then back again, but when the program is run, there is an approximately 2 second delay in between the first for loop and the second. This is my code in the loop function:

int leds[] = {13, 12, 11};
int i;

void setup() {
  Serial.begin(9600);

  for (int i = 0; i < sizeof(leds); i ++) {
     pinMode(leds[i], OUTPUT);
  }
}

void loop() {
    for (i = 0; i < sizeof(leds); i ++) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
    for (i; i > 0; i --) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
}

It really makes the program look weird when the LEDs are blinking on a breadboard. Keep in mind that this only happens after the first for loop, the second is continuous back to the first. Any idea why this might be and how to fix it?

Minecat40
  • 43
  • 3

3 Answers3

5

The claim “The cause it that your 3rd led (2nd element in the array) is reapeted twice” is wrong.

Apparently that sentence is supposed to mean that the long delay is due to i being one too large, coming out of the first loop and entering the second one. Indeed, i is too large, but that's because each element of the array is a 2-byte int, so that instead of for (int i = 0; i < sizeof(leds); i ++) being equivalent to for (int i = 0; i < 3; i ++), it is equivalent to for (int i = 0; i < 6; i ++).

Thus, one second goes by with an extra 4 occurrences of delay(250); in the first loop, and another one second with 4 extra occurrences of delay(250); in the other loop.

To correct the problem, write sizeof leds/sizeof leds[0] instead of sizeof(leds), and write for (--i; i > 0; i--) instead of for (i; i > 0; i --).

James Waldby - jwpat7
  • 8,840
  • 3
  • 17
  • 32
1

The problem is on "for loops" condition,
Try this:

int leds[] = {13, 12, 11};
int i;

void setup() {
  Serial.begin(9600);

  for (int i = 0; i < (sizeof(leds)/sizeof(leds[0])); i ++) {
     pinMode(leds[i], OUTPUT);
  }
}

void loop() {
    for (i = 0; i < (sizeof(leds)/sizeof(leds[0]); i ++) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
    for (i=((sizeof(leds)/sizeof(leds[0])-1); i>=0; i --) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
}
duck
  • 1,258
  • 10
  • 27
  • This is not true. It depends on the persons intention. – Dat Ha Nov 10 '16 at 03:10
  • Well my friend, the "intention" is to blink a LED. (1) You turn on a LED, (2) put it on hold by using delay, (3) You turn the LED off, (4) loops, the LED is turned on again. Well, if by "blinking" you mean its turn off for less than 10 clocks instruction, I accept. – duck Nov 10 '16 at 03:18
  • 1
    You turn it off then change leds so there is a delay anyways. – Dat Ha Nov 10 '16 at 03:21
  • ah you are right. Sorry my bad. – duck Nov 10 '16 at 03:22
  • 2
    You mean sizeof(leds)/sizeof(leds[0]). – Edgar Bonet Nov 10 '16 at 08:58
  • wow thanks, another mistake. forget about the byte size – duck Nov 10 '16 at 08:59
  • Still wrong. Why don't you just const int LED_COUNT = 3;? – Edgar Bonet Nov 10 '16 at 09:02
  • well, maybe its personal preference to prepare "ifs..", maybe reducing or increasing the number LED. May i know why it still wrong? – duck Nov 10 '16 at 09:04
  • Because 1) you forgot to divide the first sizeof, 2) sizeof(int) is 2 on the Uno, not 8, 3) using an explicit integer instead of sizeof(leds[0]) makes the code non portable to, say, an Arduino Due. – Edgar Bonet Nov 10 '16 at 09:27
-1

The cause it that your 3rd led (2nd element in the array) is repeated twice.

Try this instead:

int leds[3] = {13, 12, 11};

void setup() {   
  for (int i = 0; i < 3; i++) {
    pinMode(leds[i], OUTPUT);
  }
}

void loop() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(leds[i], HIGH);
    delay(250);
    digitalWrite(leds[i], LOW);
  }
  for (int i = 2; i > 0; i--) {
    digitalWrite(leds[i], HIGH);
    delay(250);
    digitalWrite(leds[i], LOW);
  }
}
Dat Ha
  • 2,913
  • 6
  • 23
  • 45