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);
}
}