# 2 seperate LED chasing sequence question

I am trying to program an UNO to run 2 separate LED chasing sequences. One chase sequence has 5 LED’s and chases all the time. The other chase sequence only runs on a push of a button and runs while the button is pushed. I have the LED’s connected for one chase on pins 2-6 and the pins for the other chase on 8-12 and the button is on 7. Currently my programming is causing one chase of lights, the 2-6 to chase out from the middle starting on the number 4 pin. Pins 8-12 are always chasing, the button does nothing. I’ve copied some code trying to get it to work, so I do not know what everything in my code is doing exactly, just the general idea.

Can I please get help in fixing my program. I think I need to use array’s and the millis function, but I’m having trouble figuring it out.

Thanks for helping out the rookie!

``````// Programming for 2 different 5 LED chasing sequences. One 5 LED chasing sequence will always run.
//The other 5 LED chasing sequence will run while the button is pushed.

int ledPins[] = {2, 3, 4, 5, 6};       // an array of pin numbers to which always on LEDs are attached
int allPins[] = {2, 3, 4, 5, 6, 8, 9, 10, 11, 12};   //the array of pin numbers that go on when the button is pushed.
int pinCount = 6;           // the number of pins (i.e. the length of the array), I'm not sure what this is really doing
int button = 7;            //Pin location for the button

long sequenceDelay = 200;  //length of time between the LED's lighting
long flashDelay = 500; //length of time for lighting of LED's

boolean LED2state = false;
boolean LED3state = false;
boolean LED4state = false;
boolean LED5state = false;
boolean LED6state = false;
boolean LED8state = false;
boolean LED9state = false;     // the LED will turn ON in the first iteration of loop()
boolean LED10state = false;     // need to seed the light to be OFF
boolean LED11state = false;
boolean LED12state = false;

long waitUntil2 = 0; //start point for the waitUntil variables for the lighting sequence
long waitUntil3 = sequenceDelay;         // the seed will determine time
long waitUntil4 = sequenceDelay + sequenceDelay;
long waitUntil5 = sequenceDelay + sequenceDelay + sequenceDelay;
long waitUntil6 = sequenceDelay + sequenceDelay + sequenceDelay + sequenceDelay;
long waitUntil8 = 0;
long waitUntil9 = sequenceDelay;
long waitUntil10 = sequenceDelay + sequenceDelay;
long waitUntil11 = sequenceDelay + sequenceDelay + sequenceDelay;
long waitUntil12 = sequenceDelay + sequenceDelay + sequenceDelay + sequenceDelay;

void setup() {
pinMode(button, INPUT); //sets the button's mode

int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
pinMode(ledPins[thisPin], OUTPUT);
}

for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
pinMode(allPins[thisPin], OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest:
while (button == LOW); //while button is not pushed pins 2-6 light in a chasing sequence
digitalWrite(2, LED2state);
digitalWrite(3, LED3state);
digitalWrite(4, LED4state);
digitalWrite(5, LED5state);
digitalWrite(6, LED6state);

if (millis() >= waitUntil2) {
LED2state = !(LED2state);
waitUntil2 = millis() +flashDelay;
}
if (millis() >= waitUntil3) {
LED3state = !(LED2state);
waitUntil3 = millis() +flashDelay;
}
if (millis() >= waitUntil4) {
LED4state = !(LED4state);
waitUntil4 = millis() +flashDelay;
}
if (millis() >= waitUntil5) {
LED5state = !(LED5state);
waitUntil5 = millis() +flashDelay;
}
if (millis() >= waitUntil6) {
LED6state = !(LED6state);
waitUntil6 = millis() +flashDelay;
}

while(button == HIGH)  // when the button is pushed it lights all lights in a 5 LED chase sequence.

digitalWrite(2, LED2state);
digitalWrite(3, LED3state);
digitalWrite(4, LED4state);
digitalWrite(5, LED5state);
digitalWrite(6, LED6state);
digitalWrite(8, LED8state);
digitalWrite(9, LED9state);
digitalWrite(10, LED10state);
digitalWrite(11, LED11state);
digitalWrite(12, LED12state);

if (millis() >= waitUntil2) {
LED2state = !(LED2state);
waitUntil2 = millis() +flashDelay;
}
if (millis() >= waitUntil3) {
LED3state = !(LED2state);
waitUntil3 = millis() +flashDelay;
}
if (millis() >= waitUntil4) {
LED4state = !(LED4state);
waitUntil4 = millis() +flashDelay;
}
if (millis() >= waitUntil5) {
LED5state = !(LED5state);
waitUntil5 = millis() +flashDelay;
}
if (millis() >= waitUntil6) {
LED6state = !(LED6state);
waitUntil6 = millis() +flashDelay;
}

if (millis() >= waitUntil8) {
LED8state = !(LED8state);
waitUntil8 = millis() +flashDelay;
}
if (millis() >= waitUntil9) {
LED9state = !(LED9state);
waitUntil9 = millis() +flashDelay;
}
if (millis() >= waitUntil10) {
LED10state = !(LED10state);
waitUntil10 = millis() +flashDelay;
}
if (millis() >= waitUntil11) {
LED11state = !(LED11state);
waitUntil11 = millis() +flashDelay;
}
if (millis() >= waitUntil12) {
LED12state = !(LED12state);
waitUntil12 = millis() +flashDelay;
}
}
``````
``````  while(button == HIGH)  // when the button is pushed it lights all lights in a 5 LED chase sequence.

digitalWrite(2, LED2state);
digitalWrite(3, LED3state);
digitalWrite(4, LED4state);
``````

The auto format tool is a great way of pointing out errors.

``````while (button == LOW);
``````

Eliminating (possibly) excess semicolons is another.

Arrays would shorten your code considerably.

``````int ledPins[] = {
2, 3, 4, 5, 6};       // an array of pin numbers to which always on LEDs are attached
int allPins[] = {
2, 3, 4, 5, 6, 8, 9, 10, 11, 12};   //the array of pin numbers that go on when the button is pushed.
int pinCount = 6;           // the number of pins (i.e. the length of the array), I'm not sure what this is really doing
``````

There are not 6 numbers in either array, so I don't know that is really doing either.

You can let the compiler work it out, eg.

``````#define NUMITEMS(x) (sizeof(x) / sizeof(x[0]))
``````

Then:

``````int pinCount = NUMITEMS (allPins);           // the number of pins (i.e. the length of the array)
``````