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?

asked Nov 10, 2016 at 2:00
2
  • Please include the FULL code. Yes, including the setup even though it is not really relevant for you. Commented Nov 10, 2016 at 2:01
  • Alright, I edited it. Does that help? Commented Nov 10, 2016 at 2:04

3 Answers 3

-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);
 }
}
answered Nov 10, 2016 at 2:11
1
  • Thank you! I thought that I wouldn't have to decrement the length of the array, guess I was wrong. Commented Nov 10, 2016 at 2:20
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 --).

answered Nov 10, 2016 at 2:36
0
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);
 }
}
answered Nov 10, 2016 at 2:53
9
  • This is not true. It depends on the persons intention. Commented Nov 10, 2016 at 3: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. Commented Nov 10, 2016 at 3:18
  • 1
    You turn it off then change leds so there is a delay anyways. Commented Nov 10, 2016 at 3:21
  • ah you are right. Sorry my bad. Commented Nov 10, 2016 at 3:22
  • 2
    You mean sizeof(leds)/sizeof(leds[0]). Commented Nov 10, 2016 at 8:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.