//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
int i=0;
int password[4];
int newP[8];
int j=0;
int database[4];
char key;
int startF=0;
int changef;
int newpassword=0;
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int authF=0;
void mainScreen(){
lcd.setCursor(0,0);
lcd.print("A-start. ");
lcd.setCursor(0,2);
lcd.print("B-change password.");
}
void start(){
lcd.setCursor(0,0);
lcd.print("T=30.7C");
lcd.print(" H=77.2%");
lcd.setCursor(0,1);
lcd.print("CO2=120");
lcd.print(" BT=37.5");
lcd.setCursor(0,2);
lcd.print("dirty=true.");
lcd.setCursor(0,3);
lcd.print("cry=true.");
lcd.setCursor(13,3);
lcd.print("1-stop.");
key = keypad.getKey();
if (key != NO_KEY) {
if(key=='1'){
lcd.clear();
startF=0;
}
}
}
void showStartupMessage() {
lcd.setCursor(4, 0);
lcd.print("Baby Care");
delay(1000);
lcd.setCursor(4, 2);
String message = "Welcome!";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
void setup()
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
showStartupMessage() ;
lcd.clear();
Serial.begin(9600);
EEPROM.write(0, 1);
EEPROM.write(1, 2);
EEPROM.write(2, 3);
EEPROM.write(3, 4);
}
bool compare(int a[8]){
if(a[0]==a[4]&&a[1]==a[5]&&a[2]==a[6]&&a[3]==a[7])
return true;
else
return false;
}
void authScreen(){
lcd.setCursor(0,0);
lcd.print("enter password");
lcd.setCursor(4,3);
lcd.print("_ _ _ _");
key = keypad.getKey();
if (key != NO_KEY){
password[i]=key-48;
lcd.setCursor(4+(3*i),2);
lcd.print("* ");
i++;
if(i==4){
for(int i=0;i<4;i++){
database[i]=EEPROM.read(i);
}
if(password[0]==database[0]&&password[1]==database[1]&&password[2]==database[2]&&password[3]==database[3]){
lcd.clear();
lcd.print("correct");
i=0;
delay(2000);
startF=1;
lcd.clear();
}
else {
i=0;
lcd.clear();
lcd.print("wrong");
delay(1000);
}}
}
}
void changeScreen(){
lcd.setCursor(0,0);
lcd.print("enter old password");
lcd.setCursor(4,3);
lcd.print("_ _ _ _");
key = keypad.getKey();
if (key != NO_KEY){
password[i]=key-48;
lcd.setCursor(4+(3*i),2);
lcd.print("* ");
i++;
if(i==4){
for(int i=0;i<4;i++){
database[i]=EEPROM.read(i);
}
if(password[0]==database[0]&&password[1]==database[1]&&password[2]==database[2]&&password[3]==database[3]){
lcd.clear();
i=0;
newpassword=1;
changef=0;
}
else {
i=0;
lcd.clear();
lcd.print("wrong");
delay(1000);
}}
}
}
void newPassword(){
lcd.setCursor(0,0);
if(j==0)
lcd.print("enter new password");
else
lcd.print("confirm password ");
lcd.setCursor(4,3);
lcd.print("_ _ _ _");
key = keypad.getKey();
if(i==4){
j=1;
lcd.setCursor(0,2);
lcd.print(" ");
}
if(i==8){
boolean check=compare(newP);
if(check==true){
for(int i=0;i<4;i++){
EEPROM.write(i,newP[i]);
}
i=0;
newpassword=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("password changed");
delay(1000);
}
else {
i=0;
lcd.clear();
lcd.print("wrong");
delay(1000);}
}
if (key != NO_KEY){
newP[i]=key-48;
if(j==0)
lcd.setCursor(4+(3*i),2);
else
lcd.setCursor(4+(3*(i-4)),2);
lcd.print("* ");
i++;
}
}
void loop()
{
if(startF==1){
authF=0;
changef=0;
start();
}
if(newpassword==1){
newPassword();
}
if(changef==1){
changeScreen();
}
if(authF==0&&startF==0&&changef==0&&newpassword==0){
mainScreen();
}
if(authF==1){
authScreen();}
key = keypad.getKey();
if (key != NO_KEY) {
if(key=='A'){
lcd.clear();
authF=1;
}
if(key=='B'){
lcd.clear();
changef=1;
}
}
}