#include <LiquidCrystal.h>
#define CONFIRM 2
#define ADD_LOGIN 13
#define LOGINS 10
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
typedef enum {
NAME_PASS,
ADD,
LOGIN,
LOGGED_IN
} State;
State locker_sts;
String user_name = "";
String user_passw = "";
String* logins[LOGINS];
int index = 0;
bool name=false;
void setup() {
pinMode(CONFIRM, INPUT_PULLUP);
pinMode(ADD_LOGIN, INPUT_PULLUP);
lcd.begin(20, 4);
Serial.begin(9600);
Serial.println("");
locker_sts = NAME_PASS;
}
bool confirm_login(String& name, String& passw){
for(int i=0; i<LOGINS; i++){
if(name == logins[i][0] && passw == logins[i][1]){
return true;
}
}
return false;
}
String* name_pass() {
static String login[2];
lcd.setCursor(3, 0);
lcd.print("CREATE ACCOUNT");
lcd.setCursor(0, 1);
lcd.print("LOGIN: ");
if(digitalRead(CONFIRM) == LOW){
if(!name && user_name.length() > 0){
name = true;
login[0] = user_name;
lcd.setCursor(0, 2);
lcd.print("PASS: ");
}
else if(name && user_passw.length() > 0){
locker_sts = LOGIN;
login[1] = user_passw;
lcd.clear();
clear_np();
return login;
}
}
if (Serial.available()) {
char aux = Serial.read();
if(!name){
user_name += aux;
lcd.setCursor(7, 1);
lcd.print(user_name);
}
else{
lcd.setCursor(6, 2);
user_passw += aux;
lcd.print(user_passw);
}
}
}
void login(){
lcd.setCursor(0, 0);
lcd.print("PLEASE, INSERT LOGIN");
lcd.setCursor(0, 1);
lcd.print("LOGIN: ");
if(digitalRead(CONFIRM) == LOW){
if(!name && user_name.length() > 0){
name = true;
lcd.setCursor(0, 2);
lcd.print("PASS: ");
}
else if(name && user_passw.length() > 0){
lcd.clear();
if(confirm_login(user_name, user_passw)){
locker_sts = LOGGED_IN;
lcd.print("OK");
delay(1000);
lcd.clear();
}
else{
lcd.print("No");
clear_np();
}
}
}
if (Serial.available()) {
char aux = Serial.read();
if(!name){
user_name += aux;
lcd.setCursor(7, 1);
lcd.print(user_name);
}
else{
lcd.setCursor(6, 2);
user_passw += aux;
lcd.print(user_passw);
}
}
};
void clear_np(){
user_name = "";
user_passw = "";
name = false;
}
void add(){
clear_np();
lcd.clear();
locker_sts = NAME_PASS;
index++;
}
void logged(){
lcd.setCursor(0, 0);
lcd.print("Vai se fuder " + user_name);
delay(2000);
lcd.setCursor(0, 1);
lcd.print("Saindo da conta...");
delay(1200);
locker_sts = LOGIN;
lcd.clear();
clear_np();
}
void loop() {
if(digitalRead(ADD_LOGIN) == LOW){
if(locker_sts == ADD) locker_sts = LOGIN;
else if(locker_sts == LOGIN) locker_sts = ADD;
}
switch (locker_sts) {
case NAME_PASS:
logins[index] = name_pass();
break;
case LOGIN:
login();
break;
case ADD:
add();
break;
case LOGGED_IN:
logged();
break;
}
}