Ok, so I decided to enter the full code as I have it for better legibility, the reason I didnt use lidOpen was because I've already named that variable so when the servo is turned to point 160 as a const int at the very top. I changed the variable name to lidUp and rather than use a boolean Im using an int to see if its 0 then it means the lid is closed, if the int is 1 then it means the lid is Open. I figured out how to store the array password so that when a user enters a new password when the lid is open it would overwrite the old one. But now it also overwrites the closing password and doesnt allow me to close it. If I add the close() function to the follwing:
if (lidUp = 1)
{
for (int k=0; k<4; k++)
{
inputArray[k] = key;
inputArray[k] = Open[k];
}
lidUp = 0;
}
the servo opens and closes right away without giving me the chance to enter a new password.
Here is the full code:
//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
const int lidOpen = 160; //Servo Value to Open Lid
const int lidClosed = 10; //Servo Value to Close Lid
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] = {8, 7, 6, 5 }; //array to map keypad to MCU pins
byte colPins[myCols] = {4, 3, 2 }; //array to map keypad to MCU pins
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, myRows, myCols ); //keypad library map function
char inputArray[5]; //array to gather user keypad presses
char Open[4] = {'1','2','3','4'}; //array to hold keypad password
char Close[4] = {'0','0','0','0'}; //array to hold the guest password
// char Lock[5] = {'1','1','1','1','#'}; //array to hold the lock password
int ledPin = 13; //led to register keypad pressses
int registerPin = 12; //led that registers correct password entry
int ServoPin = 9; //servo that registers correct password entry
int i = 0;
int pos = 0; //variable to store the servo position
int lidUp = 0; // variable that determines whether the lid is currently closed (0) or open (1)
int open()
{
digitalWrite(registerPin, HIGH); //turn on registerPin led
delay(1000);
digitalWrite(registerPin, LOW);
myservo.write(lidOpen);
delay(100);
return 0;
};
int close()
{
myservo.write(lidClosed);
delay(100);
}
int MasterFunction()
{
{
//match input array to password array
// array that controls the function for servo to open
if (inputArray[0] == Open[0] &&
inputArray[1] == Open[1] &&
inputArray[2] == Open[2] &&
inputArray[3] == Open[3] )
{
Serial.println("Safe opening...");
open();
delay(1000);
}
else
{
Serial.println("Sorry, incorrect password.");
delay(300);
digitalWrite (ledPin, HIGH);
delay(300);
digitalWrite (ledPin, LOW);
delay(300);
digitalWrite (ledPin, HIGH);
delay(300);
digitalWrite (ledPin, LOW);
delay(300);
digitalWrite (ledPin, HIGH);
delay(300);
digitalWrite (ledPin, LOW);
delay(300);
}
if (inputArray[0] == Close[0] &&
inputArray[1] == Close[1] &&
inputArray[2] == Close[2] &&
inputArray[3] == Close[3] )
{
Serial.println("Safe now closing...");
close();
delay(1000);
lidUp = 0;
}
}
}
void setup()
{
myservo.write(lidClosed);
Serial.begin(9600); //open serial porrt
pinMode(ledPin, OUTPUT); //define led pin as output
pinMode(registerPin, OUTPUT); //define led pin as output
myservo.attach(9); //attaches the servo on pin 9 to the servo object
}
void loop()
{
char key = kpd.getKey();
//if a key is pressed
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 (key=='#')
{
Serial.println("Password submitted");
//MasterFunction();
i = 0;
}
if (lidUp = 1)
{
for (int k=0; k<4; k++)
{
inputArray[k] = key;
inputArray[k] = Open[k];
}
lidUp = 0;
}
if (lidUp = 0)
close();
}
}