The servo is going to be used to rotate a lock, so it doesnt need to keep itself in the same place - it wont be moved unless I tell it to. Also, I have a detach delay that lets me control how long until they get detached. Here is the attach/detach code Ive been using to solve the problem:
#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
bool state = true;
bool doorOpen;
bool flashState = true;
int currentLength = 0;
int i = 0;
int screenDelay = 1000;
int detachDelay = 500;
long interval = 500;
long flashInterval = 2000;
long previousMillis, previousFlashMillis;
char password[4] = {
0,0,0,0};
char entered[4];
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{
'1','2','3' }
, {
'4','5','6' }
, {
'7','8','9' }
, {
'*','0','#' }
};
byte rowPins[ROWS] = {
3, 2, 1, 0};
byte colPins[COLS] = {
7, 6, 5};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
Servo servo; // create servo object to control a servo
void setup()
{
servo.attach(4); // attaches the servo on pin 0 to the servo object
servo.write(0);
delay(10);
servo.write(0);
delay(detachDelay);
servo.detach();
lcd.begin(2, 16);
lcd.setCursor(0,0);
lcd.print(" Please enter a ");
while (currentLength < 4)
{
if (millis() - previousFlashMillis > flashInterval)
{
previousFlashMillis = millis();
if (flashState)
{
lcd.setCursor(0,0);
lcd.print("4-digit password");
flashState = false;
}
else
{
lcd.setCursor(0,0);
lcd.print(" Please enter a ");
flashState = true;
}
}
char key = keypad.getKey();
if (key != NO_KEY)
{
lcd.setCursor(currentLength + 6, 1);
lcd.print(key);
password[currentLength] = key;
currentLength++;
//delay(200);
}
if (millis() - previousMillis > interval)
{
previousMillis = millis();
if (state)
{
lcd.setCursor(currentLength + 6, 1);
lcd.cursor();
state = false;
}
else
{
lcd.setCursor(currentLength + 6, 1);
lcd.noCursor();
state = true;
}
}
delay(20);
}
if (currentLength == 4)
{
lcd.noCursor();
lcd.setCursor(0,0);
lcd.print("Your password is");
delay((screenDelay + detachDelay) * 2);
currentLength = 0;
}
}
void loop()
{
if (!doorOpen)
{
lcd.setCursor(0,0);
lcd.print(" Enter password ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(6, 1);
while (currentLength < 4)
{
char key = keypad.getKey();
if (key != NO_KEY)
{
lcd.print(key);
entered[currentLength] = key;
currentLength++;
delay(200);
lcd.noCursor();
lcd.setCursor(currentLength + 5, 1);
lcd.print("*");
lcd.setCursor(currentLength + 6, 1);
lcd.cursor();
}
if (millis() - previousMillis > interval)
{
previousMillis = millis();
if (state)
{
lcd.cursor();
state = false;
}
else
{
lcd.noCursor();
state = true;
}
}
delay(20);
}
if (currentLength == 4)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3])
{
lcd.noCursor();
lcd.setCursor(0,0);
lcd.print("--- Password ---");
lcd.setCursor(0,1);
lcd.print("--- Accepted! --");
servo.attach(4);
servo.write(90);
delay(20);
servo.write(90);
delay(detachDelay);
servo.detach();
delay(screenDelay);
lcd.setCursor(0,0);
lcd.print("----- Door -----");
lcd.setCursor(0,1);
lcd.print("---- opened ----");
delay(screenDelay + detachDelay);
currentLength = 0;
doorOpen = true;
}
else
{
lcd.noCursor();
lcd.setCursor(0,0);
lcd.print("--- Password ---");
lcd.setCursor(0,1);
lcd.print("--- Incorrect! --");
delay(screenDelay + detachDelay);
currentLength = 0;
}
}
}
else
{
lcd.setCursor(0,0);
lcd.print("--- Press # ----");
lcd.setCursor(0,1);
lcd.print("--- to lock. ---");
while (doorOpen)
{
char key = keypad.getKey();
if (key != NO_KEY && key == '#')
{
lcd.setCursor(0,0);
lcd.print("----- Door -----");
lcd.setCursor(0,1);
lcd.print("---- locked ----");
servo.attach(4);
servo.write(0);
delay(20);
servo.write(0);
delay(detachDelay);
servo.detach();
delay(screenDelay);
doorOpen = false;
}
}
}
}