Randomize a output (led) after a input (button)

Hello my name is Jeremy I am self taught and never went to school. Pretty much a jack of all trades and I just learned about the raspberry pi 2 weeks ago came up with a project that needed more I/O so now I have the arduino mega.

As of January of this year I started building a Trampoline park. I build everything myself. After seeing the game speed of light Speed of Light Jackpot 999 - YouTube . I got the idea to do this while jumping on a Trampoline. So some buttons may be low and some high. Basically a great way to make the Trampoline a true work out.

So my order of operation would be like this.

Start button is pushed
First random button with light comes on
They have 5 seconds to hit this button or game over.
If button is pressed another random light/button will appear and they will have 5 seconds.
I have currently 16 buttons.

I started with the standard how to light a led with a button and got that part all figured out. I know I need to do a seed or a array but I have no clue what so ever on how to do that. So here is where I was I did leave the test button/light just because I was testing it and playing with delay and other things. Question is how do I proceed from here?

At some point ill get into over head lights and possible sound with it. Say when the game starts it gets bright. When you fail it goes red. I'll do this just by colored lights cause I already have them.

Maybe start of game add a camera so people can see from the outside and finally a score. How many buttons pushed before failure.

I know I'm far away but any help would be appreciated.

Could you maybe modify your post so all lines of the sketch are within just 2 code-tags ?
as for arrays you can do

int ledpins[] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53}

and let that follow in setup by

for (int i = 0; i <16; i++) {
  pinmode(ledpins[i], OUTPUT);
}

But you can read about such things in standard C++ textbook or in the Examples that are included in the IDE.

You could use the randomness of a humanoid life form pressing a button.

Set up a variable to hold a number from 0-15. Every pass through loop() increment this number. When it gets to 16 reset it to zero. When a button press is sensed grab the current number and use it to turn on an LED or do whatever.

void setup() 
{
  // put your setup code here, to run once:
  randomSeed(analogRead(0));
  for (int i=0;i<15;i++)
  {
    // use your switches to connect input with GND default their input value is 1 if not connected:   
    pinMode(22+i*2,INPUT_PULLUP); 
    // 16 LED's or whatever, these pins have to be defined as outputs:
    pinMode(23+i*2,OUTPUT);       
  }
}

// the score:
int score=0;

void loop() 
{
  Serial.begin(9600); // look at the score in the Arduino Output window (menu tools / serial monitor).
  // random number
  int key=random(0,15);
  // activtate LED
  for (int i=0;i<15;i++)
  {
    if (i==key) 
      digitalWrite(23+i*2,HIGH);
    else  
      digitalWrite(23+i*2,LOW);
  }
  // read key
  int choice=-1;
  unsigned long millisRef=millis();
  // cycle through loop until 5000 ms = 5sec are gone or button is pressed:  
  for (int i=0;(i<15) && (choice==-1) && (millis()-millisRef<5000);i++)
  {
    if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
      choice=i; // remember button pressed:
  }
  if (choice==key) // right button.
  {
      score++; // increase score
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
  }
  else
  {
    score=0;
    Serial.print("Wrong key or timeout; score=");
    Serial.println(score);
  }
}

This program should get you started, try to understand each line and look it up with Google or a good textbook.
I think this should get you started.
Using a for loop can reduce your code drastically.
Beware of the use of inputs that are not connected to 5V or ground, they will create random input values, better is to use INPUT_PULLUP, then the input is connected to the 5V in the Arduino processor and by connecting the pin to GND you get a stable signal.

Best regards,
Johi.

JOHI:

void setup() 

{
 // put your setup code here, to run once:
 randomSeed(analogRead(0));
 for (int i=0;i<15;i++)
 {
   // use your switches to connect input with GND default their input value is 1 if not connected:  
   pinMode(22+i2,INPUT_PULLUP);
   // 16 LED's or whatever, these pins have to be defined as outputs:
   pinMode(23+i
2,OUTPUT);      
 }
}

// the score:
int score=0;

