Sequential Passwords for escape room clues

I am trying to have an Arduino Uno connected to an LCD, Keypad, and 3 LEDs for an escape room/ proposal. My goal is to have clues hidden around the property and have a map placed on the dining room table with LEDs underneath it, so when the correct password is inputted the next location on the map is revealed. However, when running the code it just flashes through the entire program rather than waiting for input to begin. Sorry in advance for the messy code, I was grasping at straws for a while trying to make it work.`Use code tags to format code for the forum

/*
Eli Turrentine
Escape room sequential keypad password system

 Code based on
  DroneBot Workshop 2020
  https://dronebotworkshop.com
 AND
  https://www.circuitbasics.com/
 
*/

// Include Arduino Wire library for I2C
#include <Wire.h>
// Include LCD display library for I2C
#include <Adafruit_LiquidCrystal.h>
// Include Keypad library
#include <Keypad.h>

// Length of password + 1 for null character
#define Password_Length 5
// Character to hold password input
char Data[Password_Length];
// Password
char Master1[Password_Length] = "1111";
char Master2[Password_Length] = "2222";
char Master3[Password_Length] = "3333";


// Pin connected to lock relay input
int led1 = 13;
int led2 = 12;
int led3 = 11;
// Counter for character entries
byte data_count = 0;

// Character to hold key input
char customKey;

// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;

// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

// Connections to Arduino
byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };

// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

// puzzle selection
bool checkPuzzle1 = false;
bool checkPuzzle2 = false;
bool checkPuzzle3 = false;
// Create LCD object

Adafruit_LiquidCrystal lcd_1(0);

void setup() {
  // Setup LCD with backlight and initialize
  lcd_1.begin(16, 2);

  // Set led1, 2, 3  as OUTPUT pin
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop() {

  // Initialize LCD and print
  lcd_1.setCursor(0, 0);
  lcd_1.print("Enter Password:");

  // Look for keypress
  customKey = customKeypad.getKey();
  if (customKey) {
    // Enter keypress into array and increment counter
    Data[data_count] = customKey;
    lcd_1.setCursor(data_count, 1);
    lcd_1.print(Data[data_count]);
    data_count++;
  }

  // See if we have reached the password length
  if (data_count == Password_Length - 1) {
    lcd_1.clear();

    if (!strcmp(Data, Master1)) {
      // Password is correct
      lcd_1.print("Correct");
      // Turn on relay for 5 seconds
      digitalWrite(led1, HIGH);
      delay(2500);
      checkPuzzle1 = true;
      //clear data
       while (data_count != 0) {
        Data[data_count--] = 0;
      }
    } else {
      // Password is incorrect
      lcd_1.print("Incorrect");
      delay(1000);
      while (data_count != 0) {
        Data[data_count--] = 0;
      }
    }
    lcd_1.setCursor(0, 0);
    lcd_1.print("Enter Password:");
  }
  //puzzle 2
  customKey = customKeypad.getKey();
  // just print the pressed key
  if ((customKey) && (checkPuzzle1 = true)) {
    Data[data_count] = customKey;
    lcd_1.setCursor(data_count, 1);
    lcd_1.print(Data[data_count]);
    data_count++;
  }
  if (data_count == Password_Length - 1) {
    lcd_1.clear();

    if (!strcmp(Data, Master2)) {
      // password Correct
      lcd_1.print("Correct");
      digitalWrite(12, HIGH);
      delay(2500);
      while (data_count != 0) {
        Data[data_count--] = 0;
      }
      checkPuzzle2 = true;
    } else {
      lcd_1.print("Incorrect");
      delay(2000);
      // Clear data and LCD display
      lcd_1.clear();
      while (data_count != 0) {
        Data[data_count--] = 0;
      }
    }
    lcd_1.setCursor(0, 0);
    lcd_1.print("Enter Password:");
  }

  customKey = customKeypad.getKey();
  // just print the pressed key
  if ((customKey) && (checkPuzzle2 = true)) {
    Data[data_count] = customKey;
    lcd_1.setCursor(data_count, 1);
    lcd_1.print(Data[data_count]);
    delay(1);
    data_count++;
  }
  if (data_count == Password_Length - 1) {
    lcd_1.clear();

    if (!strcmp(Data, Master2)) {
      // password Correct
      lcd_1.print("Correct");
      digitalWrite(12, HIGH);
      delay(2500);
      while (data_count != 0) {
        Data[data_count--] = 0;
      }
      checkPuzzle3 = true;
    } else {
      lcd_1.print("Incorrect");
      delay(2000);
      // Clear data and LCD display
      lcd_1.clear();
      while (data_count != 0) {
        Data[data_count--] = 0;
      }
    }
  }
  if(checkPuzzle3=true){
  lcd_1.clear();
  lcd_1.print("CONGRATS");
  }
}

One thing that I notice is that you are not explicitly terminating the string held in the Data array. It should not matter because the array will be filled with zeroes as it is declared globally but adding the string terminator after each character would be good programming practice

As to the problem that you describe, look at this portion of the sketch

   // Look for keypress
    customKey = customKeypad.getKey();
    if (customKey)
    {
        // Enter keypress into array and increment counter
        Data[data_count] = customKey;
        lcd_1.setCursor(data_count, 1);
        lcd_1.print(Data[data_count]);
        data_count++;
    }

What will happen if no key is pressed and hence customKey is zero, ie false ?

Just seeing in a first fast flight about your code

if ((customKey) && (checkPuzzle1 = true)) {

booleans are true or false, and you want test for equality, not assignment, so either

if ((customKey) && (checkPuzzle1 == true)) {

or

if (customKey && checkPuzzle1) {

where I also tossed some unnecessary harmless but distracting parentheses.

That might change some results.

a7

This is a state machine all day long.
If it was me, I would use one driving variable, not booleans of

checkPuzzle1 = true

an so on.
Break the whole game down into every "level" if you will, all the possible states/outcomes and use a byte mode; in your global definitions and initialized in

void setup(){
mode = 0; //start here. Waiting for some player to do some thing
}

then maybe like this overall

byte mode;

void setup() {
  mode = 0; //start here. Waiting for some player to do some thing
}

void loop() {
  // Look for keypress
  // and whatever other input stuff here sets mode variable
  customKey = customKeypad.getKey();
  if (customKey) {
    // Enter keypress into array and increment counter
    Data[data_count] = customKey;
    lcd_1.setCursor(data_count, 1);
    lcd_1.print(Data[data_count]);
    data_count++;
  }

  // See if we have reached the password length
  if (data_count == Password_Length - 1) {
    lcd_1.clear();

    if (!strcmp(Data, Master1)) {
      // Password is correct
      mode = 1;
    }
    else {
      // Password is incorrect
      mode = 2;

    }
    // I don't know what this does, put it here or in
    // switch/case as needed
    // but do you need to do it twice? Shouldn't it go here?
    while (data_count != 0) {
      Data[data_count--] = 0;
    }
    // etc but whatever ends the game, ie win or lose
    // after driving those states
    // maybe "trapped" is mode 5 and "you escaped"
    // is mode 6, wrap back around to
    mode = 0; // reset game
    break;
  }

  switch (mode) {
    case 0:
      // output stuff while waiting for someone to do something
      // so idle state at start may just be
      // Initialize LCD and print
      lcd_1.setCursor(0, 0);
      lcd_1.print("Enter Password:");
      break; // go back and check loop
    case 1:
      lcd_1.print("Correct");
      // Turn on relay for 5 seconds
      digitalWrite(led1, HIGH);
      delay(2500);
      break;
    case 2:
      lcd_1.print("Incorrect");
      delay(1000);
      break;
      // and so on
  } // end of switch
} // end of loop

Fwiw, this way has become my basic template for every Hallowe'en prop I make, and they all have some built in game where my entire haunt is like a living video game/escape room/rage room inspired mega game.

I'm not saying you should listen to me, in fact I didn't even look at your code that closely and @alto777 is probably right (again :wink: ) but making parlour/escape room style games became much easier for me once I understood how to design them as a state machine. It really was a "game" changer.

@eturrentine1 you have the same error several times over.

Once I changed the assigment operators '=' to tests for equality, or just let the bolleans be booleans, the sketch started to work better.

I suggest adding serial printing. Just now it is stuck on soliciting and deeming correct or incorrect just the first password.

Here's the problem, or a problem:


// first hurdle
  customKey = customKeypad.getKey();
  if (customKey) {

// second hurdle  
  customKey = customKeypad.getKey();
  if (customKey && checkPuzzle1) {

// third hurdle
  customKey = customKeypad.getKey();
  if (customKey && checkPuzzle2) {

The logic never gets over the first hurdle, as there is no test to make it skip that and do the next block.

I think there is enough commonality to the three hurdles that they could be handled by one block of code.

But if that is a stretch or not true, you need to have a different mechanism. I suggest a counter, which when 0 zero keeps us looking at the first hurdle, which increments the variable so that 1 one is the next and so forth.

I haven't flown low over @hallowed31's suggestions, at a glance it appears to be similar advices.

When I work with your code, I will try to leave it as you presented it; we can talk later about stepping it up a bit with a few tricks that should be next on your list of things to learn about, if you wanna.

a7

Sry, gotta jet.

But following my own advice, I was able to make the three password test and receive a plausible CONGRATS message.

You have a few copy/past/edit errors; this is the problem with coding in that style. A real pain to edit everything just so, and N times the pain when you discover a flaw in the logic that must be fixed in N places.

So I agree with myself - the code should be one common password solicitation and check.

There needs be only one printing of "enter password" and one getting of a valid key.

a7

My only question to that is if you have master3 set as case 3 for instance and you enter the password on the keypad before entering the first and second password would it do like I want it and have it say “Incorrect” until the other 2 passwords are entered or would it say “correct” because it still technically has a case for it? I’m sorry if that doesn’t make sense I’m not really sure how to phrase it :slight_smile:

You are overthinking it.

In my code (not in front of me now, so) I used a variable named stageNumber initialized to 1 one.

As long as stageNumber is N, the Nth password is the one you have to get right. When you get a hurdle correctly passed, stageNumber is incremented in exactly the places where you tried to accomplish the same thing with the checkPuzzle flags.

As @hallowed31 points out, the combination of the checkPuzzle flags says no more than a single number could tell you, it only makes it harder to deal with.

I also changed the block structure, so that the entirety of a stage or hurdle is within one if statement. There are other ways to do it but essential

  if (stageNumber == 2) {

// all the code for the second password, including success increments stageNumber)

  }

  if (stageNumber == 3) {

// all the code for the third password, including success increments stageNumber)

  }

I read the keyboard once until I get a valid key. It blocks, but just because I was lazy and wanted the thing to work, a hack if you will, viz:

  customKey = 0;
  while (!customKey)
    customKey = customKeypad.getKey();

Easily unblocked one day if necessary.

I repeat my opinion that that three stages are identical except for the password being checked, so could be one block of code instead of three.

It would even be easy to have the passwords get progressively longer four digits for the first, five for the second or something like that, all with the same code.

The signs are recognizable. If you are creating multiple entities by copy/paste/editing, it is an indication that loops and arrays might be useful. And functions, maybe.

This

      // Turn on relay for 5 seconds
      digitalWrite(led1, HIGH);
      delay(2500);

turns on led1 and pauses for 2.5 seconds, nothing ever turns off the LED.

L8R when I can work with the copy of your circuit that I built and see the sketch as it is for me now.

a7

This is the code that is working! Do you have any suggestions on where to possibly add more functions to make it more streamlined, when expanding to more puzzles?
Thank you for all the suggestions thus far!!

/*
Eli Turrentine
Escape room sequential keypad password system

 Code based on
  DroneBot Workshop 2020
  https://dronebotworkshop.com
 AND
  https://www.circuitbasics.com/
 
*/

// Include Arduino Wire library for I2C
#include <Wire.h>
// Include LCD display library for I2C
#include <Adafruit_LiquidCrystal.h>
// Include Keypad library
#include <Keypad.h>
// length of feedback display
const byte feedbackTime = 2000;
// Length of password + 1 for null character
#define Password_Length 5
// Character to hold password input
char Data[Password_Length];
// Passwords
char Master1[Password_Length] = "1111";
char Master2[Password_Length] = "2222";
char Master3[Password_Length] = "3333";
// Pin connected to lock relay input
int led1 = 13;
int led2 = 12;
int led3 = 11;
// Counter for character entries
byte data_count = 0;
// Character to hold key input
char customKey;
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
// Connections to Arduino
byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// puzzle selection
byte puzzleNum = 1;
// Create LCD object
Adafruit_LiquidCrystal lcd_1(0);
void setup() {
  // Setup LCD with backlight and initialize
  lcd_1.begin(16, 2);
  // set up serial display
  Serial.begin(9600);
  // Initialize LCD and print
  lcd_1.setCursor(0, 0);
  lcd_1.print("Enter Password:");
}

void loop() {
  // Look for keypress
  customKey = customKeypad.getKey();
  if (customKey) {
    // Enter keypress into array and increment counter
    Data[data_count] = customKey;
    lcd_1.setCursor(data_count, 1);
    lcd_1.print(Data[data_count]);
    data_count++;
  }

  // See if we have reached the password length
  if (data_count == Password_Length - 1) {
    lcd_1.clear();
    if (puzzleNum == 1) {
      if (!strcmp(Data, Master1)) {
     // Turn on LED1
  digitalWrite(led1, HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
    }
    if (puzzleNum == 2) {
      if (!strcmp(Data, Master2)) {
            // Turn on LED2
        digitalWrite(led2, HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
    }
    if (puzzleNum == 3) {
      if (!strcmp(Data, Master3)) {
       // Turn on LED3
         digitalWrite(led3, HIGH);
        puzzleNum++;
      } else {
        incorrectSeq();
      }
    }

    // Automate Congradulations
    if (puzzleNum == 4) {
      lcd_1.clear();
      lcd_1.print("Congradulations");
      puzzleNum++;
    }
    //Built in Reset
    if (puzzleNum == 5) {
      puzzleNum = 1;
    }
    dataClear();  //Clear Data to enter another variable
  }
}

void incorrectSeq() {
  lcd_1.print("Incorrect");
  delay(feedbackTime);
  lcd_1.clear();
  lcd_1.print("Enter Password:");
  delay(100);
}

void correctSeq() {
  // Password is correct
  lcd_1.print("Correct");
  delay(feedbackTime);
  lcd_1.clear();
  lcd_1.print("Enter Password:");
  puzzleNum++;
}

void dataClear() {
  // reset data array to zero
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
}

And yes the delay is for the "Correct" display. The led will stay lit as a backlight point on a map where players will get there next clue. I'm building this game to propose to my gf, and her and some friends will be playing and the size of the 'room' will be a 5 acre piece of land. I'm super excited to get to design this as a Gen 2 'room' so it will have electronic elements as well as physical locks and clues. Thanks again for the help :grinning:

// Passwords
char Master1[Password_Length] = "1111";
char Master2[Password_Length] = "2222";
char Master3[Password_Length] = "3333";
// Pin connected to lock relay input
int led1 = 13;
int led2 = 12;
int led3 = 11;

Any time you make multiple variables with the same name except for different digits, you should be using arrays. If the variable is already an array, it should become a 2-dimensional array:

// Passwords
char Master[Num_Puzzles][Password_Length] = {
  "1111",
  "2222",
  "3333"
};
// Pin connected to lock relay input
int led[Num_Puzzles] = {13, 12, 11};

This will enable you to extend the code to include more puzzles, passwords and LEDs without duplicating the same code lines for each puzzle.

Nice wrok. @PaulRB points the way forward. If you place the passwords in an array, the only thing different in all three blocks would be the reference to the array name. I think. I am looking through the tiny window (in transit).

Here's something I did yesterday but still haven't tested. It shows the common block for all stages, and uses the length of the collected partial password as well as direct character to character comparison, it just kept boiling down.

Please read this code while you learn about arrays in general and character arrays in C/C++ in particular.


// at stage N collect and check password N

theKey = 0;
  while (!theKey)
    theKey = theKeypad.getKey();
   
  lcd_1.setCursor(dataIndex, 1);
  lcd_1.print(theKey);

  Data[dataIndex] = theKey;
  Data[++dataIndex] = '\0';

  if (!strcmp(Data, password[stage])) {
    lcd_1.clear();
    lcd_1.print("Correct");

    digitalWrite(led1, HIGH);
    delay(2500);
    stage++; 
   }
   else  {
     if (strlen(Data) == strlen(password[stage])) {
       lcd_1.clear();
       lcd_1.print("Incorrect");
       delay(1000);
       dataIndex = 0;
      }
    }
  }

It is designed to go into the loop function. I renamed a few things that were distracting me and removed the comments, I'm not much for comments in a few dozen lines of code like this, but naturally you do you on that.

I see you have broken out some common code for functions, that also is good. I can't roll your stuff into my code here, but I think you are capable of doing if you do choose to reduce by 1/3 the code.

The above would make a nice function body, call it alla time with the stage number as an argument, and it returns a code that says not yet, right or wrong. I would use number codes, enum is a nice way to give a name to a number.

You can see that in the code, setting the dtatIndex to 0 zero is all it takes to empty a null-terminated character array, as all uses of the array are maintained with the terminator.

  Data[dataIndex] = theKey;  // stash the new character
  Data[++dataIndex] = '\0';    // make it a string for checking

There is no need for a count, and automatically it means that longer or shorter passwords are entirely a matter of making the initial character strings longer or shorter.

char *master[] = {"11", "222", "777", "314159", };
const int nPasswords = sizeof master / sizeof *master;

and you'd have four stages w/ custom lengths, and we let the compiler figure out how many with a little idiom that shows up and should be recognizable and one day exploited by you.

The stray comma if you spotted is is intentional and serbs for me or anyone as a reminder that another password can be added right here with no other changes necessary

HTH and sry if the code here still has flaws, I think it is close.

a7

OK, more me. I just read a little closer, and here

    if (puzzleNum == 2) {
      if (!strcmp(Data, Master2)) {
            // Turn on LED2
        digitalWrite(led2, HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
    }

is a perfect example. The three similar code sections are all just

      if (!strcmp(Data, Master[puzzleNum])) {
            // Turn on the associated LED
        digitalWrite(ledPin[puzzleNum], HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
  

Where there is assumed an array ledPin w/ the pin numbers for the LEDs.

It keeps me awake thjs stuff. I have no life.

a7

Requesting a second set of eyes on this one. My problem is after the correctSeq() occurs the incorrectSeq() begins. I believe this is because of the puzzleNum++ happening

void correctSeq() {
  // Password is correct
  lcd_1.print("Correct");
  delay(feedbackTime);
  lcd_1.clear();
  lcd_1.print("Enter Password:");
  puzzleNum++;
  dataClear();  //Clear Data to enter another variable
}

then it reads as incorrect for the next puzzle; then clears and allows for the next puzzle to be entered.

Here is the whole code:


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

#define Password_Length 5
#define Num_Puzzles 4

const int feedbackTime = 2000;
char Data[Password_Length];

// Passwords
char Master[Num_Puzzles][Password_Length] = {
  "1111",
  "2222",
  "3333",
  "4444",
};
byte puzzleNum = 0;
// Pin connected to lock relay input
int led[Num_Puzzles] = { 13, 12, 11, 10 };

byte data_count = 0;
int customKey;
char getKey;

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

// Create LCD object
LiquidCrystal_I2C lcd_1(0x27, 16, 2);

void setup() {
  // Setup LCD with backlight and initialize
  lcd_1.init();
  lcd_1.backlight();
  // set up serial display
  Serial.begin(9600);
  // Initialize LCD and print
  pinMode(led[0], OUTPUT);
  pinMode(led[1], OUTPUT);
  pinMode(led[2], OUTPUT);
  pinMode(led[3], OUTPUT);
}

void loop() {
  lcd_1.setCursor(0, 0);
  lcd_1.print("Enter Password:");
  // Look for keypress
  customKey = customKeypad.getKey();
  if (customKey) {
    // Enter keypress into array and increment counter
    Data[data_count] = customKey;
    lcd_1.setCursor(data_count, 1);
    lcd_1.print(Data[data_count]);
    data_count++;
  }
  // See if we have reached the password length
  if (data_count == Password_Length - 1) {
    lcd_1.clear();
    if (puzzleNum == 0) {
      if (!strcmp(Data, Master[0])) {
        // Turn on LED1
        digitalWrite(led[puzzleNum], HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
    }
    if (puzzleNum == 1) {
      if (!strcmp(Data, Master[1])) {
        // Turn on LED2
        digitalWrite(led[puzzleNum], HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
    }
    if (puzzleNum == 2) {
      if (!strcmp(Data, Master[2])) {
        // Turn on LED3
        digitalWrite(led[puzzleNum], HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
    }
    if (puzzleNum == 3) {
      if (!strcmp(Data, Master[3])) {
        // Turn on LED3
        digitalWrite(led[puzzleNum], HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
    }
    // Automate Congradulations
    if (puzzleNum == 4) {
      lcd_1.clear();
      lcd_1.print("Congradulations");
      digitalWrite(led[puzzleNum], HIGH);
      delay(5000);
      puzzleNum = 0;
      digitalWrite(led[3], LOW);
      delay(1000);
      digitalWrite(led[2], LOW);
      delay(1000);
      digitalWrite(led[1], LOW);
      delay(1000);
      digitalWrite(led[0], LOW);
      delay(1000);
    }
  }
}

void incorrectSeq() {
  lcd_1.print("Incorrect");
  delay(feedbackTime);
  lcd_1.clear();
  lcd_1.print("Enter Password:");
  dataClear();  //Clear Data to enter another variable
}

void correctSeq() {
  // Password is correct
  lcd_1.print("Correct");
  delay(feedbackTime);
  lcd_1.clear();
  lcd_1.print("Enter Password:");
  puzzleNum++;
  dataClear();  //Clear Data to enter another variable
}

void dataClear() {
  // reset data array to zero
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
}

The best kind, automatic.

The code looks plausible. But I am looking through the tiny window.

You

  Serail.begin(9600);

but appear to make no use of the serial monitor. I suggest to add printing at various places liberally to confirm that values of the variables that inform the flow through your code are plausible and what you expect.

I don't think I've ever gotten anything to work, and felt sure it was doing things the way I intended without some feedback beyond proper operation. Printing, status LEDs, logic analyser and oscilloscopes come in handy.

Of those, printing is free, easy and usually moves things along fairly rapidly.

I look later, prolly something simple.

Indulge me and pinch off the string as you build it. It may not be the problem, but it will keep ppl like me off your back as it will be clearer that it is not:

// position and print the new key press
    lcd_1.setCursor(data_count, 1);
    lcd_1.print(customKey);

// stash it in a null terminated string
    Data[data_count] = customKey;
    data_count++;
    Data[data_count] = '\0';

L8R and HTH

a7

From:

it looks like the only way incorrectSeq could be called after a correctSeq() call is if data_count isn't reset.

You should follow @alto777's advice and add some serial print debugging to check beliefs about puzzleNum and data_count. Second sets of eyes aren't as good at simulating/guessing the details as well as some good debug prints.

Yes, even in transit risking motion sickness.

Since you increment puzzleNum in correctSeq(), the next if statement is wrongly called.

    if (puzzleNum == 1) {
      if (!strcmp(Data, Master[1])) {
        // Turn on LED2
        digitalWrite(led[puzzleNum], HIGH);
        correctSeq();
      } else {
        incorrectSeq();
      }
    }
    if (puzzleNum == 2) {    // here it is 2 if we were just successful in puzzleNum == 1 section

Four different solututoins submitted for your approval and choice:

  • make the if statements into an if/else/if chain, so only one of the three sections is executed
  • use a switch/case statement, often clearer than an if/else/if chain
  • increment puzzleNum elsewhere, when it is finally time to
  • rewrite that! There is no need for three sections of nearly identical code!
// puzzle number is 1, 2 or 3 here, forget this test:    if (puzzleNum == 2) {

    if (!strcmp(Data, Master[puzzleNumber])) {
      // Turn on the LED
      digitalWrite(led[puzzleNum], HIGH);
      correctSeq();
    }
    else {
      incorrectSeq();
    }

That's the hole point of using arrays - code can be parameterized and used for any N puzzle number, as the difference are available in arrays at index N.

I may have puzzle number 1..3 wrong, as arrays go from 0 to N - 1. Can't look closer now, we've just arrived, a spectacular day now, warm enough and no breeze and no waves, that's OK.

a7

You have missed my point entirely.

The idea of using arrays is to enable you to avoid duplicating code. If you continue to duplicate the same code repeatedly, you are no better off than when you were using multiple variables!

This:

  pinMode(led[0], OUTPUT);
  pinMode(led[1], OUTPUT);
  pinMode(led[2], OUTPUT);
  pinMode(led[3], OUTPUT);

becomes:

  for (byte c=0; c<4; c++) {
    pinMode(led[c], OUTPUT);
  } 

and this:

    if (puzzleNum == 1) {
      if (!strcmp(Data, Master[1])) {
        // Turn on LED2
        digitalWrite(led[puzzleNum], HIGH);
...
    if (puzzleNum == 2) {
      if (!strcmp(Data, Master[2])) {
        // Turn on LED3
        digitalWrite(led[puzzleNum], HIGH);
...

becomes:

    if (!strcmp(Data, Master[puzzleNum])) {
      // Turn on LED for this puzzle
      digitalWrite(led[puzzleNum], HIGH);

Remember that array indexes begin at 0, not 1. So it's best to think of your puzzles starting at puzzle number 0 and so on.

WOW!

Still very new to this but that was the way to go! It fixed the multiple issues I was having and made it way more streamlined. About to go research all I can on arrays!