Escape room puzzles- Multiple puzzles running in order after the other

Hello,
I am extremely new to this world and coding, but I have been inspired to create an all-in-one escape room in a briefcase with multiple puzzles. I have completed each puzzle and coded every puzzle so all 6 work independently of each other; but I want them all to run off a single Arduino Mega 2560.

My issue is that I do not know how to write the code so that when the first puzzle is completed it will move to the next, etc...

After doing a ton of research, I have yet to find anything that will truly help me move the code together except merging all the codes.

My question is; is there a way to build the code so it will read the first sketch and wait for the answer, then when the puzzle has been solved it turns that puzzle off and moves to the second puzzle, initializing it, putting it in a running state, and again waiting for a solve before moving forward.

Basically, I want the Arduino code to move in the order I arrange it as. Because each puzzle will need something from the prior puzzle to give them the answer for the solve.

Thank you in advance for your assistance and time.

One possibility is a state machine implemented with the switch/case construct.

switch (puzzleState)

  case state1
  do puzzle1Stuff();
  if (puzzle1 == solved)
  {
    puzzleState = state2;
    break;
  }
  else
  {
    break;
  }

  case state2
  do puzzle2Stuff()
  etc., etc.

Number your puzzles in the order in which you want them to be completed. (1, 2, 3, ...)

In your code, use a variable to keep track of which puzzle is now active.

In your loop, check which puzzle is active, and perform the appropriate actions accordingly. Something like:

// declare a variable to keep track of which puzzle is active
byte activePuzzle = 0;

void setup() {
  
  // 
  // This is where you put the stuff that happens only once,
  // for when you turn the power on.
  //
  
  // You will probably want to start with puzzle #1.
  activePuzzle = 1;
  
}

void loop() {
  
  if (activePuzzle == 1) {
    
    // This is where you put the stuff for when puzzle #1 is running.
    
  }
  
  else if (activePuzzle == 2) {
    
    // This is where you put the stuff for when puzzle #2 is running.
    
  }
  
  else if (activePuzzle == 3) {
    
    // This is where you put the stuff for when puzzle #3 is running.
    
  }
  
  // and so forth for puzzles #4, #5, and #6
  
  else {
    
    // This is where you put the stuff for when none of the puzzles is running.
    // For example, this is where you put the code to open the door.
    
  }

}

But, that's just how I would do it. Different people on this forum might recommend that you do it a different way.

For me, I would code each puzzle into separate functions. I'm assuming the puzzles need to be completed in a unique order. For each puzzle, I would make a bool value called completed or something. Once the user completed the puzzle, this bool value would be flipped to 1 (true). At the end, I would slap an if statement checking if all the puzzles were completed.

odometer did a great job of explaining a coding way.

Hope it works out!

-Englishscone

Thank you all so much!

odometer- Your coding worked perfectly and after a little working on it the puzzles now work along with each other. Thank you all for taking time out of your day to help me!