void loop()
{
 Serial.begin(9600); // look at the score in the Arduino Output window (menu tools / serial monitor).
 // random number
 int key=random(0,15);
 // activtate LED
 for (int i=0;i<15;i++)
 {
   if (i==key)
     digitalWrite(23+i2,HIGH);
   else  
     digitalWrite(23+i
2,LOW);
 }
 // read key
 int choice=-1;
 unsigned long millisRef=millis();
 // cycle through loop until 5000 ms = 5sec are gone or button is pressed:  
 for (int i=0;(i<15) && (choice==-1) && (millis()-millisRef<5000);i++)
 {
   if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
     choice=i; // remember button pressed:
 }
 if (choice==key) // right button.
 {
     score++; // increase score
     Serial.print("OK, score=");
     Serial.println(score); // print score in Arduino output Window.
 }
 else
 {
   score=0;
   Serial.print("Wrong key or timeout; score=");
   Serial.println(score);
 }
}



This program should get you started, try to understand each line and look it up with Google or a good textbook.
I think this should get you started.
Using a for loop can reduce your code drastically.
Beware of the use of inputs that are not connected to 5V or ground, they will create random input values, better is to use INPUT_PULLUP, then the input is connected to the 5V in the Arduino processor and by connecting the pin to GND you get a stable signal.

Best regards,
Johi.
www.SylvesterSolutions.com

First of off I want to say thanks Johi. So I've played with the code and I've got it up and running. The only issue I have with it is the millis. Ive tried researching it but haven't had no luck. I can make the value as small as 1 and as big as 50000000000 and no change. So I know that means its either not in the right spot or something. I've tried to figure it out. But I have verified all my leds are firing and if I can get a press to match it does get a score.

void setup()
{
  Serial.begin(9600); // look at the score in the Arduino Output window (menu tools / serial monitor).
  // put your setup code here, to run once:
  randomSeed(analogRead(0));
  for (int i=0;i<15;i++)
  {
    // use your switches to connect input with GND default their input value is 1 if not connected:   
    pinMode(22+i*2,INPUT_PULLUP);
    // 16 LED's or whatever, these pins have to be defined as outputs:
    pinMode(23+i*2,OUTPUT);       
  }
}

// the score:
int score=0;

void loop()
{
  // random number
  int key=random(0,15);
  // activtate LED
  for (int i=0;i<15;i++)
  {
    if (i==key)
      digitalWrite(23+i*2,HIGH);
    else  
      digitalWrite(23+i*2,LOW);
  }
  // read key
  int choice=-1;
  unsigned long millisRef=millis();
  // cycle through loop until 5000 ms = 5sec are gone or button is pressed:  
  for (int i=0;(i<15) && (choice==-1) && (millis()-millisRef<5000);i++)
  {
    if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
      choice=i; // remember button pressed:
  }
  if (choice==key) // right button.
  {
      score++; // increase score
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
  }
  else
  {
    score=0;
    Serial.print("Wrong key or timeout; score=");
    Serial.println(score);
  }
}

for (int i=0;i<15;i++)This only loops 15 times, didn't you want 16 times ?

for (int i=0;(i<15) && (choice==-1) && (millis()-millisRef<5000);i++)
  {
    if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
      choice=i; // remember button pressed:
  }

this loops as long as all conditions are true. The first condition fails first i guess after a full cycle through polling the pins.
how about

while ((choice==-1) && (millis()-millisRef<5000)) {
  for (int i=0;i<15;i++)
    {
      if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
        choice=i; // remember button pressed:
    }
  }

You need to create a nested loop

Thank you Deva. As far as wanting 16 I really want 20 but there is only 16 on that spot of the mega so i was just starting there. So I didn't realize how the cpu board knew what pins was what but now realize the 22+i*2 is telling it ever other pin. So since we are starting on #1 (pin22) is it considered 15 because it's 15 additional or is it 16 counting the pin thats starting the string?

anyways so it appears its working now I gotta get all the other lights wired up so I can do this in real time effort. But one issue I have having is I believe I need a Delay? after the light coming on and I hit the button when looking at the serial string I'm often getting 5 or so wrong clicks with the good click.
So am I thinking correctly? If we add in a delay after a button click it would eliminate it from seeing wrong button and starting the score back over? I tried adding in a delay (100); and it just didn't work at all lol.
06:23:02.991 -> OK, score=1
06:23:03.038 -> Wrong key or timeout; score=0
06:23:03.038 -> Wrong key or timeout; score=0
06:23:03.085 -> Wrong key or timeout; score=0
06:23:03.132 -> Wrong key or timeout; score=0
06:23:03.132 -> OK, score=1
06:23:03.179 -> Wrong key or timeout; score=0
06:23:08.125 -> Wrong key or timeout; score=0

