LED sequence does not start (Code filled with explanatory comments)

I have built a circuit on my Arduino Mega 2560, with 8 LEDS with built in resistors plugged into digital ports 39, 41, 43, 45, 47, 49, 51, and 53. They are controlled by 8 Momentary Push Buttons plugged into digital ports 38, 40, 42, 44, 46, 48, 50, and 52 and the ground rail. I also have a piezo buzzer plugged into digital pin 8.

My code is as follows:

//This program has 8 LEDs and 8 buttons paired together in 8 Stations. At the start of the program there is 5 seconds for buttons to be pressed in a specific order, these are added to an array in that order. After those 5 seconds setup is doneand one of the selected stations is illuminated. When the current illuminated station's button is pressed, the next station is illuminated and this continues until the arduino is powered off.
const int LED1 = 39;
const int LED2 = 41;
const int LED3 = 43;
const int LED4 = 45;
const int LED5 = 47; 
const int LED6 = 49;
const int LED7 = 51;
const int LED8 = 53;
const int Button1 = 38;
const int Button2 = 40;
const int Button3 = 42;
const int Button4 = 44;
const int Button5 = 46;
const int Button6 = 48;
const int Button7 = 50;
const int Button8 = 52; //Button and LED pin definition
int AllButton[] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8};
int AllLED[] = {LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8}; //Arrays of all possible LEDs and Buttons
int AllowedButton[] = {0, 0, 0, 0, 0, 0, 0, 0};
int AllowedLED[] = {0, 0, 0, 0, 0, 0, 0, 0}; //Arrays to be modified to contain all used LEDs and Buttons in correct order
int CB, CL; //variables to track current button and LED
int count, prepcount; //variables to move through arrays
long seed; //seeding for random number generation
int RandomStart; //variable for determining random starting station from the allowed lists
bool prep = false; // variable for spliting between program setup and program execution in void loop

void setup() {
  long seed = analogRead(0) + 1024 * analogRead(1); //generate seed from 2 different analog pins to increase range of possible seeds
  randomSeed(seed); //use generated seed
  pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT); //define outputs
  pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
pinMode(Button7, INPUT_PULLUP);
pinMode(Button8, INPUT_PULLUP); //define inputs
  prepcount = 0; //intilizes array counter for setup
  tone(8, 50, 1000); //play tone  to indicate end of void setup and start of my program's setup
}

void loop() {
  if (prep == false) {
    if (digitalRead(AllButton[0]) == LOW) {
      AllowedButton[prepcount] = Button1;
      AllowedLED[prepcount] = LED1;
      prepcount++; //test if Button 1 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[1]) == LOW) {
      AllowedButton[prepcount] = Button2;
      AllowedLED[prepcount] = LED2;
      prepcount++; //test if Button 2 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[2]) == LOW) {
      AllowedButton[prepcount] = Button3;
      AllowedLED[prepcount] = LED3;
      prepcount++; //test if Button 3 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[3]) == LOW) {
      AllowedButton[prepcount] = Button4;
      AllowedLED[prepcount] = LED4;
      prepcount++; //test if Button 4 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[4]) == LOW) {
      AllowedButton[prepcount] = Button5;
      AllowedLED[prepcount] = LED5;
      prepcount++; //test if Button 5 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[5]) == LOW) {
      AllowedButton[prepcount] = Button6;
      AllowedLED[prepcount] = LED6;
      prepcount++; //test if Button 6 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[6]) == LOW) {
      AllowedButton[prepcount] = Button7;
      AllowedLED[prepcount] = LED7;
      prepcount++; //test if Button 7 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[7]) == LOW) {
      AllowedButton[prepcount] = Button8;
      AllowedLED[prepcount] = LED8;
      prepcount++; //test if Button 8 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    RandomStart = random(0, (sizeof(AllowedButton) / sizeof(AllowedButton[0]))); //pick random starting station from allowed stations
    CB = AllowedButton[RandomStart]; //set current button variable to decided starting point
    CL = AllowedLED[RandomStart]; //set current LED variable to decided starting point
    count = RandomStart; //intilizes array counter for program
  }
  if (millis() >= 5000 && prep == false) {//ends timer for setup of allowing specific stations for use in program
    digitalWrite(CL, HIGH); //illuminates the current station
    prep = true; //ends setup
    tone(8, 50, 1000); //plays tone to indicate setup is done
  }
  if (digitalRead(CB) == LOW && prep == true) {//actual program loop
    digitalWrite(CL, LOW);//when a button is pressed, turn that station's LED off
    count++; //increase array counter
    if ((count >= sizeof(AllowedButton) / sizeof(AllowedButton[0])) || (AllowedButton[count] == 0)) count = 0; //if the array counter has gone further than allowed stations reset to first station
    CB = AllowedButton[count]; //set current button to the next station
    CL = AllowedLED[count]; //set current LED to the next station
    digitalWrite(CL, HIGH); //set new Station's LED on
  }
}

The program correctly beeps at the start and end of setup, but it doesn't seem to correctly pick a random LED or illuminate any of the stations.

This is functional code, but it only works with all 8 buttons.

const int LED1 = 39, LED2 = 41, LED3 = 43, LED4 = 45, LED5 = 47, LED6 = 49, LED7 = 51, LED8 = 53, Button1 = 38, Button2 = 40, Button3 = 42, Button4 = 44, Button5 = 46, Button6 = 48, Button7 = 50, Button8 = 52, Speaker = 36;
int CurrentButton[] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8}, CurrentLED[] = {LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8};
int CB;
int CL;
int count;
int RandomStart;
long seed;

