#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
Servo locker;
LiquidCrystal lcd(13, 12, 11, 9, A1, A0);
char secretCode[4] = {'1', '2', '3', '4'};
char input[4];
char key;
int position = 0;
int wrong = 0;
int i = 0;
int j = 0;
int pos = 0;
int a = 0;
int b = 0;
const byte rows = 4;
const byte cols = 4;
boolean c = false;
char keys[rows][cols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPines[rows] = {8, 7, 6, 5};
byte colPines[cols] = {4, 3, 2, 1};
Keypad keypad = Keypad(makeKeymap(keys), rowPines, colPines, rows, cols);
void input_key();
void password();
void password_check();
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16, 2);
locker.attach(10);
locker.write(90);
pinMode(10, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
while (a == 0)
{
input_key();
password();
password_check();
}
int sum = a;
if(sum ==1)
{
lcd.clear();
lcd.setCursor(0, 0);
pinMode(10,OUTPUT);
locker.write(180);
lcd.print("perfect complete");
delay(2000);
pinMode(10, INPUT);
key = keypad.waitForKey();
if(key == '*')
{
a = 0;
lcd.clear();
lcd.setCursor(0, 0);
pinMode(10, OUTPUT);
locker.write(90);
lcd.print(" Lock ON");
delay(2000);
pinMode(10,INPUT);
}
}
}
void input_key()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Password");
for (i = 0; i < 4; i++)
{
key = keypad.waitForKey();
input[i] = key;
lcd.setCursor(i + 6, 1);
lcd.print("*");
if (i == 3)
{
Serial.println("");
}
}
}
void password()
{
wrong = 0;
for (j = 0; j <4; j++)
{
if (secretCode[j] != input[j])
{
}
else if (secretCode[j] != input[j])
{
wrong += 1;
}
}
}
void password_check()
{
if (wrong == 0)
{
Serial.println("correct");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" PASS");
delay(2000);
a = 1;
lcd.clear();
}
else if (wrong != 0)
{
Serial.println("wrong");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" wrong");
delay(2000);
a = 0;
lcd.clear();
wrong = 0;
}
이코드가 비밀번호가 1234가 아니여도 왜 서브모터가 돌아가고 틀려도왜 맞다고 전광판에 뜰까여...
|