void setup()
{
  Serial.begin(9600); // look at the score in the Arduino Output window (menu tools / serial monitor).
  // put your setup code here, to run once:
  randomSeed(analogRead(0));
  for (int i=0;i<16;i++)
  {
    // use your switches to connect input with GND default their input value is 1 if not connected:   
    pinMode(22+i*2,INPUT_PULLUP);
    // 16 LED's or whatever, these pins have to be defined as outputs:
    pinMode(23+i*2,OUTPUT);       
  }
}

// the score:
int score=0;

void loop()
{
  // random number
  int key=random(0,15);
  // activtate LED
  for (int i=0;i<16;i++)
  {
    if (i==key)
      digitalWrite(+i*2,HIGH);
    else  
      digitalWrite(23+i*2,LOW);
  }
  // read key
  int choice=-1;
  unsigned long millisRef=millis();
  // cycle through loop until 5000 ms = 5sec are gone or button is pressed:
  while ((choice==-1) && (millis()-millisRef<5000))  {  
  for (int i=0;i<16;i++)
  {
    if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
      choice=i; // remember button pressed:
  }
  }
  if (choice==key) // right button.
  {
      score++; // increase score
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
  }
  else
  {
    score=0;
    Serial.print("Wrong key or timeout; score=");
    Serial.println(score);
  }
}

Now that I'm thinking about this it would be best to setup a button to function as a single pulse even if you hold the button. How do you do that or what is it called? I don't mind doing the leg work its just nice knowing what direction to walk.

As far as wanting 16 I really want 20 but there is only 16 on that spot of the mega so i was just starting there.

There is more ways to connect multi buttons and leds than just 1 per pin.

So since we are starting on #1 (pin22)

we start at #0 !
Yes i suppose you do need a debounce function. You anyway need a debounce function simply because a pressing a button may get registered as pressing and releasing a button and pressing it again (static and all sorts of reasons)
Please remember to indent your code properly (ctrl-t) before posting.
It makes the most sense to hold up the sketch until all buttons are released at the end of loop (if choice is not -1) so something like

if (choice != -1) {
  bool bpress = true;
  while (bpress) { 
    bpress = false;
    for (int i=0;i<16;i++)
      {
         if (digitalRead(22+i*2)==0)  bpress = true; 
      }
    }
  }

Wack away ! (i am reminded of a black mirror episode now...)

Deva you are totally awesome!!!! So I've managed to get everything going I have the random light and the score is happening and what not. The only thing left I believe is a couple small things.

I'm gonna charge just 25 cents per play so I want this to run as a program. So I want it to start from a button press, give a count down 5, 4, 3, 2, 1 then first led lights up, and then on the event they either make a wrong button press or they time out I want it to end on it's own.

and at that point I want the score to stay. Currently right now as soon as you lose it goes back to 0. So just those last few tweaks and I think I'll be good. I was trying to do it but I failed lol. I'm sure I'm close. So here's the code with the added details for it to start and stop and you can tell me what I was doing wrong lol.

Thanks in advance!!!!! and don't worry when I get done I'll post the final product. I'm sure people ask for help all the time and people who help probably never get to see what they helped with.

int run;
int buttonPin;

void setup()
{
  run = 0; //starts stopped
   buttonPin = 2; //whatever pin your button is plugged into

   pinMode(buttonPin, INPUT_PULLUP);

  Serial.begin(9600); // look at the score in the Arduino Output window (menu tools / serial monitor).
  // put your setup code here, to run once:
  randomSeed(analogRead(0));
  for (int i=0;i<16;i++)
  {
    // use your switches to connect input with GND default their input value is 1 if not connected:   
    pinMode(22+i*2,INPUT_PULLUP);
    // 16 LED's or whatever, these pins have to be defined as outputs:
    pinMode(23+i*2,OUTPUT);       
  }
}

// the score:
int score=0;

