HELP! Simon Says-adding game start input and more

I am extremely new to all this so I apologize for the extremely easy questions here but your time really is appreciated.

Currently the below programming will start the game when any of the 4 buttons are pressed. I want to add an additional button that will be used to start the game while removing the ability of the original 4 buttons to start the game.

I absolutely can not figure out what is actually calling the game start action in this code and how to modify it to the above goal.

I also want to add an additional output that will issue a pulse every time the next level in the game is achieved.

I tried to post the entire sketch but it says the file is too large.

Here is a link to the entire code on paste bin :

Any help with this is greatly appreciated.

void loop()
{
  attractMode(); // Blink lights while waiting for user to press a button

  // Indicate the start of game play
  setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW); // Turn all LEDs on
  delay(1000);
  setLEDs(CHOICE_OFF); // Turn off LEDs
  delay(250);

  if (gameMode == MODE_MEMORY)
  {
    // Play memory game and handle result
    if (play_memory() == true) 
      play_winner(); // Player won, play winner tones
    else 
      play_loser(); // Player lost, play loser tones
  }

  if (gameMode == MODE_BATTLE)
  {
    play_battle(); // Play game until someone loses

    play_loser(); // Player lost, play loser tones
  }
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//The following functions are related to game play only

// Play the regular memory game
// Returns 0 if player loses, or 1 if player wins
boolean play_memory(void)
{
  randomSeed(millis()); // Seed the random generator with random amount of millis()

  gameRound = 0; // Reset the game to the beginning

  while (gameRound < ROUNDS_TO_WIN) 
  {
    add_to_moves(); // Add a button to the current moves, then play them back

    playMoves(); // Play back the current game board

    // Then require the player to repeat the sequence.
    for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++)
    {
      byte choice = wait_for_button(); // See what button the user presses

      if (choice == 0) return false; // If wait timed out, player loses

      if (choice != gameBoard[currentMove]) return false; // If the choice is incorect, player loses
    }

    delay(1000); // Player was correct, delay before playing moves
  }

  return true; // Player made it through all the rounds to win!
}

// Play the special 2 player battle mode
// A player begins by pressing a button then handing it to the other player
// That player repeats the button and adds one, then passes back.
// This function returns when someone loses
boolean play_battle(void)
{
  gameRound = 0; // Reset the game frame back to one frame

  while (1) // Loop until someone fails 
  {
    byte newButton = wait_for_button(); // Wait for user to input next move
    gameBoard[gameRound++] = newButton; // Add this new button to the game array

    // Then require the player to repeat the sequence.
    for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++)
    {
      byte choice = wait_for_button();

      if (choice == 0) return false; // If wait timed out, player loses.

      if (choice != gameBoard[currentMove]) return false; // If the choice is incorect, player loses.
    }

    delay(100); // Give the user an extra 100ms to hand the game to the other player
  }

  return true; // We should never get here
}

// Plays the current contents of the game moves
void playMoves(void)
{
  for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++) 
  {
    toner(gameBoard[currentMove], 150);

    // Wait some amount of time between button playback
    // Shorten this to make game harder
    delay(150); // 150 works well. 75 gets fast.
  }
}