void setup() {
  long seed = analogRead(0) + 1024 * analogRead(1);
  randomSeed(seed);
  RandomStart = random(0, 8);
  //RandomStart=5;
  pinMode(LED1, OUTPUT), pinMode(LED2, OUTPUT), pinMode(LED3, OUTPUT), pinMode(LED4, OUTPUT), pinMode(LED5, OUTPUT), pinMode(LED6, OUTPUT), pinMode(LED7, OUTPUT), pinMode(LED8, OUTPUT);
  pinMode(Button1, INPUT_PULLUP), pinMode(Button2, INPUT_PULLUP), pinMode(Button3, INPUT_PULLUP), pinMode(Button4, INPUT_PULLUP), pinMode(Button5, INPUT_PULLUP), pinMode(Button6, INPUT_PULLUP), pinMode(Button7, INPUT_PULLUP), pinMode(Button8, INPUT_PULLUP);
  CB = CurrentButton[RandomStart];
  CL = CurrentLED[RandomStart];
  count = RandomStart;
  digitalWrite(CL, HIGH);
  tone(8, 50, 100);
}

void loop() {
  if (digitalRead(CB) == LOW) {
    digitalWrite(CL, LOW);
    count++;
    if (count >= sizeof(CurrentButton) / sizeof(CurrentButton[0])) count = 0;
    CB = CurrentButton[count];
    CL = CurrentLED[count];
    digitalWrite(CL, HIGH);
  }
}

Sorry Dracore, but trying to put all your code on one line (or two) just makes it too hard to digest without reformatting everything.

Now you have three threads about the same problem, why?

DKWatson:
Sorry Dracore, but trying to put all your code on one line (or two) just makes it too hard to digest without reformatting everything.

I apologize, that is simply my personal preference. If it helps, the only things that are consolidated are variable intilizations of the same type. It really shouldn’t affect any possible assistance with my problem as that all occurs after the consolidation.

But again, my apologies.

Whandall:
Now you have three threads about the same problem, why?

I have 3 separate threads because I have had 3 separate problems. And while they all occurred within the same program, if you check my posted code you can see the program has changed drastically between each post. Thus creating varieties of problems.

I could post them all in the same thread, but that wouldn’t be helpful for anybody in the future, as the 3 problems are all very different in nature.

Why did you stop responding over here?

Delta_G:
The thing you have to realize is how many people who could help you who will now just skip this thread because if you can't be bothered to make the code easy for me to read why should I be all that worried about fixing it for you. Have a little respect for the people you are asking for free help. If it is for YOU to read then format however you want. But as soon as you need someone else to read it you need to adopt standard formatting.

Again apologies, here is the corrected code:

