#include <LiquidCrystal.h> 
#include <EEPROM.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define bt_up     A0
#define bt_down   A1
#define bt1       A2

long da1, da2, da3, da4, da5;
long ea1, ea2, ea3, ea4, ea5;

long db1, db2, db3, db4, db5;
long eb1, eb2, eb3, eb4, eb5;

long valuea1 ;
long valueb1 ;

bool valb1A = 1;
bool valb1B = 1;
bool valb2A = 1;
bool valb2B = 1;

void setup(){
  
 pinMode(bt_up,    INPUT_PULLUP);
 pinMode(bt_down,  INPUT_PULLUP);
 pinMode(bt1,      INPUT_PULLUP);
 Serial.begin(115200);

if(EEPROM.read(50)==0){
}else{WriteEeprom();}

EEPROM.write(50,0); 
ReadEeprom();
}

void loop(){  
  valb1B = digitalRead(bt_up);
if(valb1A == 1 && valb1B == 0){ 
  da1=da1+1; // แก้ค่า เพิ่มค่าหลักละเท่าไหร่ ที่นี่ da1 = หลักหน่วย da2 = หลักสิบ da3 = หลักร้อย da4 = หลักพัน da5 = หลักหมื่น
  if(da1>9){da1=0; da2=da2+1;}  
  if(da2>9){da2=0; da3=da3+1;}
  if(da3>9){da3=0; da4=da4+1;} 
  if(da4>9){da4=0; da5=da5+1;} 
  if(da5>9){da5=0;}
  db1=db1+1;
  if(db1>9){db1=0; db2=db2+1;}  
  if(db2>9){db2=0; db3=db3+1;}
  if(db3>9){db3=0; db4=db4+1;} 
  if(db4>9){db4=0; db5=db5+1;} 
  if(db5>9){db5=0;} 
  WriteEeprom(); 
}
  valb1A = valb1B;
  valb2B = digitalRead(bt_down);
if(valb2A == 1 && valb2B == 0){ 
  da1=da1-1; // แก้ค่า ลดค่าหลักละเท่าไหร่ ที่นี่ da1 = หลักหน่วย da2 = หลักสิบ da3 = หลักร้อย da4 = หลักพัน da5 = หลักหมื่น
  if(da1<0){da1=9; da2=da2-1;}  
  if(da2<0){da2=9; da3=da3-1;} 
  if(da3<0){da3=9; da4=da4-1;} 
  if(da4<0){da4=9; da5=da5-1;} 
  if(da5<0){da5=9;} 
  db1=db1-1;
  if(db1<0){db1=9; db2=db2-1;}  
  if(db2<0){db2=9; db3=db3-1;} 
  if(db3<0){db3=9; db4=db4-1;} 
  if(db4<0){db4=9; db5=db5-1;} 
  if(db5<0){db5=9;}
  WriteEeprom(); 
} 
  valb2A = valb2B;

ea1 = da1;
ea2 = da2;
ea3 = da3;
ea4 = da4;
ea5 = da5;

eb1 = db1;
eb2 = db2;
eb3 = db3;
eb4 = db4;
eb5 = db5;

valuea1 = (ea1=ea1*1)+(ea2=ea2*10)+(ea3=ea3*100)+(ea4=ea4*1000)+(ea5=ea5*10000);
valueb1 = (eb1=eb1*1)+(eb2=eb2*10)+(eb3=eb3*100)+(eb4=eb4*1000)+(eb5=eb5*10000);

if(digitalRead(bt1) == LOW){ delay(100); Serial.println(valuea1);}
lcd.setCursor(1,1);
if(valuea1<10){lcd.print("0");}
if(valuea1<100){lcd.print("0");}
if(valuea1<1000){lcd.print("0");}
if(valuea1<10000){lcd.print("0");}
lcd.print(valuea1);

lcd.setCursor(10,2);
if(valueb1<10){lcd.print("0");}
if(valueb1<100){lcd.print("0");}
if(valueb1<1000){lcd.print("0");}
if(valueb1<10000){lcd.print("0");}
lcd.print(valueb1);
//lcd.clear();
}

void ReadEeprom() {
da1=EEPROM.read(1);
da2=EEPROM.read(2);
da3=EEPROM.read(3);
da4=EEPROM.read(4);
da5=EEPROM.read(5);

db1=EEPROM.read(6);
db2=EEPROM.read(7);
db3=EEPROM.read(8);
db4=EEPROM.read(9);
db5=EEPROM.read(10);
}

void WriteEeprom() {  
EEPROM.write(1, da1);
EEPROM.write(2, da2);
EEPROM.write(3, da3);
EEPROM.write(4, da4);
EEPROM.write(5, da5);

EEPROM.write(6, db1);
EEPROM.write(7, db2);
EEPROM.write(8, db3);
EEPROM.write(9, db4);
EEPROM.write(10, db5);
}