#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>


const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
LiquidCrystal_I2C lcd(0x27, 16, 2);

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 );

  float kml = 18.0; //kilometer bilen kører på literen
  float krl = 17.0; //pris på benzin
  float kkm = krl / kml; //kroner per kilometer

  float Akm; //asbjørn rejste km
  float Akr; //asbjørn pris
  int Akm_cell = 0;
  int Mkm_cell = Akm_cell + sizeof(float);
  int Rkm_cell = Mkm_cell + sizeof(float);
  int input;
  int personer;

  float Mkm;
  float Mkr;


  float Rkm;
  float Rkr;
  
  float Xkm;
  float Xkr;
  
  float menu;


  const int buzzer = 12;

void reset(){
  EEPROM.put(Akm_cell, 0.0);
  EEPROM.put(Mkm_cell, 0.0);
  EEPROM.put(Rkm_cell, 0.0);
  EEPROM.get(Akm_cell, Akm);
  EEPROM.get(Mkm_cell, Mkm);
  EEPROM.get(Rkm_cell, Rkm);
}

void setup(){
  //reset();
  
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("Welcome");
  lcd.setCursor(1,1);
  lcd.print("Select profile");

  EEPROM.get(Akm_cell, Akm);
  EEPROM.get(Mkm_cell, Mkm);
  EEPROM.get(Rkm_cell, Rkm);
  Serial.print("Asbjorn: "); //skriv når noget tastes på keypadden
  Serial.println(Akm); //skriv når noget tastes på keypadden
  Serial.print("Markus: "); //skriv når noget tastes på keypadden
  Serial.println(Mkm); //skriv når noget tastes på keypadden
  Serial.print("Rasmus: "); //skriv når noget tastes på keypadden
  Serial.println(Rkm); //skriv når noget tastes på keypadden

  pinMode(buzzer, OUTPUT);
  delay(100);
  tone(buzzer, 1100);
  delay(50);
  noTone(buzzer);
  tone(buzzer, 1200);
  delay(50);
  noTone(buzzer);
  tone(buzzer, 1500);
  delay(50);
  noTone(buzzer);
}