void loop()
    //code you always run here; you can leave this section blank if you want the entire program to stop and start, or add code here if you want it to always run

  //check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
{  if(digitalRead(buttonPin) == LOW) //funcitons based off of button pulling input pin LOW
  {
     if(run == 0)
     {
         run = 255;
     }
     else
     {
         run = 0;
     }
  }

  if(run > 0)
  {
     //code you only run if button was pressed, stops running when button pressed again, so forth...
  }
  // random number
  int key=random(0,15);
  // activtate LED
  for (int i=0;i<16;i++)
  {
    if (i==key)
      digitalWrite(23+i*2,HIGH);
    else  
      digitalWrite(23+i*2,LOW);
  }
  // read key
  int choice=-1;
  unsigned long millisRef=millis();
  // cycle through loop until 5000 ms = 5sec are gone or button is pressed:
  while ((choice==-1) && (millis()-millisRef<1000))  {  
  for (int i=0;i<16;i++)
  {
    if (choice != -1) {
  bool bpress = true;
  while (bpress) {
    bpress = false;
    for (int i=0;i<16;i++)
      {
         if (digitalRead(22+i*2)==0)  bpress = true;
      }
    }
  }
    if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
      choice=i; // remember button pressed:
  }
  }
  if (choice==key) // right button.
  {
      score++; // increase score
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
  }
  else
  {
    score=0;
    Serial.print("Wrong key or timeout; score=");
    Serial.println(score);
  }
}

I was just thinking I know you can add tabs to your program. Are those used like I want to do a easy medium and hard?

Basically easy I would set it to say only 6 lower lights. Medium 12 and hard 20.

If so I understand how to change the code for more or less lights and which ones they would be.

So would it or does it work like this. Use pin 2 for the credit/start

Use pin 3 for easy tab
Use pin 4 Medium tab
Use pin 5 hard tab

I think that will finish my code.

You mean the tabs in the IDE window? Those don't create multiple sub-programs to select from at run time, they are for dividing your code into logical sections.

More likely, you would have a variable to control the skill level like

int skillLevel = 0;
...
// somewhere you decide to go to a new level
skillLevel++;
...
// using it:
if (skillLevel == 0) {
  do this
  }
else if (skillLevel == 1) {
  do this instead
  }
