Hello everyone. This is my first post here, so I'm sorry if this is in the wrong place. I searched, but didn't find any similar posts.
So, here's my problem.
I am using a keypad and passwords to control (right now just an led) my door lock. I have made code to work with one or more passwords of my choosing, that is fine.
However, I've been trying to work on coding for a new feature called "Guest Mode." In this, I can use a "Lock" password on the keypad to control whether or not a Guest password would be accepted.
I'm trying to figure it out, but when I use the Lock password, the Guest password still works.
Any help would be greatly appreciated. Thank you for your help!
AdamFier
//keypad with password
#include <Keypad.h>
#include <Servo.h>
Servo myservo; //create servo object to control a servo
const byte myRows = 4; // number of rows
const byte myCols = 3; //number of columns
char keys[myRows][myCols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
}; //character array to map the button layout of the keypad
byte rowPins[myRows] = {7, 2, 3, 5 }; //array to map keypad to MCU pins
byte colPins[myCols] = {6, 8, 4 }; //array to map keypad to MCU pins
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, myRows, myCols ); //keypad library map function
char inputArray[4]; //array to gather user keypad presses
char Main[4] = {'4','6','2','7'}; //array to hold keypad password
char Guest[4] = {'2','4','6','8'}; //array to hold the guest password
char Lock[4] = {'1','1','1','1'}; //array to hold the lock password
#define ledPin 13 //led to register keypad pressses
#define registerPin 12 //led that registers correct password entry
#define ServoPin 10 //servo that registers correct password entry
int i = 0;
int pos = 0; //variable to store the servo position
int open()
{
digitalWrite(registerPin, HIGH); //turn on registerPin led
delay(1000);
digitalWrite(registerPin, LOW);
return 0;
};
int MasterGuest()
{ //MasterGuest
{
//match input array to password array
if ((inputArray[0] == Main[0] &&
inputArray[1] == Main[1] &&
inputArray[2] == Main[2] &&
inputArray[3] == Main[3]) ||
(inputArray[0] == Guest[0] &&
inputArray[1] == Guest[1] &&
inputArray[2] == Guest[2] &&
inputArray[3] == Guest[3]))
{
open();
}
}
{
if (inputArray[0] == Lock[0] &&
inputArray[1] == Lock[1] &&
inputArray[2] == Lock[2] &&
inputArray[3] == Lock[3])
{
Master();
}
}
}
int Master()
{
{
//match input array to password array
if ((inputArray[0] == Main[0] &&
inputArray[1] == Main[1] &&
inputArray[2] == Main[2] &&
inputArray[3] == Main[3]))
{
open();
}
}
{
if (inputArray[0] == Lock[0] &&
inputArray[1] == Lock[1] &&
inputArray[2] == Lock[2] &&
inputArray[3] == Lock[3])
{
MasterGuest();
}
}
}
void setup()
{
Serial.begin(9600); //open serial porrt
pinMode(ledPin, OUTPUT); //define led pin as output
pinMode(registerPin, OUTPUT); //define led pin as output
myservo.attach(10); //attaches the servo on pin 1 to the servo object
}
void loop()
{
char key = kpd.getKey();
//if a key is presseds
if(key)
{
//turn on ledPin
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
inputArray[i] = key; //store entry into array
i++;
Serial.println(key); //print keypad character entry to serial port
if (key=='*')
{
Serial.println("Reset");
i=0; //reset i
}
if (i == 4) //if 4 presses have been made
{
MasterGuest();
{
i=0;
}
}
}
}