Ein Passwort im EEprom hab ich.
Aber allein hier mehrer rein, raus und vergleichen überschreitet mein Kenntnisse leider.
#include <Wire.h> // For some strange reasons, Wire.h must be included here
#include <DS1307new.h>
#define Password_Lenght 5
uint16_t startAddr = 0x0000; // Start address to store in the NV-RAM
uint16_t lastAddr; // new address for storing in NV-RAM
uint16_t TimeIsSet = 0xaa55; // Helper that time must not set again
#include <Wire.h> // Comes with Arduino IDE
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#include<EEPROM.h>
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char Data[Password_Lenght];
char Data2[Password_Lenght];
char Master[Password_Lenght];
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char key;
byte key_state = 0;
char last_press_key;
byte mode = 0;
int change_password_allow_time = 10000;
long time_old = 0;
bool just_allowed_pass =0;
char FistTimePassword[] = {'4','3','2','1'}; // setup first-time password here
void setup()
{
pinMode(2, INPUT); // Test of the SQW pin, D2 = INPUT
digitalWrite(2, HIGH); // Test of the SQW pin, D2 = Pullup on
Serial.begin(9600);
RTC.setRAM(0, (uint8_t *)&startAddr, sizeof(uint16_t));// Store startAddr in NV-RAM address 0x08
/*
Uncomment the next 2 lines if you want to SET the clock
Comment them out if the clock is set.
DON'T ASK ME WHY: YOU MUST UPLOAD THE CODE TWICE TO LET HIM WORK
AFTER SETTING THE CLOCK ONCE.
*/
// TimeIsSet = 0xffff;
// RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
/*
Control the clock.
Clock will only be set if NV-RAM Address does not contain 0xaa.
DS1307 should have a battery backup.
*/
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
if (TimeIsSet != 0xaa55)
{
RTC.stopClock();
RTC.fillByYMD(2018,8,25);
RTC.fillByHMS(10,12,0);
RTC.setTime();
TimeIsSet = 0xaa55;
RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
RTC.startClock();
}
else
{
RTC.getTime();
}
RTC.ctrl = 0x00; // 0x00=disable SQW pin, 0x10=1Hz,
// 0x11=4096Hz, 0x12=8192Hz, 0x13=32768Hz
RTC.setCTRL();
uint8_t MESZ;
MESZ = RTC.isMEZSummerTime();
Check_EEPROM();
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines and turn on backlight
lcd.clear();
}
void loop()
{
key = keypad.getKey();
key_state = keypad.getState();
if(key){
last_press_key = key;
Serial.println(key);
}
if( mode == 3){
if(last_press_key == '#' && key_state == 2){
mode = 1;
}
if(last_press_key == '*' && key_state == 2){
mode = 0;
lcd.clear();
lcd.setCursor(4,0);
lcd.print("LOCKED");
digitalWrite(13, LOW);
delay(2000);
}
}
if(mode == 0){
lcd.setCursor(1,0);
lcd.print("Enter Password");
}else if(mode == 1){
lcd.setCursor(0,0);
lcd.print("Set New Password");
}else if(mode == 2){
lcd.setCursor(0,0);
lcd.print("Password Again");
}else if(mode == 3){
lcd.setCursor(4,0);
lcd.print("UNLOCKED");
}
if (key && key != '#' && mode != 3)
{
collectKey();
}
if(data_count == Password_Lenght-1)
{
if(mode == 0){
lcd.clear();
if(!strcmp(Data, Master)) {
lcd.setCursor(2, 0);
lcd.print("WELCOME BACK");
lcd.setCursor(4, 1);
lcd.print("MASTER");
just_allowed_pass = 1;
time_old = millis();
digitalWrite(13, HIGH);
delay(2000);
mode = 3;
}else{
lcd.setCursor(2, 0);
lcd.print("INCORRECT !");
lcd.setCursor(4, 1);
lcd.print("PASSWORD");
delay(2000);
}
delay(1000);//
lcd.clear();
clearData();
// digitalWrite(13, LOW);
}else if( mode == 1){
lcd.clear();
mode = 2;
for(int i = 0; i < Password_Lenght; i = i+1){
Data2[i] = Data[i];
}
clearData();
}else if(mode == 2){
if(!strcmp(Data, Data2)){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("New Password is ");
lcd.setCursor(4, 1);
lcd.print(Data);
delay(2000);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Saving...");
for(int i =0; i <= 100; i = i+10){
lcd.setCursor(4, 1);
lcd.print(i);
lcd.setCursor(7, 1);
lcd.print("%");
delay(200);
}
EEPROM.put(0, Data);
EEPROM.get(0, Master);
delay(500);
}else{
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("PASSWORD");
lcd.setCursor(3, 1);
lcd.print("NOT MATCH!");
delay(2000);
}
mode = 3;
clearData();
lcd.clear();
}
}
}
void collectKey(){
Data[data_count] = key;
lcd.setCursor(4+data_count,1);
lcd.print("*");
data_count++;
}
void clearData()
{
while(data_count !=0)
{
Data[data_count--] = 0;
}
}
void Check_EEPROM(){
EEPROM.get(0, Master);
if(Master[0] == 0 && Master[1] == 0 && Master[2] == 0 && Master[3] == 0){ // check if EEPRM have store password ?
Serial.println("No EEPROM PASSWORD FOUND"); // if not found will burn EEPROM a first time password
EEPROM.put(0, FistTimePassword);
EEPROM.get(0, Master);
}
}
// this coding by Xmaker Channel Visit us at https://www.youtube.com/channel/UCuvSIbgie6Lz24_wh7RNnMg
// this code evolution from example by https://playground.arduino.cc/Main/KeypadPassword