0

I am working on a puzzle for my nephew. I currently have an arduino mega, SD card reader and keypad. Everything is wired correctly and I have it currently programmed so when you enter the right password than an audio file plays and a green LED lights up. Where I am struggling is I do not know how to program it so that when the wrong pass code is entered then my red LED light flashes and the code resets to position 0. I also noticed that my current password is 1,2,3 and when I add a random number between my pass code, say, 1,5,2,3 or even 1,3,2,2,3 then the audio file still plays and my green LED lights up. Any suggestions?

#include <Keypad.h>
#include <SD.h>                           
#include <TMRpcm.h>    
#define SD_ChipSelectPin 53                

TMRpcm tmrpcm;                           
char* password = "123";  
int position = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = { 8, 7, 6, 9 };
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int R = 12;
int G = 13;

void setup(){
  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);

  LockedPosition(true); 
  tmrpcm.speakerPin = 46;                                                                                                  
  if (!SD.begin(SD_ChipSelectPin)) {          
    return;                              
  }  
}

void loop()
{
  char key = keypad.getKey();
  if (key == '*' || key == '#')
  {
    position = 0;
    LockedPosition(true);
  }
  if (key == password[position])
  {
    position ++;
  } 
  if (position == 3)
  {
    LockedPosition(false);
  }
  delay(100);
}

void LockedPosition(int locked)
{
  if (locked)
  {
    digitalWrite(R, HIGH);
    digitalWrite(G, LOW);
    position=0;
  }
  else
  {
    digitalWrite(R, LOW);
    digitalWrite(G, HIGH);
    tmrpcm.setVolume(6);                   
    tmrpcm.play("matt.wav"); ;
    delay(10000);
    digitalWrite(G,LOW);
  }
}

2 Answers 2

1

Define two Strings:

String password = "123";
String answer = "";

replace the loop function with:

void loop()
{
        char key = keypad.getKey(); // init. variable key keypad is non blocking
        while(key == NO_KEY)                 // we got no key press? -> do again
        {
                key = keypad.getKey();
                delay(100);
        }
        answer.concat(key);         //add key to answer
        if(answer.equals(password)) //password OK
        {
                LockedPosition(false);
                answer="";
        }
        else
        {
                LockedPosition(true);
        }

        if (answer.length() > password.length()) { // answer is different and bigger than password
                                                   // can't become the right answer
                answer = "";
        }


        if (key == '*' || key == '#')
        {
                LockedPosition(true);
                answer="";
        }


}

That should do the job.

3
  • Hi @JoB. Unfortunately I got no response from the keypad when I substituted my void loop() with the one posted above. Any reason why this may be? Commented Dec 13, 2017 at 17:58
  • Hi @MattGargiulo I have edited my answer. I missed that getKey is not waiting for a key (non blocking). Now it will wait for a key and then add it to the answer String. Hope that will work for you. I have worked with keypads before but I do not have a keypad here to test that code. So try this and report here if it does not work.
    – JoB
    Commented Dec 13, 2017 at 19:21
  • you are the man! Can i integrate your code with two different passwords. My ultimate goal is to set up a puzzle for my nephew where one combo gives 1 clue and a different combo gives another clue. Commented Dec 13, 2017 at 20:27
0

You have the part checking if the key equals the position in the password and if so you increment the position. But if it is not equal then you don't do anything. You should probably have an else there that resets the position to 0 and tells the user they entered the wrong password.

1
  • Thanks for the comment. Could provide an example code to correct this issue. I tried several different else statements but nothing seemed to work. For example: I tried: if if (key == password[position]) { position ++; } if (position == 3) { LockedPosition(false); } else{ digitalWrite(R,HIGH); delay(250); digitalWrite(R,LOW); delay(250); digitalWrite(R,HIGH); position=0; } delay(100); } Commented Dec 12, 2017 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.