#include <EEPROM.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include "OneButton.h"
OneButton button(8, true);
String passKey = "EEE20003";
String patrick = "103073458";
String evan = "103614750";
String michael = "103597806";
int ID1_length = patrick.length() + 25;
int ID2_length = evan.length() + 50;
int ID3_length = michael.length() + 75;
int length = passKey.length() + 100;
int j = 0;
int menuOpt;
bool initAccess = false;
bool menuAccess = false;
// Uncomment according to your hardware type
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
// Defining size, and output pins
#define MAX_DEVICES 7
#define CS_PIN 53
MD_Parola Display = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Display.begin();
Display.displayClear();
Display.setTextAlignment(PA_CENTER);
button.attachClick(singleclick);
button.attachLongPressStop(longclick);
storageEEPROM();
Serial.println("");
}
int ID = 0;
String Pass;
// Rudimentary menu system
void menu() {
menuAccess = true;
while (Serial.available() == 0) {} // freeze program until input is given
String attempt = Serial.readStringUntil('\n');
menuOpt = attempt.toInt();
/*
if (menuOpt == 1) {
Display.print("Option 1");
} else if (menuOpt == 2) {
Display.print("Option 2");
} else if (menuOpt == 3) {
Display.print("Option 3");
} else {
menuAccess = false;
Display.print("TRY AGAIN");
}
}
*/
int menuselect = Serial.parseInt();
switch (menuselect){
case 1:
Serial.println("The password change function");
break;
case 2:
Serial.println("Option 2");
break;
case 3:
//sensor code goes here
Serial.println("Option 3");
break;
default:
Serial.println("Please choose a valid selection");
}
}
// Function to compare input to saved EEPROM
void keyEntry() {
bool result = true;
while (Serial.available() == 0) {} // freeze program until input is given
String attempt = Serial.readStringUntil('\n');
// for loop to compare input with EEPROM
j = 0;
for (int i = 100; i < (length); i++) {
char check = EEPROM.read(i);
if (check != attempt[j]) {
result = false;
}
j++;
}
// Make sure length of input is correct
if (attempt.length() != 8) {
result = false;
}
// saying whether or not correct password was entered
if (result == true) {
Display.print("GRANTED");
initAccess = true;
Serial.println("1: Change user ID's");
Serial.println("2: Temperature Sensor");
Serial.println("3: Enter user ID");
} else {
Display.print("DENIED");
}
}
int singleclick()
//chose number between 0-9, button will change it
{
ID++;
if (ID > 9) {
ID = 0;
}
//the input ID will be displayed on the matrix
Serial.println(ID);
}
int longclick()
//select button to password
{
Pass += ID;
ID = 0;
Serial.println(Pass);
if (Pass.length() == 9 && Pass.toFloat() != 0)
{
if (Pass == "100000000") {
}
else {
Serial.println("Denied");
}
//verification code placed here printing result
Pass = "";
}
}
// compare passcode to EEPROM
void passEntry() {
int result = 0;
bool isMichaelEqual = true;
bool isPatrickEqual = true;
bool isEvanEqual = true;
while (Serial.available() == 0) {} // freeze program until input is given
String attempt = Serial.readStringUntil('\n');
// for loop to compare input with EEPROM
j = 0;
for (int i = 25; i < (ID1_length); i++) {
char check = EEPROM.read(i);
if (check != attempt[j]) {
isPatrickEqual = false;
}
j++;
}
j = 0;
for (int i = 50; i < (ID2_length); i++) {
char check = EEPROM.read(i);
if (check != attempt[j]) {
isEvanEqual = false;
}
j++;
}
j = 0;
for (int i = 75; i < (ID3_length); i++) {
char check = EEPROM.read(i);
if (check != attempt[j]) {
isMichaelEqual = false;
}
j++;
}
// Make sure length of input is correct
if (attempt.length() != 9) {
result = 0;
}
// Set result dependent on which ID it matches
if (isMichaelEqual) {
result = 1;
} else if (isEvanEqual) {
result = 2;
} else if (isPatrickEqual) {
result = 3;
} else {
result = 0;
}
// saying whether or not correct password was entered
if (result == 0) {
Display.print("NICE TRY :D");
} else if (result == 1) {
Display.print("GRANTED M");
} else if (result == 2) {
Display.print("GRANTED E");
} else if (result == 3) {
Display.print("GRANTED P");
}
}
void storageEEPROM() {
j = 0;
for (int i = 25; i < ID1_length; i++) {
EEPROM[i] = patrick.charAt(j);
j++;
}
j = 0;
for (int i = 50; i < ID2_length; i++) {
EEPROM[i] = evan.charAt(j);
j++;
}
j = 0;
for (int i = 75; i < ID3_length; i++) {
EEPROM[i] = michael.charAt(j);
j++;
}
j = 0;
// Putting passKey into EEPROM at specific address
for (int i = 100; i < length; i++) { // For loop for length of passKey
EEPROM[i] = passKey.charAt(j); // Insert each individual character
j++;
}
}
void loop() {
button.tick();
// put your main code here, to run repeatedly:
while (initAccess == false) {
keyEntry();
}
while (menuAccess == false) {
menu();
}
passEntry();
Serial.println("");
}