//This program has 8 LEDs and 8 buttons paired together in 8 Stations. At the start of the program there is 5 seconds for buttons to be pressed in a specific order, these are added to an array in that order. After those 5 seconds setup is doneand one of the selected stations is illuminated. When the current illuminated station's button is pressed, the next station is illuminated and this continues until the arduino is powered off.
const int LED1 = 39;
const int LED2 = 41;
const int LED3 = 43;
const int LED4 = 45;
const int LED5 = 47; 
const int LED6 = 49;
const int LED7 = 51;
const int LED8 = 53;
const int Button1 = 38;
const int Button2 = 40;
const int Button3 = 42;
const int Button4 = 44;
const int Button5 = 46;
const int Button6 = 48;
const int Button7 = 50;
const int Button8 = 52; //Button and LED pin definition
int AllButton[] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8};
int AllLED[] = {LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8}; //Arrays of all possible LEDs and Buttons
int AllowedButton[] = {0, 0, 0, 0, 0, 0, 0, 0};
int AllowedLED[] = {0, 0, 0, 0, 0, 0, 0, 0}; //Arrays to be modified to contain all used LEDs and Buttons in correct order
int CB, CL; //variables to track current button and LED
int count, prepcount; //variables to move through arrays
long seed; //seeding for random number generation
int RandomStart; //variable for determining random starting station from the allowed lists
bool prep = false; // variable for spliting between program setup and program execution in void loop

void setup() {
  long seed = analogRead(0) + 1024 * analogRead(1); //generate seed from 2 different analog pins to increase range of possible seeds
  randomSeed(seed); //use generated seed
  pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT); //define outputs
  pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
pinMode(Button7, INPUT_PULLUP);
pinMode(Button8, INPUT_PULLUP); //define inputs
  prepcount = 0; //intilizes array counter for setup
  tone(8, 50, 1000); //play tone  to indicate end of void setup and start of my program's setup
}