void loop(){
  char key = keypad.getKey();
  if (key){ 
    Serial.println(key); //skriv når noget tastes på keypadden
  }


   
  

  if (key == 'A'){
    Akr = kkm * Akm;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Asbjorn:");
    lcd.setCursor(0,1);
    lcd.print((int)Akr);
    lcd.print(" kr | ");
    lcd.print((int)Akm);
    lcd.print(" km");
    menu = 1; // sæt menu til 1
  } else if ((key == '*') && (menu == 1)){
    menu = 2;
    input = 0;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("indtast km:");
    lcd.setCursor(0,1);
  } else if ((key >= '0' && key <= '9') && (menu == 2)){
    lcd.print(key);
    input = input * 10 + key - '0';
    Serial.println(input);
  } else if (key == '*' && menu == 2 && input > 0){
    menu = 3;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("indtast personer:");
    lcd.setCursor(0,1);
  } else if ((key >= '0' && key <= '9') && (menu == 3)){
    menu = 4;
    lcd.print(key);
    personer = key - '0';
  } else if (key == '*' && menu == 4 && personer > 0){
    Akm = Akm + (input / (float)personer);
    EEPROM.put(Akm_cell, Akm);
    Akr = kkm * Akm;
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("registreret");
    tone(buzzer, 1000);
    delay(50);
    noTone(buzzer);
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Asbjorn:");
    lcd.setCursor(0,1);
    lcd.print((int)Akr);
    lcd.print(" kr | ");
    lcd.print((int)Akm);
    lcd.print(" km");
    Serial.println(Akr);
    menu = 1; // sæt menu til 1
  } else if (key == '#'){
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Welcome");
    lcd.setCursor(1,1);
    lcd.print("Select profile");
    menu = 0;
  }  

    if (key == 'B'){
    Mkr = kkm * Mkm;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Markus:");
    lcd.setCursor(0,1);
    lcd.print((int)Mkr);
    lcd.print(" kr | ");
    lcd.print((int)Mkm);
    lcd.print(" km");
    Serial.println(Mkr);
    menu = 5; // sæt menu til 1
  } else if ((key == '*') && (menu == 5)){
    menu = 6;
    input = 0;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("indtast km:");
    lcd.setCursor(0,1);
  } else if ((key >= '0' && key <= '9') && (menu == 6)){
    lcd.print(key);
    input = input * 10 + key - '0';
    Serial.println(input);
  } else if (key == '*' && menu == 6 && input > 0){
    menu = 7;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("indtast personer:");
    lcd.setCursor(0,1);
  } else if ((key >= '0' && key <= '9') && (menu == 7)){
    menu = 8;
    lcd.print(key);
    personer = key - '0';
  } else if (key == '*' && menu == 8 && personer > 0){
    Mkm = Mkm + (input / (float)personer);
    EEPROM.put(Mkm_cell, Mkm);
    Mkr = kkm * Mkm;
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("registreret");
    tone(buzzer, 1000);
    delay(50);
    noTone(buzzer);
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Markus:");
    lcd.setCursor(0,1);
    lcd.print((int)Mkr);
    lcd.print(" kr | ");
    lcd.print((int)Mkm);
    lcd.print(" km");
    Serial.println(Mkr);
    menu = 5; // sæt menu til 1
  } else if (key == '#'){
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Welcome");
    lcd.setCursor(1,1);
    lcd.print("Select profile");
    menu = 0;
  }

  if (key == 'C'){
    Rkr = kkm * Rkm;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Rasmus:");
    lcd.setCursor(0,1);
    lcd.print((int)Rkr);
    lcd.print(" kr | ");
    lcd.print((int)Rkm);
    lcd.print(" km");
    Serial.println(Rkr);
    menu = 9; // sæt menu til 1
  } else if ((key == '*') && (menu == 9)){
    menu = 10;
    input = 0;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("indtast km:");
    lcd.setCursor(0,1);
  } else if ((key >= '0' && key <= '9') && (menu == 10)){
    lcd.print(key);
    input = input * 10 + key - '0';
    Serial.println(input);
  } else if (key == '*' && menu == 10 && input > 0){
    menu = 11;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("indtast personer:");
    lcd.setCursor(0,1);
  } else if ((key >= '0' && key <= '9') && (menu == 11)){
    menu = 12;
    lcd.print(key);
    personer = key - '0';
  } else if (key == '*' && menu == 12 && personer > 0){
    Rkm = Rkm + (input / (float)personer);
    EEPROM.put(Rkm_cell, Rkm);
    Rkr = kkm * Rkm;
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("registreret");
    tone(buzzer, 1000);
    delay(50);
    noTone(buzzer);
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Rasmus:");
    lcd.setCursor(0,1);
    lcd.print((int)Rkr);
    lcd.print(" kr | ");
    lcd.print((int)Rkm);
    lcd.print(" km");
    Serial.println(Rkr);
    menu = 9; // sæt menu til 1
  } else if (key == '#'){
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Welcome");
    lcd.setCursor(1,1);
    lcd.print("Select profile");
    menu = 0;
  }


  
  if (key == 'D'){
    Xkr = kkm * Xkm;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Enkelt tur:");
    lcd.setCursor(0,1);
    lcd.print((int)Xkr);
    lcd.print(" kr | ");
    lcd.print((int)Xkm);
    lcd.print(" km");
    Serial.println(Xkr);
    menu = 13; // sæt menu til 1
  } else if ((key == '*') && (menu == 13)){
    menu = 14;
    input = 0;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("indtast km:");
    lcd.setCursor(0,1);
  } else if ((key >= '0' && key <= '9') && (menu == 14)){
    lcd.print(key);
    input = input * 10 + key - '0';
    Serial.println(input);
  } else if (key == '*' && menu == 14 && input > 0){
    menu = 15;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("indtast personer:");
    lcd.setCursor(0,1);
  } else if ((key >= '0' && key <= '9') && (menu == 15)){
    menu = 16;
    lcd.print(key);
    personer = key - '0';
  } else if (key == '*' && menu == 16 && personer > 0){
    Xkm = Xkm + (input / (float)personer);
    Xkr = kkm * Xkm;
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("registreret");
    tone(buzzer, 1000);
    delay(50);
    noTone(buzzer);
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Enkelt tur:");
    lcd.setCursor(0,1);
    lcd.print((int)Xkr);
    lcd.print(" kr | ");
    lcd.print((int)Xkm);
    lcd.print(" km");
    Serial.println(Xkr);
    menu = 13; // sæt menu til 1
  } else if (key == '#'){
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Welcome");
    lcd.setCursor(1,1);
    lcd.print("Select profile");
    menu = 0;
  }
  
  /********************
   * Slet al data
   *******************/
  if (key == '*' && menu == 0){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Slet alt data?");
    lcd.setCursor(0,1);
    lcd.print("0 = ja, # = nej");
    menu = 17; // sæt menu til 1
  } else if ((key == '0') && (menu == 17)){
    reset();
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Data slettet");
    tone(buzzer, 1000);
    delay(50);
    noTone(buzzer);
    delay(1000);
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Welcome");
    lcd.setCursor(1,1);
    lcd.print("Select profile");
    menu = 0;
  } else if (key == '#' && menu == 17){
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Welcome");
    lcd.setCursor(1,1);
    lcd.print("Select profile");
    menu = 0;
  } 
}