#include <Keypad.h>
#include "RTClib.h"
#include <EEPROM.h>
#include <UnixTime.h>
RTC_DS1307 rtc;
#define RELAY_PIN A0 // the Arduino pin, which connects to the IN pin of relay
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
String input_password;
uint32_t install_date;
uint32_t last_unlocked;
// long numbers[10];
void writeLongArrayIntoEEPROM(int address, long numbers[], int arraySize) {
int addressIndex = address;
for (int i = 0; i < arraySize; i++)
{
EEPROM.write(addressIndex, (numbers[i] >> 24) & 0xFF);
EEPROM.write(addressIndex + 1, (numbers[i] >> 16) & 0xFF);
EEPROM.write(addressIndex + 2, (numbers[i] >> 8) & 0xFF);
EEPROM.write(addressIndex + 3, numbers[i] & 0xFF);
addressIndex += 4;
}
}
void readLongArrayFromEEPROM(int address, long numbers[], int arraySize) {
int addressIndex = address;
for (int i = 0; i < arraySize; i++)
{
numbers[i] = ((long)EEPROM.read(addressIndex) << 24) +
((long)EEPROM.read(addressIndex + 1) << 16) +
((long)EEPROM.read(addressIndex + 2) << 8) +
(long)EEPROM.read(addressIndex + 3);
addressIndex += 4;
}
}
void setup() {
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.
digitalWrite(RELAY_PIN, LOW);
for (int j = 0; j < 500; j++) {
if(EEPROM.read(j) == 255) {
EEPROM.write(j, 0);
}
}
// int ARRAY_SIZE = 10;
// int STARTING_EEPROM_ADDRESS = 100;
// int numbers[ARRAY_SIZE] = {12345, 56789, 98765, 45678, 78901, 34567, 89012, 23456, 67890, 01234};
// writeIntArrayIntoEEPROM(STARTING_EEPROM_ADDRESS, numbers, ARRAY_SIZE);
// Initialize RTC
rtc.begin();
if(EEPROM.read(0) == 0){
install_date = 1694960640;
EEPROM.put(0, install_date);
}
else {
EEPROM.get(0, install_date);
}
if(EEPROM.read(50) == 0) {
last_unlocked = install_date;
}
else {
EEPROM.get(50, last_unlocked);
}
if(EEPROM.read(100) == 0) {
long numbers[10]= {12345, 56789, 98765, 45678, 78901, 34567, 89012, 23456, 67890, 96524};
writeLongArrayIntoEEPROM(100, numbers, 10);
}
else {
long numbers[10];
readLongArrayFromEEPROM(100, numbers, 10);
}
}
void loop() {
// int ARRAY_SIZE = 10;
// int STARTING_EEPROM_ADDRESS = 100;
// int newNumbers[ARRAY_SIZE];
// readIntArrayFromEEPROM(STARTING_EEPROM_ADDRESS, newNumbers, ARRAY_SIZE);
bool is_present = false;
long newNumbers[10];
readLongArrayFromEEPROM(100, newNumbers, 10);
// for(int i = 0; i < 10; i++) {
// Serial.println(newNumbers[i]);
// }
// Check if it's time to turn on the relay
DateTime now = rtc.now();
uint32_t timestamp_now = now.unixtime();
Serial.println(timestamp_now - install_date);
if (
(timestamp_now - install_date) > 60
&&
(last_unlocked-install_date) < ((((timestamp_now - install_date)/60)*60))
) {
// Turn on the relay
digitalWrite(RELAY_PIN, HIGH);
}
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '*') {
input_password = ""; // reset the input password
} else if (key == '#') {
const char* input_password_char = input_password.c_str();
long input_password_long = atol(input_password_char);
// Check if the input password matches one of the passwords
for(int i = 0; i < 10; i++) {
if(newNumbers[i] == input_password_long) {
newNumbers[i] = -1;
writeLongArrayIntoEEPROM(100, newNumbers, 10);
// long test[10];
// readLongArrayFromEEPROM(100, test, 10);
// for(int j = 0; j < 10; j++) {
// Serial.println(test[j]);
// }
is_present = true;
break;
}
}
if (is_present) {
// Turn off the relay
digitalWrite(RELAY_PIN, LOW);
last_unlocked = timestamp_now;
EEPROM.put(50, last_unlocked);
} else {
// Reset the input password
input_password = "";
}
} else {
// Append new character to input password string
input_password += key;
}
}
}