void loop() {
  if (prep == false) {
    if (digitalRead(AllButton[0]) == LOW) {
      AllowedButton[prepcount] = Button1;
      AllowedLED[prepcount] = LED1;
      prepcount++; //test if Button 1 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[1]) == LOW) {
      AllowedButton[prepcount] = Button2;
      AllowedLED[prepcount] = LED2;
      prepcount++; //test if Button 2 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[2]) == LOW) {
      AllowedButton[prepcount] = Button3;
      AllowedLED[prepcount] = LED3;
      prepcount++; //test if Button 3 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[3]) == LOW) {
      AllowedButton[prepcount] = Button4;
      AllowedLED[prepcount] = LED4;
      prepcount++; //test if Button 4 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[4]) == LOW) {
      AllowedButton[prepcount] = Button5;
      AllowedLED[prepcount] = LED5;
      prepcount++; //test if Button 5 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[5]) == LOW) {
      AllowedButton[prepcount] = Button6;
      AllowedLED[prepcount] = LED6;
      prepcount++; //test if Button 6 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[6]) == LOW) {
      AllowedButton[prepcount] = Button7;
      AllowedLED[prepcount] = LED7;
      prepcount++; //test if Button 7 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    if (digitalRead(AllButton[7]) == LOW) {
      AllowedButton[prepcount] = Button8;
      AllowedLED[prepcount] = LED8;
      prepcount++; //test if Button 8 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }
    RandomStart = random(0, (sizeof(AllowedButton) / sizeof(AllowedButton[0]))); //pick random starting station from allowed stations
    CB = AllowedButton[RandomStart]; //set current button variable to decided starting point
    CL = AllowedLED[RandomStart]; //set current LED variable to decided starting point
    count = RandomStart; //intilizes array counter for program
  }
  if (millis() >= 5000 && prep == false) {//ends timer for setup of allowing specific stations for use in program
    digitalWrite(CL, HIGH); //illuminates the current station
    prep = true; //ends setup
    tone(8, 50, 1000); //plays tone to indicate setup is done
  }
  if (digitalRead(CB) == LOW && prep == true) {//actual program loop
    digitalWrite(CL, LOW);//when a button is pressed, turn that station's LED off
    count++; //increase array counter
    if ((count >= sizeof(AllowedButton) / sizeof(AllowedButton[0])) || (AllowedButton[count] == 0)) count = 0; //if the array counter has gone further than allowed stations reset to first station
    CB = AllowedButton[count]; //set current button to the next station
    CL = AllowedLED[count]; //set current LED to the next station
    digitalWrite(CL, HIGH); //set new Station's LED on
  }
}

I simply will never understand why it is the convention to add totally unnecessary lines to your program to do the same thing repeatedly.

septillion:
Why did you stop responding over here?

Sorry, I didn’t intend to leave you hanging but my code had continued to grow and improve past what was posted in that thread, making your very good assistance little to no help any longer. My code now only needs some single problem corrected instead of a complete overhaul like in that thread.

"I simply will never understand why it is the convention to add totally unnecessary lines to your program to do the same thing repeatedly."

For clarity and comprehension.

DKWatson:
"I simply will never understand why it is the convention to add totally unnecessary lines to your program to do the same thing repeatedly."

For clarity and comprehension.

At least personally, I find that I comprehend the code much faster, if I don’t have to scroll through a bunch of repeated text and it is just grouped with like variables on one line.

Not only does it save space but it communicates the simularity of those variables much faster.

But regardless this thread has moved so far off topic, it won’t be helpful for anybody.

To drag it back, my code (correctly formatted) was just posted a few replies back.

To drag it back, my code (correctly formatted) was just posted a few replies back.

You don't like scrolling, but you expect us to do that. Hmmm...

Dracore:
Sorry, I didn’t intend to leave you hanging but my code had continued to grow and improve past what was posted in that thread, making your very good assistance little to no help any longer.

Then that's you being rude for the help :wink: Aka, your problem. If you have a problem it's pretty stupid to let the program grow beyond repair...

Dracore:
My code now only needs some single problem corrected instead of a complete overhaul like in that thread.

Quick glance at it, I disagree... You still don't get the idea of arrays and the fact you can index over them. See blow.

Dracore:
I simply will never understand why it is the convention to add totally unnecessary lines to your program to do the same thing repeatedly.

No, you should not! If you feel you're repeating yourself you should stop and rethink what you're doing becaue there probably is an easier way :wink:

What you should do, is use clear (variable) names and comments But try not to repeat yourself in the comment if it comes to details. Otherwise you get stuff like this crap (not from here) if you start changing stuff:

digitalWrite(4, HIGH); //set pin 6 low

septillion:
What you should do, is use clear (variable) names and comments But try not to repeat yourself in the comment if it comes to details. Otherwise you get stuff like this crap (not from here) if you start changing stuff:

digitalWrite(4, HIGH); //set pin 6 low

I believe that all of my variable names are very clear, and every line of my code has a comment explaining its purpose.

That makes it so much easier.

You do realize that

RandomStart = random(0, (sizeof(AllowedButton) / sizeof(AllowedButton[0])));

is always going to pre-compile to

RandomStart = random(0,8);

DKWatson:
That makes it so much easier.

You do realize that

RandomStart = random(0, (sizeof(AllowedButton) / sizeof(AllowedButton[0])));

is always going to pre-compile to

RandomStart = random(0,8);

Oh dang it, yeah. I do not believe that is the source of my problem though because no LED illuminates.

Thank you though.

  pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT); //define outputs

or

for(byte b=0; b<sizeof(AllLED)/sizeof(AllLED[0]); b++)
{
   pinMode(AllLED[b], OUTPUT);
}

Which is less code? Which is easier to change if the number of LEDs changes?

 pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
pinMode(Button7, INPUT_PULLUP);
pinMode(Button8, INPUT_PULLUP); //define inputs

Another 8 lines of code, instead of 4.

If YOU don't like scrolling, don't make us scroll. Minimize your code whenever you can.

   if (digitalRead(AllButton[0]) == LOW) {
      AllowedButton[prepcount] = Button1;
      AllowedLED[prepcount] = LED1;
      prepcount++; //test if Button 1 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
    }

More code copied and pasted and edited, when a for loop would have made that copy/paste/edit cycle unnecessary.

So, when are you going to fix your code for real?

PaulS:
So, when are you going to fix your code for real?

I apologize, I am quite bad and uninformed at this. I reduced scrolling in the way that I knew how, but your way is definitely better, so I will work on attempting to convert it over.

Have you manage to find the actual error and not just my exceptionally poor formatting however?

I didn't say you're bad at adding comment :wink:

But I would NOT call CB and CL clear variable names. Might be clear for you know, but do note that the "psychopath that knows where you live" is simply future you :wink: And you can convince yourself very hard that will still be clear over 2 year / after 20 projects but the fact others have trouble with it now tells me it's not :wink:

Are you pressing a button within the first 5 seconds ?

I'm not sure what you intended to do.