I want the servo motor to rotate 90 degrees, and then stop the lock function.
and I want the servo motor to rotate revers to its original position in the Unlock function.
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define Password_Lenght 5
Servo servol;
LiquidCrystal_I2C lcd(0x27, 16, 2);
char Data_one[Password_Lenght];
char Lock_Code[Password_Lenght];
char UnLock_Code[Password_Lenght];
char master1[Password_Lenght] = "2222";
char master2[Password_Lenght] = "3333";
byte maxPasswordLength = 4;
byte currentPasswordLength = 0;
byte data_count_one = 0;
boolean pos0 = false;
boolean pos1 = false;
boolean pos2 = false;
boolean pos3 = false;
boolean p0 = false;
boolean p1 = false;
boolean p2 = false;
boolean p3 = false;
boolean pp0 = false;
boolean pp1 = false;
boolean pp2 = false;
boolean pp3 = false;
char customKey_one;
int pos = 0;
boolean state_lock = false; //คือยังไม่ล็อค
boolean mstate_lock1 = false;
boolean mstate_lock2 = false;
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] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
servol.attach(9);
lcd.begin();
lcd.backlight();
Serial.begin(9600);
}
void lock()
{
//Motor moves to a position 90 degrees and stop
}
void unlock()
{
//Motor moves back to its original position and stop.
}
void loop()
{
if(customKey_one = keypad.getKey())
{
if(customKey_one != '#')
{
if (customKey_one != '*')
{
Data_one[data_count_one] = customKey_one;
if(data_count_one >= 4)
{
data_count_one=0;
lcd.clear();
lcd.setCursor(0, 0);
Data_one[data_count_one] = customKey_one;
lcd.setCursor(data_count_one,1);
lcd.print(Data_one[data_count_one]);
data_count_one++;
}else
{
Data_one[data_count_one] = customKey_one;
lcd.setCursor(data_count_one,1);
lcd.print(Data_one[data_count_one]);
data_count_one++;
}
if(state_lock == true)
{
lcd.setCursor(0, 0);
lcd.print("Locked");
}
}else
//Unlock process
if(state_lock == true)
{
if (data_count_one == 4)
{
for (int i=0; i<=3; i++)
{
if (UnLock_Code[0]==Lock_Code[0])
{ pos0=true;
}else {pos0=false;}
if (UnLock_Code[1]==Lock_Code[1])
{ pos1=true;
}else {pos1=false;}
if (UnLock_Code[2]==Lock_Code[2])
{ pos2=true;
}else {pos2=false;}
if (UnLock_Code[3]==Lock_Code[3])
{ pos3=true;
}else {pos3=false;}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (pos0&&pos1&&pos2&&pos3)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Unlocked");
delay(500);
lcd.clear();
unlock();
state_lock = false;
data_count_one=0;
//pos0=false;
//pos1=false;
//pos2=false;
//pos3=false;
}else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Code");
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Locked");
data_count_one=0;
}
}else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("4 Number Only");
delay(200);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Locked");
data_count_one=0;
}
}
}else
//Lock process
if(state_lock == false)
{
if (data_count_one == 4)
{
for (int i=0; i<=3; i++)
{
Lock_Code[i] = Data_one[i];
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Locked");
lock();
state_lock = true;
data_count_one=0;
}else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("4 Number Only");
data_count_one=0;
}
}
}
}