else if (skillLevel == 2) {
...

Then you can change the level dependent behaviour at run time and not have to duplicate all the rest of the code.

You can get a lot of mileage from your DIY approach, I know I did in the beginning but you will start to hit brick walls like this one very soon. I encourage you to try to go study coding and the language itself. There are numerous online resources for learning C++.

Also try to understand the examples. They are not perfectly written but they are correct, and demonstrate how many things are done. Much of that knowledge is transferable to your code, or eventually will be.

Deva you are totally awesome!!!!

Ah that is always nice to hear. Call me Rishi.

I'm gonna charge just 25 cents per play so I want this to run as a program.

now i want 10% :wink:

else
  {
    score=0;
    Serial.print("Wrong key or timeout; score=");
    Serial.println(score);
  }

you should probably reset the score after you print it, not before.

//code you only run if button was pressed, stops running when button pressed again, so forth

you can probably just do a simple blocking countdown where first all leds are on and then they count down turning them off 1 at a time.
I think it is also time you restructure your program a little, and put some things into separate functions.
The thing that popped into my head which i think is quite easy to incorporate into your program would be an ever increasing difficulty. Basically, now your timeout is 5000ms, but with every correct button press you reduce the timeout a little, say 1%.

Rishi can you tell me what terms I should be searching?

The increased difficulty works as well. Especially if I could range the lights. Only reason I was doing certain lights was because of height. I can't expect a 5 year old to be hitting a button that is 10' high. But slowly reducing the time would be awesome. I wish I had someone to sit down with. I'm a hands on type person. Obviously I have the start stop function in the examples and still failed to get it to work. But ill be messing with it here soon.

int key=random(0,15);This is where you define the number of leds lighting up, you let the random generator choose a number from 0 to 15, if you want the generator to choose within a different range, this is where you do that. That the program still checks all button doesn't really matter.
Now lets see if we can get that 'run' thing to work.
Actually i started on it but i really think the program needs to be more structured first.

// random number
  int key=random(0,15);
  // activtate LED
  for (int i=0;i<16;i++)
  {
    if (i==key)
      digitalWrite(23+i*2,HIGH);
    else 
      digitalWrite(23+i*2,LOW);
  }
  // read key
  int choice=-1;
  unsigned long millisRef=millis();
  // cycle through loop until 5000 ms = 5sec are gone or button is pressed:
  while ((choice==-1) && (millis()-millisRef<1000))  { 
  for (int i=0;i<16;i++)
  {
    if (choice != -1) {
  bool bpress = true;
  while (bpress) {
    bpress = false;
    for (int i=0;i<16;i++)
      {
         if (digitalRead(22+i*2)==0)  bpress = true;
      }
    }
  }
    if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
      choice=i; // remember button pressed:
  }
  }
  if (choice==key) // right button.
  {
      score++; // increase score
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
  }

This whole thing should be in a separate function that we call from loop. I am starting to lose overview of what is actually happening.
The structure should be clearer :
1 - Set up the program (pinmodes, Serial.begin())
2 - wait for the Button press to start.
3 - Play the intro / countdown
4 - Play the game
5 - Game Over, show the score
6 - return to step 2.

But slowly reducing the time would be awesome.

I thought so to, also because it is so easy to implement, really it is not a lot of work.while ((choice==-1) && (millis()-millisRef<1000))  { this is where you check for 'timeout' at the moment it is 1000ms (it was 5000ms before) You just change that to a variable that you declare and set to your timeout when you set the score to 0. The problem with your current code is that currently you loop() function is actually what should be a sub-loop. Anyway as a global for now you declare float timeout = 1200.0; // or whatever start value you wantChange the timeout function like this while ((choice==-1) && (millis()-millisRef< (uint32_t) timeout))  { and then when you up the score

score++; // increase score
   timeout = timeout * .99;
   Serial.print("OK, score=");

Of course when you reset the score you have to reset the timeout value.

I'm a hands on type person

So am i, ad i found that when i need to learn something, reading a book about it really helps. Code like this can be found in many books including good explanations about what does what etc. The 'for dummies' series is great. It really is not just for dummies !

So i currently have 5 buttons hooked up and at first it was still catching my button pushes as multiply hits so I added in a delay and got that taken care of. I've still never gotten the start/ stop to work let alone start it with a button and use the timeout or a wrong button press to stop it.

The intro I haven't even attempted to do. light everything up and 1 by 1 take them off till there is just one light to start the game.

The uint32_t timeout I haven't gotten to work. I've played with it and tried looking it up and there is just so many ways you can use the uint32 I don't even know where to begin. I tried several different approaches and never succeeded. I know timeout = timeout * .50 means half the time. Been doing that to try and see a change quickly and it's not worked.

BTW I have a C++ for dummies by By Stephen R. Davis on the way.

void setup()
{
  Serial.begin(9600); // look at the score in the Arduino Output window (menu tools / serial monitor).
  // put your setup code here, to run once:
  randomSeed(analogRead(0));
  for (int i=0;i<16;i++)
  {
    // use your switches to connect input with GND default their input value is 1 if not connected:   
    pinMode(22+i*2,INPUT_PULLUP);
    // 16 LED's or whatever, these pins have to be defined as outputs:
    pinMode(23+i*2,OUTPUT);       
  }
}

// the score:
int score=0;
void loop()
{
 
    // random number
 
  int key=random(0,5);
  // activtate LED
  for (int i=0;i<6;i++)
  {
    if (i==key)
      digitalWrite(23+i*2,HIGH);
    else  
      digitalWrite(23+i*2,LOW);
  }
  // read key
  int choice=-1;
  float timeout = 5000.0; // or whatever start value you want
  unsigned long millisRef=millis();
  // cycle through loop until 5000 ms = 5sec are gone or button is pressed:
  while ((choice==-1) && (millis()-millisRef< (uint32_t)timeout))  {  
  for (int i=0;i<6;i++)
  {
    if (choice != -1) {
  bool bpress = true;
  delay(50);
  while (bpress) {
    bpress = false;
    for (int i=0;i<6;i++)
      {
         if (digitalRead(22+i*2)==0)  bpress = true;
      }
    }
  }
    if (digitalRead(22+i*2)==0) // button pressed -> input goes to 0
      choice=i; // remember button pressed:
  }
  }
  if (choice==key) // right button.
  {
      score++; // increase score
      timeout = timeout * .50;
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
  }
  else
  {
    Serial.print("Wrong key or timeout; score=");
    Serial.println(score); 
    score=0;
  }
   
}

The uint32_t timeout I haven't gotten to work.

well you keep resetting it to 5000.0 every time a new led is supposed to light up. You should do that where you set the score.

so I added in a delay and got that taken care of.

Yep that usually does it. I sometimes poll for 50ms to see if there is any change in the pins and only continue if there isn't.
Anyway, I've put some structure in you program, of course i don't have the hardware (well i have but i have other stuff to built), but it compiles.

#define LEDS_BUTTS 5
#define START_BUTT 2

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  for (int i = 0; i < LEDS_BUTTS; i++)  {
    pinMode(22 + i * 2, INPUT_PULLUP);
    pinMode(23 + i * 2, OUTPUT);
  }
  pinMode(START_BUTT, INPUT_PULLUP);
}


void loop() {
  WaitForStart();
  PlayIntro();
  int scr = PlayGame();
  ShowScore(scr);
}

void WaitForStart() {
  while(digitalRead(START_BUTT) == HIGH) {
    int ledon = LOW;
    if ((millis() % 1000) / 500) ledon = HIGH;
    for (int i = 0; i < LEDS_BUTTS; i++)  {   // blink the leds while waiting
      digitalWrite(23 + i * 2, ledon);
    }
  }
}

void PlayIntro() {
  for (int i = 0; i < LEDS_BUTTS; i++)  {  // turn 'm all on
    digitalWrite(23 + i * 2, HIGH);
  }
  for (int i = 0; i < LEDS_BUTTS; i++)  { // turn 'm off one by one
    delay(200);
    digitalWrite(23 + i * 2, LOW);    
  }
  delay(500); // wait a bit before we start.
}

int PlayGame() {
  int score = 0;
  float timeout = 5000.0;  
  
  while (1) {  // keep looping
    int key = random(0, LEDS_BUTTS);
    for (int i = 0; i < LEDS_BUTTS; i++)  {   // light the led
      if (i == key)  digitalWrite(23 + i * 2, HIGH);
      else digitalWrite(23 + i * 2, LOW);
    }

    int choice = -1;
    unsigned long millisRef = millis();
    while ((choice == -1) && (millis() - millisRef < (uint32_t) timeout))  {
      for (int i = 0; i < 6; i++)  {
        if (digitalRead(22 + i * 2) == 0)   choice = i; // remember button pressed:
      }
    }
    if (choice != -1) {  // button pressed
      bool bpress = true;
      delay(50);
      while (bpress) {
        bpress = false;
        for (int i = 0; i < LEDS_BUTTS; i++)  {
          if (digitalRead(22 + i * 2) == 0)  bpress = true;
        }
      }   // and released
      delay(50);
    }
    if (choice == key)   {
      score++; // increase score
      timeout = timeout * .90;
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
      // delay(200);
    }
    else return score;  // or exit.
  }  // while(1)
}

void ShowScore(int score) {
  Serial.print("Wrong key or timeout; score=");
  Serial.println(score);
}

Hello Rishi,
Guess my book will be here on Saturday. I have most of my buttons hooked up and I've played with it and everything seems to be working well. I've added in a few small things as I think about it and brain storm and I'm sure once it's up and running I'll do more with it. For now though the only thing I can think of is one thing we half mentioned and that's the difficulty level. 7 buttons vs 14 vs 20.....easy, medium, and hard. So I've read several different post and examples that's used either a button press or a users typed in. But none of them showed like how I would change my button/leds from say 7 to now 14. I actually tried adding in another line of

while (1) {  // keep looping
    int key = random(0, 5);
    for (int i = 0; i < LEDS_BUTTS; i++)  {   // light the led
      if (i == key)  digitalWrite(23 + i * 2, HIGH);
      else digitalWrite(23 + i * 2, LOW);

But then I realized how would I select the second one that has different led and buttons?
I also have a display screen coming ( SparkFun Serial Enabled LCD Kit ).

If you would help me out this one last time I'm appreciate it.

Thanks Jeremy

#define LEDS_BUTTS 6
#define START_BUTT 2

int horn = 3;
int buzzer = 4;
int smoke = 5;
void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  for (int i = 0; i < LEDS_BUTTS; i++)  {
    pinMode(22 + i * 2, INPUT_PULLUP);
    pinMode(23 + i * 2, OUTPUT);
  }
  pinMode(START_BUTT, INPUT_PULLUP); {
    pinMode(horn, OUTPUT);
    pinMode(buzzer, OUTPUT);
  }
}


void loop() {
  WaitForStart();
  PlayIntro();
  int scr = PlayGame();
  ShowScore(scr);
}

void WaitForStart() {
  while(digitalRead(START_BUTT) == HIGH) {    
    int ledon = LOW;
    if ((millis() % 1000) / 500) ledon = HIGH;
    for (int i = 0; i < LEDS_BUTTS; i++)  {   // blink the leds while waiting
      digitalWrite(23 + i * 2, ledon);
    }
  }
}

void PlayIntro() {
  for (int i = 0; i < LEDS_BUTTS; i++)  {  // turn 'm all on
    digitalWrite(23 + i * 2, HIGH);
  }
  {
  digitalWrite(horn, HIGH);
  delay(1000);
  digitalWrite(horn, LOW); 
  digitalWrite(smoke, HIGH);
  delay(1000);
  digitalWrite(smoke, LOW);

  for (int i = 0; i < LEDS_BUTTS; i++)  { // turn 'm off one by one
    delay(200);
    digitalWrite(23 + i * 2, LOW);    
  }
  delay(500); // wait a bit before we start.
}
}

int PlayGame() {
  int score = 0;
  float timeout = 10000.0;  
  
  while (1) {  // keep looping
    int key = random(0, 5);
    for (int i = 0; i < LEDS_BUTTS; i++)  {   // light the led
      if (i == key)  digitalWrite(23 + i * 2, HIGH);
      else digitalWrite(23 + i * 2, LOW);
    }
    int choice = -1;
    unsigned long millisRef = millis();
    while ((choice == -1) && (millis() - millisRef < (uint32_t) timeout))  {
      for (int i = 0; i < 6; i++)  {
        if (digitalRead(22 + i * 2) == 0)   choice = i; // remember button pressed:
      }
    }
    if (choice != -1) {  // button pressed
      bool bpress = true;
      delay(100);
      while (bpress) {
        bpress = false;
        for (int i = 0; i < LEDS_BUTTS; i++)  {
          if (digitalRead(22 + i * 2) == 0)  bpress = true;
        }
      }   // and released
      delay(50);
    }
    if (choice == key)   {
      score++; // increase score
      timeout = timeout * .99;
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
      // delay(200);
    }
    else return score;  // or exit.
  }  // while(1)
}

void ShowScore(int score) {
  Serial.print("Wrong key or timeout; score=");
  Serial.println(score);
  digitalWrite(buzzer, HIGH);
  delay(1000);
  digitalWrite(buzzer, LOW);
}

Hi Jeremy

First of all, change this :int key = random(0, 5);back to :int key = random(0, LEDS_BUTTS);Or you will never chose a led other than the first 5.
Here i made a small mistake

for (int i = 0; i < 6; i++)  {
        if (digitalRead(22 + i * 2) == 0)   choice = i; // remember button pressed:
      }

the loop should be :

for (int i = 0; i < LED_BUTTS; i++)  {
        if (digitalRead(22 + i * 2) == 0)   choice = i; // remember button pressed:
      }

error like this happen. The idea of a #define is that you can easily adjust the program to the hardware that you have, without having to change every point where a value depends on the hardware. Like that you prevent a lot of bugs.

Guess my book will be here on Saturday.

Oh great, well it's in the reading. Of course there will be parts of the book that you will just flick through, and as far as i remember it is actually focused on C++ in general, not on the Arduino dialect, but a lot of the techniques are explained.

For now though the only thing I can think of is one thing we half mentioned and that's the difficulty level. 7 buttons vs 14 vs 20.....easy, medium, and hard. So I've read several different post and examples that's used either a button press or a users typed in.

I think with your current setup the obvious choice would be 3 different start buttons.
What is going to be a bit of a thing is that the way you have it setup, there is only 16 consecutive led & button pins. If you want to add more you may have to create an array with pin locations (rather than the offset + 2 * led/button-nr calculation that you do now)
For implementation what you can do is instead of using the #define LEDS_BUTTS 6where the compiler just replaces any occurrence of 'LEDS_BUTTS' with '6' for some parts with a variable. We can keep it local and pass it back and forth between the functions.
Here is the example :

#define LEDS_BUTTS 16  // the absolute max
#define EASY_BUTT 2
#define MED_BUTT 3
#define HARD_BUTT 4

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  for (int i = 0; i < LEDS_BUTTS; i++)  {
    pinMode(22 + i * 2, INPUT_PULLUP);
    pinMode(23 + i * 2, OUTPUT);  // 23 + 15 * 2 = 53
  }
  pinMode(MED_BUTT, INPUT_PULLUP);
  pinMode(EASY_BUTT, INPUT_PULLUP);
  pinMode(HARD_BUTT, INPUT_PULLUP);
}


void loop() {
  int ledandbuttons = WaitForStart();  // now WaitForStart returns the number of leds/buttons
  PlayIntro(ledandbuttons);
  int scr = PlayGame(ledandbuttons);
  ShowScore(scr);
}

int WaitForStart() {
  while(1) {
    if (digitalRead(EASY_BUTT) == LOW) return 5;
    if (digitalRead(MED_BUTT) == LOW) return 10;
    if (digitalRead(HARD_BUTT) == LOW) return 16;
    int ledon = LOW;
    if ((millis() % 1000) / 500) ledon = HIGH;
    for (int i = 0; i < LEDS_BUTTS; i++)  {   // blink the leds while waiting
      digitalWrite(23 + i * 2, ledon);
    }
  }
}

void PlayIntro(int led_butts) {  // in the intro you will see which leds can be used
  for (int i = 0; i < led_butts; i++)  {  // turn 'm all on
    digitalWrite(23 + i * 2, HIGH);
  }
  for (int i = 0; i < led_butts; i++)  { // turn 'm off one by one
    delay(200);
    digitalWrite(23 + i * 2, LOW);    
  }
  delay(500); // wait a bit before we start.
}

int PlayGame(int led_butts) {
  int score = 0;
  float timeout = 5000.0;  
  
  while (1) {  // keep looping
    int key = random(0, led_butts);
    for (int i = 0; i < led_butts; i++)  {   // light the led
      if (i == key)  digitalWrite(23 + i * 2, HIGH);
      else digitalWrite(23 + i * 2, LOW);
    }

    int choice = -1;
    unsigned long millisRef = millis();
    while ((choice == -1) && (millis() - millisRef < (uint32_t) timeout))  {
      for (int i = 0; i < led_butts; i++)  {
        if (digitalRead(22 + i * 2) == 0)   choice = i; // remember button pressed:
      }
    }
    if (choice != -1) {  // button pressed
      bool bpress = true;
      delay(50);
      while (bpress) {
        bpress = false;
        for (int i = 0; i < led_butts; i++)  {
          if (digitalRead(22 + i * 2) == 0)  bpress = true;
        }
      }   // and released
      delay(50);
    }
    if (choice == key)   {
      score++; // increase score
      timeout = timeout * .90;
      Serial.print("OK, score=");
      Serial.println(score); // print score in Arduino output Window.
      // delay(200);
    }
    else return score;  // or exit.
  }  // while(1)
}

void ShowScore(int score) {
  Serial.print("Wrong key or timeout; score=");
  Serial.println(score);
}

Note that i use all uppercase for a #define and lowercase for variables. This is just a convention, you can use upper and lowercase any way you like but it does help to be consistent. I did not add your buzzer and horn thing to the example. Please remember to indent your code properly, it will make it more clear what closing braces belongs to which opening brace and what part of the code is conditionally executed. Just hit ctrl-T and the IDE will do it for you.

IDK why but you can probably answer this. When the random is set to say 0, 6 and the int i+ 0; i < 6 or LEDS_BUTTS; or other words 6. Every time I hit the last button it always pops up with a wrong key pressed and I see the lights flicker as it jumps. So when I drop the random to 5 and leave all the rest at 6 I have no issues but all the lights light up and it works.

int key = random(0, 5);
for (int i = 0; i < LEDS_BUTTS; i++) {

and yes I planned on 3 buttons but I still want to charge a quarter to play. So I need a button input for a credit and then choose difficultly.