Simple Array Usage

There has been a solution posted, but a simple introduction on for loops may help you understand how things are working.

for( init; condition; increment )
{
    ...
}

init allows you to initialize any variables that you may require for the loop. Usually this will involve something like i = 1.

condition lets you say what will keep the loop running. If condition is not met, the loop ends. A typical example is i < 20.

increment lets you modify variables that the loop depends on. Again, an example of this is i++.


Let's take a look at some of the code you've provided.

01.  cycle = 29; //This section blinks on LEDs then off, and waits to blink the next one
02.  for (cycle>28;cycle<34;cycle++){
03.    count = 13;
04.    for (count<14;count>-1;count--){
05.      digitalWrite(pinArray[count], HIGH);
06.      delay(timer*2);
07.      digitalWrite(pinArray[count], LOW);
08.      delay(timer*10);
09.    }
10  }

This is almost correct. I'll just point out a few things:

  • You don't really need line 1. This can be handled in your for() loop setup. (and you're already [almost] handling it anyway.
  • Change for (cycle>28; to for( cycle = 29;. This change will tell your for loop 'start this loop by setting the value of the variable cycle to 29.'
  • Similarly for lines 3 and 4, you don't need line 3, and start line 4 off with for( count = 13;.
  • A personal touch, instead of using count>-1; on line 4, you can use count >= 0;. I feel this is a nicer way to specify the same thing, as it's easier to read that 'this is what it's doing.' You also have the added benefit of eliminating problems that may occur with float numbers. For example, if you are counting down from 10.5, you go 10.5, 9.5, 8.5, ... 1.5, 0.5, -0.5, -1.5. It won't get picked up until it hits -1.5. So using count >= 0; is also a little more specific.

Add those minor changes throughout your project where applicable and you'll be laughing. :slight_smile: