#include <LiquidCrystal.h>
LiquidCrystal lcd(10, 9, 8,7,6,5);
#include <EEPROM.h>
unsigned long batch,mul,count,s_count;
int m,a,b,c,c1,c2,c3,c4,c5,c6,c7,c8,d,relay,relay1,r,r1,r2,r3,pointer;
uint8_t temp,temp1;
bool state,state1,s_state,sensor,rly,contact;
void setup() { // put your setup code here, to run once
pinMode(11,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(2,OUTPUT);
pinMode(12,OUTPUT);
mul=0;
lcd.begin(20,4);
lcd.setCursor(0,0);
lcd.print("BATCH: ");
lcd.setCursor(0,1);
lcd.print(" M.L. : ");
lcd.setCursor(0,2);
lcd.print("---------------------");
lcd.setCursor(0,3);
lcd.print("#DESIGN: NURUL HAQUE ");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
temp=EEPROM.read(0);
if(temp>99)temp=0;
r1=temp%10;
r2=temp/10;
temp=EEPROM.read(1);
if(temp>99)temp=0;
r3=temp%10;
relay= r1+(r2*10)+(r3*100);
//--batch
temp=EEPROM.read(2);
if(temp>99)temp=0;
batch=temp;
temp=EEPROM.read(3);
if(temp>99)temp=0;
batch=batch+(temp*100);
temp=EEPROM.read(4);
if(temp>99)temp=0;
batch=batch+(temp*10000);
temp=EEPROM.read(6);
if(temp>99)temp=0;
batch=batch+(temp*1000000);
//--count
temp=EEPROM.read(7);
if(temp>99)temp=0;
c1=temp%10;
c2=temp/10;
temp=EEPROM.read(8);
if(temp>99)temp=0;
c3=temp%10;
c4=temp/10;
temp=EEPROM.read(9);
if(temp>99)temp=0;
c5=temp%10;
c6=temp/10;
temp=EEPROM.read(10);
if(temp>99)temp=0;
c7=temp%10;
c8=temp/10;
temp=EEPROM.read(11);
if(temp>1)temp=0;
contact=temp;
}
void loop() {
if(!digitalRead(11)&!s_state&m==0&!rly){
s_state=1;
count++;
if(count>99999999)count=99999999;
if(count>(s_count-1)){
batch++;
count=0;
rly=1;
temp=batch%10;
temp1=(batch/10)%10;
temp=(temp+temp1*10);
EEPROM.update(2, temp);
temp=(batch/100)%10;
temp1=(batch/1000)%10;
temp=(temp+temp1*10);
EEPROM.update(3, temp);
temp=(batch/10000)%10;
temp1=(batch/100000)%10;
temp=(temp+temp1*10);
EEPROM.update(4, temp);
temp=(batch/1000000)%10;
temp1=(batch/10000000)%10;
temp=(temp+temp1*10);
EEPROM.update(6, temp);
}
}
if(!digitalRead(3)&digitalRead(4)){
if(m==0)a++;
if(m==1&!state){
state=1;
c=c+1;
if(c>9)c=0;
switch(mul){
case 1:
c1=c;
break;
case 2:
c2=c;
break;
case 3:
c3=c;
break;
case 4:
c4=c;
break;
case 5:
c5=c;
break;
case 6:
c6=c;
break;
case 7:
c7=c;
break;
case 8:
c8=c;
break;
}
}
if(m==2&!state){
state=1;
r=r+1;
if(r>9)r=0;
switch(mul){
case 1:
r1=r;
break;
case 2:
r2=r;
break;
case 3:
r3=r;
break;
}
}}
s_count=c1+(c2*10)+(c3*100)+(c4*1000)+(c5*10000)+(c6*100000)+(c7*1000000)+(c8*10000000);
relay= r1+(r2*10)+(r3*100);
if(!digitalRead(4)&digitalRead(3))b++;
if(!digitalRead(4)&!digitalRead(3))d++;
if(digitalRead(11))s_state=0;
if(digitalRead(4)&digitalRead(3)){
if(m==0){a=0;
b=0;
d=0;}
if(m>0&!state){
state=1;
if(b>0&b<30){
c=0;
r=0;
mul=mul+1;
if(m==3&mul>1)contact=!contact;
if(m==2&mul>3)mul=1;
if(mul>8)mul=1;
b=0;
switch(mul){
case 1:
c=c1;
r=r1;
break;
case 2:
c=c2;
r=r2;
break;
case 3:
c=c3;
r=r3;
break;
case 4:
c=c4;
break;
case 5:
c5=c;
break;
case 6:
c=c6;
break;
case 7:
c=c7;
break;
case 8:
c=c8;
break;}
}
}
state=0;
state1=0;
}
if(a>100){
count=0;
a=0;
}
if(b>100&!state1){
m++;
if(m>3){
m=0;
temp=relay%10;
temp1=(relay/10)%10;
temp=(temp+temp1*10);
EEPROM.update(0, temp);
temp=(relay/100);
EEPROM.update(1, temp);
//-----count set
temp=s_count%10;
temp1=(s_count/10)%10;
temp=(temp+temp1*10);
EEPROM.update(7, temp);
temp=(s_count/100)%10;
temp1=(s_count/1000)%10;
temp=(temp+temp1*10);
EEPROM.update(8, temp);
temp=(s_count/10000)%10;
temp1=(s_count/100000)%10;
temp=(temp+temp1*10);
EEPROM.update(9, temp);
temp=(s_count/1000000)%10;
temp1=(s_count/10000000)%10;
temp=(temp+temp1*10);
EEPROM.update(10, temp);
EEPROM.update(11, contact);
}
lcd.clear();
mul=0;
b=0;
state=1;
state1=1;
}
if(d>100){
EEPROM.update(2, 0);
EEPROM.update(3, 0);
EEPROM.update(4, 0);
EEPROM.update(6, 0);
d=0;
batch=0;
}
delay(10);
if(m==0){
lcd.setCursor(0,0);
lcd.print("BATCH: ");
lcd.print(batch);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("M.L. : ");
lcd.print(count);
lcd.print(" ");
if(rly){
relay1++;
if(relay>0){digitalWrite(2,!contact);
digitalWrite(12,!contact);}
if((relay*47)<=relay1){
relay1=0;
rly=0;
}
} else {digitalWrite(2,contact);
digitalWrite(12,contact); }
}
if(m==1){
pointer++;
lcd.setCursor(0,0);
lcd.print(" SETTINGS ");
lcd.setCursor(0,1);
lcd.print("M.L. :");
if(pointer>20&mul==8)lcd.print(" ");
else lcd.print(c8);
if(pointer>20&mul==7)lcd.print(" ");
else lcd.print(c7);
if(pointer>20&mul==6)lcd.print(" ");
else lcd.print(c6);
if(pointer>20&mul==5)lcd.print(" ");
else lcd.print(c5);
if(pointer>20&mul==4)lcd.print(" ");
else lcd.print(c4);
if(pointer>20&mul==3)lcd.print(" ");
else lcd.print(c3);
if(pointer>20&mul==2)lcd.print(" ");
else lcd.print(c2);
if(pointer>20&mul==1)lcd.print(" ");
else lcd.print(c1);
}
if(m==2){
pointer++;
lcd.setCursor(0,0);
lcd.print(" SETTINGS ");
lcd.setCursor(0,1);
lcd.print("RELAY TIME:");
if(pointer>20&mul==3)lcd.print(" ");
else lcd.print(r3);
if(pointer>20&mul==2)lcd.print(" ");
else lcd.print(r2);
if(pointer>20&mul==1)lcd.print(" ");
else lcd.print(r1);
}
if(m==3){
lcd.setCursor(0,0);
lcd.print(" SETTINGS ");
lcd.setCursor(0,1);
lcd.print("CONTACT TYPE:");
if(!contact)lcd.print("N.O");
else lcd.print("N.C");
}
if(pointer>40|m==0)pointer=0;
}