# ARRAY PROJECT NOT WORKING

**URL:** <https://forum.arduino.cc/t/array-project-not-working/665115>\
**Category:** Programming\
**Created:** [July 11, 2020, 10:45pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115 "2020-07-11T22:45:11Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Billyprps](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@Billyprps](https://forum.arduino.cc/u/Billyprps)\
**Post date:** [July 11, 2020, 10:45pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/1 "2020-07-11T22:45:11Z")

</div>

Hello, I'm trying to learn about arrays and the code I'm using won't work. The error message says, "i not declared in this scope" but it looks like it is. I also added semicolons after the for function brackets even though the tutorial I'm following left them out. here is my code.

```cpp
 int ledArray [] = { 2,3,4,5,6,7,8,9 };
  int delayTime = 150;

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

void loop() {
  for(int i; i <= 7, i++);
    {
      digitalWrite(ledArray[i], HIGH);
      delay(delayTime);
    }

  for(int i = 7; i >= 0; i--);
    {
      digitalWrite(ledArray[i], LOW);
      delay (delayTime);
    }
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 11, 2020, 10:48pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/2 "2020-07-11T22:48:09Z")

</div>

Lose the semicolons after the for(). It says, "do nothing". Since you didn't post the code that has the problem you mention, please post the correct code. Before you do that, please read the sticky posts at the top of the forum about how to post code (especially the part about using code tags).

---

<div class="post-metadata">

**Author:** ![Billyprps](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@Billyprps](https://forum.arduino.cc/u/Billyprps)\
**Post date:** [July 11, 2020, 10:51pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/3 "2020-07-11T22:51:31Z")

</div>

Thanks. The "i" in the square brackets didn't paste either. I'll edit the post.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 11, 2020, 11:03pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/4 "2020-07-11T23:03:46Z")

</div>

Oops. You only have 8 elements in the array!

```cpp
 for(int i; i < 10 ; i++);

```

Also shouldn't you give 'i' some initial value?

---

<div class="post-metadata">

**Author:** ![Billyprps](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@Billyprps](https://forum.arduino.cc/u/Billyprps)\
**Post date:** [July 11, 2020, 11:06pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/5 "2020-07-11T23:06:32Z")

</div>

Aaaand I figured it out. used commas instead of semicolons inside my for function.

Thanks

---

<div class="post-metadata">

**Author:** ![Billyprps](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@Billyprps](https://forum.arduino.cc/u/Billyprps)\
**Post date:** [July 11, 2020, 11:17pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/6 "2020-07-11T23:17:18Z")

</div>

i is initiating at 0, so that should be fine. The weird thing now is that the Loop isn't looping. It runs through LEDs up then down once and stops. Any idea why it wouldn't loop? here's the working code.

```cpp
 int ledArray [] = { 2,3,4,5,6,7,8,9 };
  int delayTime = 150;

void setup() {
  for(int i; i < 9 ; i++)
  {
    pinMode(ledArray[i], OUTPUT);
  }
  
}

void loop() {
  for(int i; i <= 7; i++)
    {
      digitalWrite(ledArray[i], HIGH);
      delay(delayTime);
    }

  for(int i = 7; i >= 0; i--)
    {
      digitalWrite(ledArray[i], LOW);
      delay (delayTime);
    }
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 11, 2020, 11:20pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/7 "2020-07-11T23:20:49Z")

</div>

> Billyprps:  
> i is initiating at 0

Perhaps it is, but it is bad form to not initialize it explicitly.

You still haven't fixed this:

```arduino
  for(int i; i < 9 ; i++)
  {
    pinMode(ledArray[i], OUTPUT);

```

That initializes 9 elements - 0 to 8

---

<div class="post-metadata">

**Author:** ![Billyprps](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@Billyprps](https://forum.arduino.cc/u/Billyprps)\
**Post date:** [July 11, 2020, 11:25pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/8 "2020-07-11T23:25:15Z")

</div>

Doesn’t that mean it increments the counter as long as it’s less than 9 and never gets to 9?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 11, 2020, 11:27pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/9 "2020-07-11T23:27:08Z")

</div>

Really? 🙂 You don't believe me? !!! Count the numbers between zero and eight, inclusive.

---

<div class="post-metadata">

**Author:** ![Billyprps](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@Billyprps](https://forum.arduino.cc/u/Billyprps)\
**Post date:** [July 11, 2020, 11:30pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/10 "2020-07-11T23:30:28Z")

</div>

I believe you. I changed it to 8... It still doesn’t loop though. Only cycles up and down once.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 11, 2020, 11:34pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/11 "2020-07-11T23:34:39Z")

</div>

Try serial debug prints of 'i'

---

<div class="post-metadata">

**Author:** ![pcbbc](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@pcbbc](https://forum.arduino.cc/u/pcbbc)\
**Post date:** [July 11, 2020, 11:40pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/12 "2020-07-11T23:40:05Z")

</div>

Variable “i” (unless it is global - which it isn’t) is stored on the stack as a local variable (or maybe in a register if the compiler so chooses).

Local variables are left un-initialised. They are basically junk random data left over from whatever was on the stack (or in the register) previously.

You really need to set i = 0 at the start of the for and stop assuming it happens automatically.

Sometimes you may be lucky and that is simply the case by random accident. The vast majority of times probably it will not. There certainly are no guarantees.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 11, 2020, 11:43pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/13 "2020-07-11T23:43:27Z")

</div>

I tried to tell you. When you see something that everyone does, think long and hard about why and do some research, before ignoring it.

---

<div class="post-metadata">

**Author:** ![Billyprps](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@Billyprps](https://forum.arduino.cc/u/Billyprps)\
**Post date:** [July 11, 2020, 11:58pm UTC](https://forum.arduino.cc/t/array-project-not-working/665115/14 "2020-07-11T23:58:05Z")

</div>

First I learned about the serial println debugging and did that. It looked like it was stuck int the digitalWrite...LOW part. Then when I came back here I saw more talking about declaring i as zero instead of assuming. Changed that and it's now looping properly.

Thanks again for the help!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:38am UTC](https://forum.arduino.cc/t/array-project-not-working/665115/15 "2021-05-06T00:38:41Z")

</div>

Closed on May 6, 2021, 12:38am UTC