// Adds a new random button to the game sequence, by sampling the timer
void add_to_moves(void)
{
  byte newButton = random(0, 4); //min (included), max (exluded)

  // We have to convert this number, 0 to 3, to CHOICEs
  if(newButton == 0) newButton = CHOICE_RED;
  else if(newButton == 1) newButton = CHOICE_GREEN;
  else if(newButton == 2) newButton = CHOICE_BLUE;
  else if(newButton == 3) newButton = CHOICE_YELLOW;

  gameBoard[gameRound++] = newButton; // Add this new button to the game array
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//The following functions control the hardware

// Lights a given LEDs
// Pass in a byte that is made up from CHOICE_RED, CHOICE_YELLOW, etc
void setLEDs(byte leds)
{
  if ((leds & CHOICE_RED) != 0)
    digitalWrite(LED_RED, HIGH);
  else
    digitalWrite(LED_RED, LOW);

  if ((leds & CHOICE_GREEN) != 0)
    digitalWrite(LED_GREEN, HIGH);
  else
    digitalWrite(LED_GREEN, LOW);

  if ((leds & CHOICE_BLUE) != 0)
    digitalWrite(LED_BLUE, HIGH);
  else
    digitalWrite(LED_BLUE, LOW);

  if ((leds & CHOICE_YELLOW) != 0)
    digitalWrite(LED_YELLOW, HIGH);
  else
    digitalWrite(LED_YELLOW, LOW);
}

// Wait for a button to be pressed. 
// Returns one of LED colors (LED_RED, etc.) if successful, 0 if timed out
byte wait_for_button(void)
{
  long startTime = millis(); // Remember the time we started the this loop

  while ( (millis() - startTime) < ENTRY_TIME_LIMIT) // Loop until too much time has passed
  {
    byte button = checkButton();

    if (button != CHOICE_NONE)
    { 
      toner(button, 150); // Play the button the user just pressed

      while(checkButton() != CHOICE_NONE) ;  // Now let's wait for user to release button

      delay(10); // This helps with debouncing and accidental double taps

      return button;
    }

  }

  return CHOICE_NONE; // If we get here, we've timed out!
}

// Returns a '1' bit in the position corresponding to CHOICE_RED, CHOICE_GREEN, etc.
byte checkButton(void)
{
  if (digitalRead(BUTTON_RED) == 0) return(CHOICE_RED); 
  else if (digitalRead(BUTTON_GREEN) == 0) return(CHOICE_GREEN); 
  else if (digitalRead(BUTTON_BLUE) == 0) return(CHOICE_BLUE); 
  else if (digitalRead(BUTTON_YELLOW) == 0) return(CHOICE_YELLOW);

  return(CHOICE_NONE); // If no button is pressed, return none
}

// Light an LED and play tone
// Red, upper left:     440Hz - 2.272ms - 1.136ms pulse
// Green, upper right:  880Hz - 1.136ms - 0.568ms pulse
// Blue, lower left:    587.33Hz - 1.702ms - 0.851ms pulse
// Yellow, lower right: 784Hz - 1.276ms - 0.638ms pulse
void toner(byte which, int buzz_length_ms)
{
  setLEDs(which); //Turn on a given LED

  //Play the sound associated with the given LED
  switch(which) 
  {
  case CHOICE_RED:
    buzz_sound(buzz_length_ms, 1136); 
    break;
  case CHOICE_GREEN:
    buzz_sound(buzz_length_ms, 568); 
    break;
  case CHOICE_BLUE:
    buzz_sound(buzz_length_ms, 851); 
    break;
  case CHOICE_YELLOW:
    buzz_sound(buzz_length_ms, 638); 
    break;
  }

  setLEDs(CHOICE_OFF); // Turn off all LEDs
}

// Toggle buzzer every buzz_delay_us, for a duration of buzz_length_ms.
void buzz_sound(int buzz_length_ms, int buzz_delay_us)
{
  // Convert total play time from milliseconds to microseconds
  long buzz_length_us = buzz_length_ms * (long)1000;

  // Loop until the remaining play time is less than a single buzz_delay_us
  while (buzz_length_us > (buzz_delay_us * 2))
  {
    buzz_length_us -= buzz_delay_us * 2; //Decrease the remaining play time

    // Toggle the buzzer at various speeds
    digitalWrite(BUZZER1, LOW);
    digitalWrite(BUZZER2, HIGH);
    delayMicroseconds(buzz_delay_us);

    digitalWrite(BUZZER1, HIGH);
    digitalWrite(BUZZER2, LOW);
    delayMicroseconds(buzz_delay_us);
  }
}

// Play the winner sound and lights
void play_winner(void)
{
  setLEDs(CHOICE_GREEN | CHOICE_BLUE);
  winner_sound();
  setLEDs(CHOICE_RED | CHOICE_YELLOW);
  winner_sound();
  setLEDs(CHOICE_GREEN | CHOICE_BLUE);
  winner_sound();
  setLEDs(CHOICE_RED | CHOICE_YELLOW);
  winner_sound();
}

mbdsimonBeta.ino (15.3 KB)

And your question is ?

Oh, and where is the rest of your program ?

sorry about that... the forum said the message was too long and erased my entire question statement etc.

I've attached a link to paste bin of the entire sketch.

Thank you in advance!

Here is a link to the entire code on paste bin

Are you looking for help on pastebin? If so, why are you here? If not, why is your code there?

Use Reply (not the Quick Reply field) and the Additional Options link you post your code here.

No, I am trying to post my entire sketch here for help trying to figure out how to modify coding to achieve the results as outlined in the first post.

However, this forum keeps saying that i've exceeded the maxium allowed characters for the post.

I used the code tags and pasted the entire sketch between them but i'm still getting the maximum allowed error.

I was using pastebin so that the entire sketch could be viewed.. I apologize if this is not the correct way of doing it.

I am unsure of how to get the entire sketch to shown between the code tags though?

I am unsure of how to get the entire sketch to shown between the code tags though?

If it exceeds 9500 characters, you can't. Re-read what I said earlier.

the .ino file is now attached under the original post... I hope this is what were you referring to?

I appreciate the patience with this.

isomorphic85: the .ino file is now attached under the original post... I hope this is what were you referring to?

I appreciate the patience with this.

So now that we have your code, what is the question ?

isomorphic85:
Currently the below programming will start the game when any of the 4 buttons are pressed. I want to add an additional button that will be used to start the game while removing the ability of the original 4 buttons to start the game.

I absolutely can not figure out what is actually calling the game start action in this code and how to modify it to the above goal.

I also want to add an additional output that will issue a pulse every time the next level in the game is achieved.

Did you add the BUTTON_START defines and such? The function where any button starts the game is in the attractMode() function. It's kind of obvious at the start of loop() with this line.

attractMode(); // Blink lights while waiting for user to press a button

Which is exactly what happens there. You could change that function to wait for your BUTTON_START button to be pressed instead.

yes I added the define BUTTON_START etc

I apologize I'm very new to this....

The modified code at the bottom is working! Thank you very much.

Also any thoughts on how I could go about the second portion of the question regarding how to pulse an output each time a new level is achieved?

there is currently a Game State Variable called byte gameRound =0 that counts the number of successful rounds the player has made it through. How can I utilize this to pulse an output?

I've created #define TICKETS_OUT 11 and set pinMode(TICKETS_OUT, OUTPUT);

I do not know what expression I would need to create though to have this read the byte gameRound and pulse TICKETS_OUT for 50 ms each time it advances.... any thoughts?

// Game state variables
byte gameMode = MODE_MEMORY; //By default, let's play the memory game
byte gameBoard[32]; //Contains the combination of buttons as we advance
byte gameRound = 0; //Counts the number of succesful rounds the player has made it through
// Show an "attract mode" display while waiting for user to press button.
void attractMode(void)
{
  while(1) 
  {
    setLEDs(CHOICE_RED);
    delay(100);
    if (digitalRead(BUTTON_START) == LOW)return;

    setLEDs(CHOICE_BLUE);
    delay(100);
    if (digitalRead(BUTTON_START) == LOW) return;

    setLEDs(CHOICE_GREEN);
    delay(100);
   if (digitalRead(BUTTON_START) == LOW) return;

    setLEDs(CHOICE_YELLOW);
    delay(100);
    if (digitalRead(BUTTON_START) == LOW) return;
  }
}

hola soy nuevo. Me podrian ayudar en un codigo, este codigo lo saque de speak fun, es un juego de simon says que estuve mejorando y editando. El problema que tengo ahora, es hacer otro pero, es de meter un par de condicionales para activar un rele que te permita cambiar el turno cada vez que terminas de agregar un boton nuevo y que lo repita el siguiente jugador. utilizando las salidas NA COMUN NC. na y nc como dos opciones para activarlos, asi como un switch y comun como alimentacion. es decir empieza el jugador 1 con un estado del rele activo la cual al momento de agregar un boton esta cambie de estado permitiendo que el jugador 1 no tenga continuidad y que el jugador dos repita la secuencia y agregue un boton nuevo y q cambie de estado, por hago que cambie de estado? los tengo en paralelo asi puedo que de forma automatica, permita activar o desactivar la continuidad. se que puede con un pulsador de 3 polos, me doy idea, pero el tema seria de forma automatica que active el rele, sin necesidad de pulsar un boton mas manualmente. me entedeis?

aca esta el codigo, es una parte de 2 jugadores, sobre los dos jugadores como habia dicho los botones esta en paralelo, es decir J1 con 4 botones y J2 con 4 botones en paralelo

// Play the special 2 player battle mode
// A player begins by pressing a button then handing it to the other player
// That player repeats the button and adds one, then passes back.
// This function returns when someone loses
boolean play_battle(void)
{
gameRound = 0; // Reset the game frame back to one fram

while (1) // Loop until someone fails
{
byte newButton = wait_for_button(); // Wait for user to input next move
gameBoard[gameRound++] = newButton; // Add this new button to the game array

// Then require the player to repeat the sequence.
for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++)
{
byte choice = wait_for_button();

if (choice == 0) return false; // If wait timed out, player loses.

if (choice != gameBoard[currentMove]) return false; // If the choice is incorect, player loses.
}

delay(100); // Give the user an extra 100ms to hand the game to the other player
}

return true; // We should never get here
}

// Wait for a button to be pressed.
// Returns one of LED colors (LED_RED, etc.) if successful, 0 if timed out
byte wait_for_button(void)
{
long startTime = millis(); // Remember the time we started the this loop

while ( (millis() - startTime) < ENTRY_TIME_LIMIT) // Loop until too much time has passed
{
byte button = checkButton();

if (button != CHOICE_NONE)
{
toner(button, 150); // Play the button the user just pressed

while(checkButton() != CHOICE_NONE) ; // Now let’s wait for user to release button

delay(10); // This helps with debouncing and accidental double taps

return button;
}

}