# I cant get the program to loop

**URL:** <https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530>\
**Category:** LEDs and Multiplexing\
**Created:** [September 11, 2021, 8:11pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530 "2021-09-11T20:11:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CircuitJunkie](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@CircuitJunkie](https://forum.arduino.cc/u/CircuitJunkie)\
**Post date:** [September 11, 2021, 8:11pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/1 "2021-09-11T20:11:54Z")

</div>

for some reason the program stops looping after turning the leds off in sequence.

```cpp

int ledArray = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int delayTime = 200;

void setup() {
//initalise ledArray as outputs
for (int i; i < 10; i++)
{
pinMode(ledArray[i], OUTPUT);
}
}

void loop() {
// turn leds on from 2-10.
for (int i; i <= 10; i++)
{
digitalWrite(ledArray[i], HIGH);
delay(delayTime);
}
// turn leds off from 10-2.
for (int i = 10; i >= 0; i--)
{
digitalWrite(ledArray[i], LOW);
delay(delayTime);
}
}

```

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [September 11, 2021, 8:24pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/2 "2021-09-11T20:24:49Z")

</div>

That's a lot of LEDs to run off an Arduino.

> [@CircuitJunkie](#):
>
> for (int i; i \< 10; i++)

What is the initial value of i in your loops?

Check out this post on for loops:  
[for - Arduino Reference](https://www.arduino.cc/reference/en/language/structure/control-structure/for/)

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [September 11, 2021, 8:30pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/3 "2021-09-11T20:30:31Z")

</div>

> [@CircuitJunkie](#):
>
> for (int i;

Oops

Please remember to use code tags when posting code

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 11, 2021, 8:34pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/4 "2021-09-11T20:34:36Z")

</div>

Hello  
Insert Serial.println(i); at POI to see what happens.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [September 11, 2021, 8:35pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/5 "2021-09-11T20:35:37Z")

</div>

> [@CircuitJunkie](#):
>
> i \<= 10

Double oops

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 11, 2021, 8:37pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/6 "2021-09-11T20:37:54Z")

</div>

OOPS² 🤓

---

<div class="post-metadata">

**Author:** ![CircuitJunkie](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@CircuitJunkie](https://forum.arduino.cc/u/CircuitJunkie)\
**Post date:** [September 11, 2021, 9:20pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/7 "2021-09-11T21:20:54Z")

</div>

The value of I increments starting at 0, 0 location pertains to location 0 which is 2, then 3, and so on to 11.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [September 11, 2021, 9:22pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/8 "2021-09-11T21:22:04Z")

</div>

> [@CircuitJunkie](#):
>
> The value of I increments starting at 0,

Have another guess

> [@CircuitJunkie](#):
>
> and so on to 11.

Oops

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [September 11, 2021, 9:46pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/9 "2021-09-11T21:46:29Z")

</div>

Compare your for loop to the for loop page that I sent you. There is a difference and it is at least part of your problem

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 11, 2021, 10:54pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/10 "2021-09-11T22:54:34Z")

</div>

![image](https://europe1.discourse-cdn.com/arduino/original/4X/a/d/2/ad28c201c6fed392b541f9a274647bc21e79403c.jpeg)

---

<div class="post-metadata">

**Author:** ![CircuitJunkie](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@CircuitJunkie](https://forum.arduino.cc/u/CircuitJunkie)\
**Post date:** [September 11, 2021, 11:14pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/11 "2021-09-11T23:14:23Z")

</div>

the initial value defaults to position 0 and in position 0 is output 2 according to int ledArray [] {2,3,4,5,6,7,8,9,10};  
0=2  
1=3  
2=4 and so on.

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [September 11, 2021, 11:17pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/12 "2021-09-11T23:17:04Z")

</div>

All right, you've told me what you think it is but the mistake is still in your code.  
To find it look very closely at the initializer portions of all three of your for loops.

---

<div class="post-metadata">

**Author:** ![CircuitJunkie](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@CircuitJunkie](https://forum.arduino.cc/u/CircuitJunkie)\
**Post date:** [September 11, 2021, 11:19pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/13 "2021-09-11T23:19:13Z")

</div>

how exactly does this oopsie daisy help in formatting a conclusion to the issue.

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [September 11, 2021, 11:20pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/14 "2021-09-11T23:20:38Z")

</div>

Try not to get frustrated. Focus on your issue.  
To be fair it's been pointed out by multiple users in multiple ways.  
It's just a couple easy to make mistakes

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 11, 2021, 11:20pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/15 "2021-09-11T23:20:49Z")

</div>

We have to have a little fun 😉

for (int i; i \<= 10; i++)

What have you instructed the compiler the starting value of **i** will be ?

![ForLoopIllustrated](https://europe1.discourse-cdn.com/arduino/original/4X/5/b/6/5b68d913cb4115cbc21f45b9463939a2cff1a42e.png)

---

<div class="post-metadata">

**Author:** ![CircuitJunkie](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@CircuitJunkie](https://forum.arduino.cc/u/CircuitJunkie)\
**Post date:** [September 11, 2021, 11:36pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/16 "2021-09-11T23:36:26Z")

</div>

thanks i found it the 0 missing in for (int i = 0; i \<= 10; i++).

Ithought i defults to 0 by itself.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 11, 2021, 11:51pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/17 "2021-09-11T23:51:12Z")

</div>

> [@CircuitJunkie](#):
>
> `ledArray = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}`

How many elements in your above array ?

* * *

```arduino

for (int i = 0; i <= 10; i++)
{
digitalWrite(ledArray[i], HIGH);
delay(delayTime);
}

```

How many elements are you accessing above ?

---

<div class="post-metadata">

**Author:** ![CircuitJunkie](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@CircuitJunkie](https://forum.arduino.cc/u/CircuitJunkie)\
**Post date:** [September 11, 2021, 11:54pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/18 "2021-09-11T23:54:57Z")

</div>

i am accessing 10

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 11, 2021, 11:57pm UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/19 "2021-09-11T23:57:32Z")

</div>

**for (int i = 0; i \<= 10; i++)**

0 1 2 3 4 5 6 7 8 9 10

🤔

11 ?

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [September 12, 2021, 12:00am UTC](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530/20 "2021-09-12T00:00:09Z")

</div>

Ordinal vs Cardinal  
If you start with 0 you have to stop \*\*\* before \*\*\* the length of the container.  
You currently go one step too far.  
Change your comparison to \<

[Next page](https://forum.arduino.cc/t/i-cant-get-the-program-to-loop/904530.md?page=2)
