#include <SPI.h>
#include <Wire.h>

#include "U8glib.h"
#include <EEPROM.h>

#define v1 2    
#define v2 3    
#define v3 4    
#define v4 5    
#define v5 6    
#define v6 7    
#define v6a 8   
#define v7  9   

#define v7a 23  
#define v8 25   
#define v9 27   
#define v10 29  

#define pac 31  
#define pcs 33  
#define pro 35  

#define buzz 37     
#define glcd_di 10  
#define glcd_rw 11  
#define glcd_en 12  
#define dled 13     
#define tst A0      
#define inc A1      
#define dec A2      
#define edt A3      
#define runstp A4   
//#define signet A5 
#define srvce A5   

#define ind1 22   
#define ind2 24   
#define ind3 26    
#define ind4 28    
#define ind5 30    
#define ind6 32    
#define ind7 34    
#define ind8 36    
#define ind9 38    
#define ind10 40   
#define ind11 42  

#define ind12 44   
#define ind13 46   
#define ind14 48   
#define ind15 50   
#define bcklight 52 

int x, bck, notif, aural, diredt, dirmin, dirsec, mode, rdetect, rstat, skey, stat, onoff, scnt, swtout, mm, ss, btout, tout, stp, blnk;
int ptime, tproc, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;
float tmpval, s1min, s1sec, s2min, s2sec, s3min, s3sec, s4min, s4sec;
float s5min, s5sec, s6min, s6sec, s7min, s7sec, s8min, s8sec, s9min, s9sec, s10min, s10sec;
int minmax = 65;
int durm, durs, tmp, tparam = 1;
int sflag, eflag, tflag, bflag;
word temp;

String stps, valves, pumps; 
unsigned long prevmillis, prevmillis2, curmillis;

U8GLIB_SSD1306_128X64 GLCD(U8G_I2C_OPT_NONE);	 

void setup(){ 
  pinMode(v1, OUTPUT);
  pinMode(v2, OUTPUT);
  pinMode(v3, OUTPUT);
  pinMode(v4, OUTPUT);
  pinMode(v5, OUTPUT);
  pinMode(v6, OUTPUT);
  pinMode(v6a, OUTPUT);
  pinMode(v7, OUTPUT);
  pinMode(v7a, OUTPUT);
  pinMode(v8, OUTPUT);
  pinMode(v9, OUTPUT);
  pinMode(v10, OUTPUT);
  pinMode(pac, OUTPUT);
  pinMode(pcs, OUTPUT);
  pinMode(pro, OUTPUT);

  pinMode(ind1, OUTPUT);
  pinMode(ind2, OUTPUT);
  pinMode(ind3, OUTPUT);
  pinMode(ind4, OUTPUT);
  pinMode(ind5, OUTPUT);
  pinMode(ind6, OUTPUT);
  pinMode(ind7, OUTPUT);
  pinMode(ind8, OUTPUT);
  pinMode(ind9, OUTPUT);
  pinMode(ind10, OUTPUT);
  pinMode(ind11, OUTPUT);
  pinMode(ind12, OUTPUT);
  pinMode(ind13, OUTPUT);
  pinMode(ind14, OUTPUT);
  pinMode(ind15, OUTPUT);
  pinMode(bcklight, OUTPUT);
    
  pinMode(buzz, OUTPUT);
  pinMode(dled, OUTPUT);
  pinMode(tst, INPUT_PULLUP);
  pinMode(inc, INPUT_PULLUP);
  pinMode(dec, INPUT_PULLUP);
  pinMode(edt, INPUT_PULLUP);
  pinMode(runstp, INPUT_PULLUP);
  //pinMode(signet, INPUT_PULLUP);
  pinMode(srvce, INPUT_PULLUP);

  delay(500);
  digitalWrite(bcklight, HIGH);
  //load_settings(); 
  test_settings(); 
  //notif = 1;
  //rdetect = 0;
  if(notif == 1){
    buzz_init();  
  }
     
  stp = 11;     
  dec_output(); 
  sequence_ind();
  dec_process(); 
  upd_data();  
  if(notif == 1){  
    buzz_rdy();
  }
}

void loop(){     
  curmillis = millis();
  if(curmillis - prevmillis2 >= 1000){
    prevmillis2 = curmillis;  
    btout++;
    if(btout >= 60){
      btout = 60;                
    }  
    check_bck();       
  }
    
  if(eflag == 0 && tflag == 0 && sflag == 0 && onoff == 1){  
    if(curmillis - prevmillis >= 1000){
      prevmillis = curmillis;             
      if(tout == tproc + 1){
        tout = 0;               
        blnk = 0;
        if(rdetect == 0){          
          onoff = 0;
          off_vp();
          off_ind();
          if(notif == 1){   
            buzz_tout();
          }
        }
        else{
          stp++;
          if(stp > 10){
            stp = 11;
            onoff = 0;
            off_vp();
            off_ind();
            if(notif == 1){   
              buzz_tout();
            }  
          }
          else{
            buzz_but2();    
          }
        }                        
      }
      dec_process();            
      ptime = (int)(tproc - tout);     
      mm = (int)(ptime/60);
      ss = (int)(ptime%60);                 
      if(onoff == 1){
        digitalWrite(dled, HIGH);
        delay(20);
        digitalWrite(dled, LOW);
        tout++; 
      }           
    }
  }        

  temp = analogRead(srvce);      
  if(temp < 50){
    btout = 0;
    bflag = 0;
    check_bck();    
    if(onoff == 1 || eflag == 1 || tflag == 1 || sflag != 0){
      if(notif == 1){   
        buzz_inv();
      }
      while(temp < 50 && swtout < 50){
        temp = analogRead(srvce);         
        delay(10);                
        swtout++;                      
      }                       
    }   
    else{
      while(temp < 50){
        temp = analogRead(srvce); 
        delay(10);   
        swtout++;  
        if(swtout > 80){
          if(notif == 1){   
            buzz_lpress();
          }
          //off_vp();
          //off_ind();
          //sflag = 1;
          //stp = 12;
          //onoff = 1;
          delay(1000);                    
          while(temp < 50){
            sflag = 2;
            temp = analogRead(srvce); 
            delay(10);
            blnk++;
            if(blnk > 10){
              blnk = 0;
            }  
            upd_data();                                  
          } 
          off_vp();
          off_ind();          
          stp = 12;
          onoff = 1;          
          sflag = 1;  
          if(notif == 1){   
            buzz_rdy();       
          }
          break;       
        }                    
      }           
    }    
  }                              
  swtout = 0; 
  temp = 1023;

  temp = analogRead(runstp);      
  if(temp < 50){
    btout = 0;
    check_bck();    
    if((eflag == 1 && stp > 10) || tflag == 1){
      if(notif == 1){   
        buzz_inv();
      }
      while(temp < 50 && swtout < 50){
        temp = analogRead(runstp);         
        delay(10);                
        swtout++;                     
      }    
    }  
    else{              
      while(temp < 50){
        temp = analogRead(runstp); 
        delay(10);   
        swtout++;
        if(swtout > 80){
          while(temp < 50){
            temp = analogRead(runstp); 
            delay(10); 
          }            
          break;       
        }                           
      }      
      if(eflag == 1 && stp < 11){
        switch(bflag){
          case 0: //edit
            if(diredt == 0){
              diredt = 1;   
            }
            else{
              diredt = 0;
            }
            break;
          case 1: //min
            if(dirmin == 0){
              dirmin = 1;   
            }
            else{
              dirmin = 0;
            }
            break;
          case 2: //sec
            if(dirsec == 0){
              dirsec = 1;   
            }
            else{
              dirsec = 0;
            }
            break;  
        }         
        if(aural == 1){   
          buzz_but();   
        }
      }
      if(eflag == 0){
        if(aural == 1){   
          buzz_but();       
        }
        else{
          delay(10);
        }        
        if(rdetect == 0){
          onoff = 0; 
          tout = 0; 
          sflag = 0;               
          if(notif == 1){   
            buzz_tout();      
          }               
        }  
        else{           
          if(onoff == 1){
            stp = 11;                     
            onoff = 0; 
            tout = 0; 
            sflag = 0; 
          }
          else{
            stp = 1;                     
            onoff = 1; 
            tout = 0; 
            sflag = 0;
          }                       
        }
      }
    }  
  }
  swtout = 0;
  temp = 1023;

  temp = analogRead(edt);      
  if(temp < 50){
    btout = 0;
    bflag = 0;
    check_bck();    
    if((onoff == 1 && eflag == 0) || tflag == 1 || sflag != 0){
      if(notif == 1){   
        buzz_inv();
      }
      while(temp < 50 && swtout < 50){
        temp = analogRead(edt);         
        delay(10);                
        swtout++;                      
      }                       
    }   
    else{
      while(temp < 50){
        temp = analogRead(edt); 
        delay(10);   
        swtout++;  
        if(swtout > 80){
          if(notif == 1){   
            buzz_lpress();
          }
          off_vp();
          off_ind();
          break;       
        }                    
      }       
      if(swtout > 80){      
        if(eflag == 0 && tflag == 0 && onoff == 0){         
          s1min = (int)(s1/60);
          s1sec = (int)(s1%60);     
          s2min = (int)(s2/60);
          s2sec = (int)(s2%60);     
          s3min = (int)(s3/60);
          s3sec = (int)(s3%60);     
          s4min = (int)(s4/60);
          s4sec = (int)(s4%60);     
          s5min = (int)(s5/60);
          s5sec = (int)(s5%60);     
          s6min = (int)(s6/60);
          s6sec = (int)(s6%60);     
          s7min = (int)(s7/60);
          s7sec = (int)(s7%60);     
          s8min = (int)(s8/60);
          s8sec = (int)(s8%60);     
          s9min = (int)(s9/60);
          s9sec = (int)(s9%60);     
          s10min = (int)(s10/60);
          s10sec = (int)(s10%60);       
          tmpval = s1;        
          stp = 1;  
          eflag = 1;         
          dec_process();                 
          upd_data();   
          delay(1000);       
        }  
        else{    
          eflag = 2;
          s1 = 60*s1min + s1sec; 
          s2 = 60*s2min + s2sec; 
          s3 = 60*s3min + s3sec; 
          s4 = 60*s4min + s4sec;  
          s5 = 60*s5min + s5sec; 
          s6 = 60*s6min + s6sec; 
          s7 = 60*s7min + s7sec;  
          s8 = 60*s8min + s8sec; 
          s9 = 60*s9min + s9sec; 
          s10 = 60*s10min + s10sec;  
          save_settings();         
          blnk = 6;
          upd_data();
          delay(1000);                    
          while(temp < 50){
            eflag = 3;
            temp = analogRead(edt); 
            delay(10);
            blnk++;
            if(blnk > 10){
              blnk = 0;
            }  
            upd_data();                                  
          }           
          eflag = 0; 
          stp = 11; 
          if(notif == 1){   
            buzz_rdy();       
          }     
        }
      }
      else{
        if(eflag == 1){
          if(aural == 1){   
            buzz_but();
          }
          else{
            delay(10);
          }
          while(temp < 50 && swtout < 5){
            temp = analogRead(edt); 
            delay(5);   
            swtout++;         
          }
          if(diredt == 0){
            stp++;
            if(stp > 14){
              stp = 1;
            }
          }
          else{
            stp--;
            if(stp < 1){
              stp = 14;
            }    
          }
          //upd_data();
          dec_process();
        } 
      }
    }    
    if(eflag != 0){
      while(temp < 50){
        eflag = 3;
        temp = analogRead(edt); 
        delay(10);  
        blnk++;
        if(blnk > 10){
          blnk = 0;
        }  
        upd_data();                              
      }
      eflag = 1;
    }                            
  }  
  swtout = 0; 
  temp = 1023;

  temp = analogRead(tst);      
  if(temp < 50){
    btout = 0;
    check_bck();    
    if(onoff == 1 || eflag == 1 || sflag != 0){
      if(notif == 1){   
        buzz_inv();
      }
      while(temp < 50 && swtout < 50){
        temp = analogRead(tst);         
        delay(10);                
        swtout++;                    
      }                       
    }
    else{   
      while(temp < 50){
        temp = analogRead(tst); 
        delay(10);   
        swtout++;  
        if(swtout > 80){
          if(notif == 1){   
            buzz_lpress();
          }
          off_vp();
          off_ind();
          break;       
        }                    
      }       
      if(swtout > 80){
        swtout = 0;
        if(eflag == 0 && tflag == 0 && onoff == 0){
          tflag = 1;
          tparam = 1;  
          upd_data();
        }  
        else{        
          stat = 0;
          off_vp();
          stp = 11;
          blnk = 6;
          upd_data();        
          tflag = 0;
          delay(1000);                    
          while(temp < 50){
            tflag = 2;
            temp = analogRead(tst); 
            delay(10);
            blnk++;
            if(blnk > 10){
              blnk = 0;
            }  
            upd_data();                                  
          }           
          tflag = 0;  
          if(notif == 1){   
            buzz_rdy();       
          }     
        }
      }
      else{
        if(tflag == 1){
          if(aural == 1){   
            buzz_but();
          }
          else{
            delay(10);
          }
          while(temp < 50 && swtout < 5){
            temp = analogRead(tst); 
            delay(5);   
            swtout++;         
          }          
          if(stat == 0){
            stat = 1;
          }
          else{
            stat = 0;
          }
          dec_output();
        }
      }
    }        
    if(tflag != 0){
      while(temp < 50){
        tflag = 2;
        temp = analogRead(tst); 
        delay(10);  
        blnk++;
        if(blnk > 10){
          blnk = 0;
        }  
        upd_data();                              
      }
      tflag = 1;
    }                            
  }  
  swtout = 0; 
  temp = 1023;
  
  temp = analogRead(inc);      
  if(temp < 50){
    btout = 0;
    bflag = 1;     
    check_bck();   
    if(eflag == 0 && tflag == 0){
      if(onoff == 1){
        if(notif == 1){   
          buzz_inv();
        }
      }
      else{
        if(notif == 1){   
          buzz_but();
        }
      }  
      while(temp < 50 && swtout < 50){
        temp = analogRead(inc);         
        delay(10);                
        swtout++;                      
      }
      if(onoff == 0 && sflag == 0 && rdetect == 0){
        stp++;
        if(stp > 10){
          stp = 1;
        }       
        tout = 0;
      }
      else{
        if(notif == 1){   
          buzz_inv();
        }
      }
    }
    if(eflag == 1 && tflag == 0){
      if(aural == 1){   
        buzz_but();
      }
      else{
        delay(10);
      }
      while(temp < 50 && swtout < 5){
        temp = analogRead(inc); 
        delay(5);   
        swtout++;         
      }
      switch(stp){
        case 1:
          if(dirmin == 0){
            s1min++;
            if(s1min > minmax){
              s1min = 0;               
            }
          }
          else{
            s1min--;
            if(s1min < 0){
              s1min = minmax;               
            }                  
          }
          break;  
        case 2:
          if(dirmin == 0){
            s2min++;
            if(s2min > minmax){
              s2min = 0;               
            }
          }
          else{
            s2min--;
            if(s2min < 0){
              s2min = minmax;               
            }                  
          }               
          break;  
        case 3:
          if(dirmin == 0){
            s3min++;
            if(s3min > minmax){
              s3min = 0;               
            }
          }
          else{
            s3min--;
            if(s3min < 0){
              s3min = minmax;               
            }                  
          }                
          break;   
        case 4:
          if(dirmin == 0){
            s4min++;
            if(s4min > minmax){
              s4min = 0;               
            }
          }
          else{
            s4min--;
            if(s4min < 0){
              s4min = minmax;               
            }                  
          }            
          break;   
        case 5:
          if(dirmin == 0){
            s5min++;
            if(s5min > minmax){
              s5min = 0;               
            }
          }
          else{
            s5min--;
            if(s5min < 0){
              s5min = minmax;               
            }                  
          }                
          break;   
        case 6:
          if(dirmin == 0){
            s6min++;
            if(s6min > minmax){
              s6min = 0;               
            }
          }
          else{
            s6min--;
            if(s6min < 0){
              s6min = minmax;               
            }                  
          }                
          break;   
        case 7:
          if(dirmin == 0){
            s7min++;
            if(s7min > minmax){
              s7min = 0;               
            }
          }
          else{
            s7min--;
            if(s7min < 0){
              s7min = minmax;               
            }                  
          }                
          break;   
        case 8:
          if(dirmin == 0){
            s8min++;
            if(s8min > minmax){
              s8min = 0;               
            }
          }
          else{
            s8min--;
            if(s8min < 0){
              s8min = minmax;               
            }                  
          }               
          break;   
        case 9:
          if(dirmin == 0){
            s9min++;
            if(s9min > minmax){
              s9min = 0;               
            }
          }
          else{
            s9min--;
            if(s9min < 0){
              s9min = minmax;               
            }                  
          }                
          break;   
        case 10:
          if(dirmin == 0){
            s10min++;
            if(s10min > minmax){
              s10min = 0;               
            }
          }
          else{
            s10min--;
            if(s10min < 0){
              s10min = minmax;               
            }                  
          }                
          break;        
        case 11:
          if(rdetect == 0){
            rdetect = 1;                
          }
          else{
            rdetect = 0;
          }                
          break;   
        case 12:
          if(notif == 0){
            notif = 1;                
          }
          else{
            notif = 0;
          }                
          break; 
        case 13:
          if(aural == 0){
            aural = 1;                
          }
          else{
            aural = 0;
          }                
          break;  
        case 14:
          bck++;
          if(bck > 2){
            bck = 0;                
          }             
          break;                                      
      }      
    }
    if(eflag == 0 && tflag == 1){
      if(aural == 1){   
        buzz_but();
      }
      else{
        delay(10);
      }
      while(temp < 50 && swtout < 5){
        temp = analogRead(inc); 
        delay(5);   
        swtout++;         
      }
      stat = 0;
      off_vp();
      tparam++;
      if(tparam > 15){
        tparam = 1;      
      }
    }         
  }
  swtout = 0;   
  temp = 1023;

  temp = analogRead(dec);      
  if(temp < 50){  
    btout = 0;
    bflag = 2;    
    check_bck();    
    if(eflag == 0 && tflag == 0){
      if(onoff == 1){
        if(notif == 1){   
          buzz_inv();
        }
      }
      else{
        if(sflag == 0 && rdetect == 0){
          stp--;
          if(stp < 1){
            stp = 10;
          }       
          tout = 0;
          if(notif == 1){   
            buzz_but();
          }
        }
        else{
          if(notif == 1){   
            buzz_inv();
          }  
        }        
      }
      while(temp < 50 && swtout < 50){
        temp = analogRead(dec);         
        delay(10);                
        swtout++;                      
      }            
    }
    if(eflag == 1 && tflag == 0){
      if(aural == 1){   
        buzz_but();
      }
      else{
        delay(10);
      }
      while(temp < 50 && swtout < 5){
        temp = analogRead(dec); 
        delay(5);   
        swtout++;         
      }      
      switch(stp){
        case 1:
          if(dirsec == 0){
            s1sec++;
            if(s1sec > 59){
              s1sec = 0;               
            }
          }
          else{
            s1sec--;
            if(s1sec < 0){
              s1sec = 59;               
            }                  
          }                
          break;  
        case 2:
          if(dirsec == 0){
            s2sec++;
            if(s2sec > 59){
              s2sec = 0;               
            }
          }
          else{
            s2sec--;
            if(s2sec < 0){
              s2sec = 59;               
            }                  
          }                
          break;  
        case 3:
          if(dirsec == 0){
            s3sec++;
            if(s3sec > 59){
              s3sec = 0;               
            }
          }
          else{
            s3sec--;
            if(s3sec < 0){
              s3sec = 59;               
            }                  
          }                
          break;   
        case 4:
          if(dirsec == 0){
            s4sec++;
            if(s4sec > 59){
              s4sec = 0;               
            }
          }
          else{
            s4sec--;
            if(s4sec < 0){
              s4sec = 59;               
            }                  
          }            
          break;   
        case 5:
          if(dirsec == 0){
            s5sec++;
            if(s5sec > 59){
              s5sec = 0;               
            }
          }
          else{
            s5sec--;
            if(s5sec < 0){
              s5sec = 59;               
            }                  
          }                
          break;   
        case 6:
          if(dirsec == 0){
            s6sec++;
            if(s6sec > 59){
              s6sec = 0;               
            }
          }
          else{
            s6sec--;
            if(s6sec < 0){
              s6sec = 59;               
            }                  
          }                
          break;   
        case 7:
          if(dirsec == 0){
            s7sec++;
            if(s7sec > 59){
              s7sec = 0;               
            }
          }
          else{
            s7sec--;
            if(s7sec < 0){
              s7sec = 59;               
            }                  
          }                
          break;   
        case 8:
          if(dirsec == 0){
            s8sec++;
            if(s8sec > 59){
              s8sec = 0;               
            }
          }
          else{
            s8sec--;
            if(s8sec < 0){
              s8sec = 59;               
            }                  
          }               
          break;   
        case 9:
          if(dirsec == 0){
            s9sec++;
            if(s9sec > 59){
              s9sec = 0;               
            }
          }
          else{
            s9sec--;
            if(s9sec < 0){
              s9sec = 59;               
            }                  
          }                
          break;   
        case 10:
          if(dirsec == 0){
            s10sec++;
            if(s10sec > 59){
              s10sec = 0;               
            }
          }
          else{
            s10sec--;
            if(s10sec < 0){
              s10sec = 59;               
            }                  
          }                
          break;        
        case 11:
          if(rdetect == 0){
            rdetect = 1;                
          }
          else{
            rdetect = 0;
          }                
          break;  
        case 12:
          if(notif == 0){
            notif = 1;                
          }
          else{
            notif = 0;
          }                
          break;   
        case 13:
          if(aural == 0){
            aural = 1;                
          }
          else{
            aural = 0;
          }                
          break; 
        case 14:
          bck--;
          if(bck < 0){
            bck = 2;                
          }                          
          break;                                    
      }      
    }
    
    if(eflag == 0 && tflag == 1){
      if(aural == 1){   
        buzz_but();
      }
      else{
        delay(10);
      }
      while(temp < 50 && swtout < 5){
        temp = analogRead(dec); 
        delay(5);   
        swtout++;         
      }
      stat = 0;
      off_vp();
      tparam--;
      if(tparam < 1){
        tparam = 15;      
      }
    }     
  }
  swtout = 0;   
  temp = 1023;
  
  switch(stp){
    case 1:          
      tmpval = 60*s1min + s1sec;
      break;  
    case 2:             
      tmpval = 60*s2min + s2sec;
      break;  
    case 3:                 
      tmpval = 60*s3min + s3sec;
      break;   
    case 4:          
      tmpval = 60*s4min + s4sec;
      break;   
    case 5:         
      tmpval = 60*s5min + s5sec;
      break;   
    case 6:           
      tmpval = 60*s6min + s6sec;
      break;   
    case 7:          
      tmpval = 60*s7min + s7sec;
      break;   
    case 8:         
      tmpval = 60*s8min + s8sec;
      break;   
    case 9:          
      tmpval = 60*s9min + s9sec;
      break;   
    case 10:          
      tmpval = 60*s10min + s10sec;
      break;                       
  }
  
  blnk++;
  if(blnk > 10){
    blnk = 0;
  }   

  upd_data();  
  dec_process();        
}

void upd_data(){
  GLCD.firstPage();  
  do{  
    GLCD.drawFrame(0,0,128,11); 
    GLCD.drawFrame(0,10,128,41); 
    GLCD.drawFrame(0,52,128,12);       
    GLCD.setFont(u8g_font_timB08);       
    GLCD.setPrintPos(14, 9);
    GLCD.print(F("MIXED BED REGEN"));       
    
    if(tflag != 0){      
      GLCD.setFont(u8g_font_timB08); 
      GLCD.setPrintPos(34, 21); 
      GLCD.print(F("TEST MODE"));

      GLCD.setPrintPos(10, 34);
      if(tparam <= 12){                
        GLCD.print(F("VALVE              STATUS"));
        GLCD.setFont(u8g_font_profont11);    
        GLCD.setPrintPos(25, 46);
        switch(tparam){
          case 1:
            GLCD.print(F("1 "));
            break;
          case 2:
            GLCD.print(F("2 "));
            break;
          case 3:
            GLCD.print(F("3 "));
            break;
          case 4:
            GLCD.print(F("4 "));
            break;
          case 5:
            GLCD.print(F("5 "));
            break;
          case 6:
            GLCD.print(F("6 "));
            break;
          case 7:
            GLCD.print(F("6A"));
            break;
          case 8:
            GLCD.print(F("7 "));
            break;
          case 9:
            GLCD.print(F("7A"));
            break;
          case 10:
            GLCD.print(F("8 "));
            break;
          case 11:
            GLCD.print(F("9 "));
            break;
          case 12:
            GLCD.print(F("10"));
            break;                      
        }        
      }
      else{        
        GLCD.print(F("PUMP                   STATUS"));
        GLCD.setPrintPos(10, 46);
        GLCD.setFont(u8g_font_profont11);    
        switch(tparam){
          case 13:
            GLCD.print(F("ACID "));
            break;
          case 14:
            GLCD.print(F("CAUSTIC"));
            break;
          case 15:
            GLCD.print(F(" RO "));
            break;
        }        
      }
      
      if(stat == 0){
        if(tparam <= 12){
          GLCD.setPrintPos(80, 45);  
          GLCD.print(F("CLOSE"));
        }
        else{
          GLCD.setPrintPos(88, 45);  
          GLCD.print(F("OFF"));  
        }
      }
      else{
        if(tparam <= 12){
          GLCD.setPrintPos(80, 45);  
          GLCD.print(F("OPEN"));
        }
        else{
          GLCD.setPrintPos(88, 45);  
          GLCD.print(F("ON"));  
        }
      }             
    }

    if(eflag != 0){
      GLCD.setFont(u8g_font_timB08);       
      switch(stp){
        case 1:
          GLCD.setPrintPos(34, 21);          
          break;
        case 2:
          GLCD.setPrintPos(45, 21);
          break;
        case 3:
          GLCD.setPrintPos(16, 21);
          break;
        case 4:
          GLCD.setPrintPos(31, 21);
          break;
        case 5:
          GLCD.setPrintPos(33, 21);
          break;
        case 6:
          GLCD.setPrintPos(22, 21);
          break;
        case 7:
          GLCD.setPrintPos(40, 21);
          break;
        case 8:
          GLCD.setPrintPos(25, 21);
          break;
        case 9:
          GLCD.setPrintPos(43, 21);
          break;
        case 10:
          GLCD.setPrintPos(29, 21);
          break;       
        case 11:
          GLCD.setPrintPos(5, 21); 
          GLCD.print(F("REGENERATION MODE"));    
          break;
        case 12:
          GLCD.setPrintPos(6, 21); 
          GLCD.print(F("AURAL NOTIFICATION"));    
          break;
        case 13:
          GLCD.setPrintPos(33, 21); 
          GLCD.print(F("KEY SOUND"));    
          break; 
        case 14:
          GLCD.setPrintPos(33, 21);           
          GLCD.print(F("BACKLIGHT"));    
          break;                                   
      }
      if(stp < 11){
        GLCD.print(stps);
      }

      GLCD.setFont(u8g_font_profont17);         
      if(stp < 11){       
        GLCD.setPrintPos(18, 40);
        if(durm < 10){
          GLCD.print('0');   
        }
        GLCD.print(durm); 
        GLCD.print(F("m ")); 
        if(durs < 10){
          GLCD.print('0');   
        }
        GLCD.print(durs); 
        GLCD.print('s');   
      }      
      else{        
        if(stp == 11){
          GLCD.setPrintPos(25, 40);
          if(rdetect == 1){
            GLCD.print(F(" AUTO "));
          }
          else{
            GLCD.print(F("MANUAL"));
          }
        }        
        if(stp == 12){          
          if(notif == 1){
            GLCD.setPrintPos(18, 40);
            GLCD.print(F("ENABLED"));            
          }
          else{            
            GLCD.setPrintPos(11, 40);
            GLCD.print(F("DISABLED"));
          }        
        }
        if(stp == 13){          
          if(aural == 1){
            GLCD.setPrintPos(18, 40);
            GLCD.print(F("ENABLED"));            
          }
          else{            
            GLCD.setPrintPos(11, 40);
            GLCD.print(F("DISABLED"));
          }        
        }
        if(stp == 14){          
          GLCD.setPrintPos(40, 40);
          switch(bck){
            case 0:
              GLCD.setPrintPos(45, 40);
              GLCD.print(F("OFF"));              
              //digitalWrite(bcklight, LOW);              
              break;
            case 1:
              GLCD.print(F(" ON"));              
              //digitalWrite(bcklight, HIGH);
              break;
            case 2:
              GLCD.print(F("AUTO"));             
              //digitalWrite(bcklight, HIGH);
              break;  
          }
          check_bck();
        }      
      }
    }
    
    if(eflag == 0 && tflag == 0){
      GLCD.setFont(u8g_font_profont10);     
      GLCD.setPrintPos(4, 19);  
      GLCD.print(F("SEQUENCE:"));
      GLCD.print(stps);          

      GLCD.setPrintPos(4, 28);  
      GLCD.print(F("TIME SET:"));
      if(stp < 11){       
        if(durm < 10){
          GLCD.print('0');   
        }
        GLCD.print(durm); 
        GLCD.print(F("m ")); 
        if(durs < 10){
          GLCD.print('0');   
        }
        GLCD.print(durs); 
        GLCD.print('s');   
      }
      else{
        GLCD.print(F("NONE   "));         
      }

      GLCD.setPrintPos(4, 38);  
      GLCD.print(F("VALVE(S):"));
      GLCD.print(valves);

      GLCD.setPrintPos(4, 48); 
      GLCD.print(F("PUMP(S) :")); 
      GLCD.print(pumps);

      GLCD.setPrintPos(4, 61);  
      if(stp < 11 && onoff == 1){
        GLCD.print(F("TIME LEFT:"));       
        if(mm < 10){
          GLCD.print('0');   
        }
        GLCD.print(mm); 
        GLCD.print(F("m ")); 
        if(ss < 10){
          GLCD.print('0');   
        }
        GLCD.print(ss); 
        GLCD.print('s');      
      }
      else{
        if(blnk > 5){                     
          if(sflag == 0){
            GLCD.print(F(" PRESS RUN/STP TO START "));            
          }
          if(sflag == 1){
            GLCD.print(F("  MIXED BED ON SERVICE  "));       
          }                 
        }
        else{
          GLCD.print(F("                        "));             
        }
      }
    }
       
    GLCD.setFont(u8g_font_profont10);     
    GLCD.setPrintPos(4, 61);  
    if(eflag != 0 || tflag != 0 || sflag != 0){
      if(blnk > 5){      
        if(eflag != 0 && tflag == 0){
          switch(eflag){
            case 1:
              if(stp < 11){
                GLCD.print(F(" MODIFY STEPS DURATION  "));                                                           
              }
              else{
                GLCD.print(F(" MODIFY SYSTEM SETTINGS "));                 
              }
              break;
            case 2:
              GLCD.print(F("  SAVING! PLEASE WAIT   "));                  
              break;
            case 3: 
              GLCD.print(F(" PLEASE RELEASE BUTTON  "));                  
              break;   
          }
        }
        if(eflag == 0 && tflag != 0){
          switch(tflag){
            case 1:
              GLCD.print(F(" TEST VALVES AND PUMPS  "));                                                
              break;
            case 2: 
              GLCD.print(F(" PLEASE RELEASE BUTTON  "));                   
              break;   
          }                      
        }
        if(eflag == 0 && tflag == 0 && sflag != 0){
          switch(sflag){
            case 2:  
              GLCD.print(F(" PLEASE RELEASE BUTTON  "));                
              break;
          }
        }
      }    
      else{
        GLCD.print(F("                        "));             
      }
    }           
  }while(GLCD.nextPage());   
}

void dec_process(){  
  switch(stp){
    case 1:      
      backwash();    
      tproc = s1;      
      break;
    case 2:
      settle();  
      tproc = s2;        
      break;
    case 3:    
      chemical_injection();  
      tproc = s3;                              
      break;
    case 4:
      slow_rinse();     
      tproc = s4;
      break;
    case 5:
      fast_rinse();      
      tproc = s5;
      break;
    case 6:
      partial_drain();     
      tproc = s6;
      break;
    case 7:   
      air_mix(); 
      tproc = s7;      
      break;
    case 8:
      air_mix_drain();    
      tproc = s8;
      break;  
    case 9:
      refill();     
      tproc = s9;
      break;
    case 10:
      final_rinse();  
      tproc = s10;    
      break;
    case 11:
      stand_by();     
      tproc = 0; 
      break;
    case 12:
      service();     
      tproc = 0; 
      break;      
  }   
  if(eflag == 0 && tflag == 0 && onoff == 1){
    dec_output();
    sequence_ind();
  } 
}

void sequence_ind(){
  off_ind();
  switch(stp){
    case 1:
      digitalWrite(ind1, HIGH);      
      break;
    case 2:
      digitalWrite(ind2, HIGH);
      break;      
    case 3:
      digitalWrite(ind3, HIGH);
      break;    
    case 4:
      digitalWrite(ind4, HIGH);
      break;    
    case 5:
      digitalWrite(ind5, HIGH);
      break; 
    case 6:
      digitalWrite(ind6, HIGH);
      break;  
    case 7:
      digitalWrite(ind7, HIGH);
      break;         
    case 8:
      digitalWrite(ind8, HIGH);
      break;    
    case 9:
      digitalWrite(ind9, HIGH);
      break;    
    case 10:
      digitalWrite(ind10, HIGH);
      break;
    case 11:
      if(blnk > 5){
        if(tflag == 0 && eflag == 0){
          digitalWrite(ind11, HIGH);
        }  
        else{
          digitalWrite(ind11, LOW);
        }        
      }
      else{
        digitalWrite(ind11, LOW);  
      }
      break;      
  }
}

void dec_output(){
  if(eflag == 0 && tflag == 0){
    off_vp();
    switch(stp){
      case 1: //Backwash        
        digitalWrite(v3, HIGH);
        digitalWrite(v4, HIGH);
        digitalWrite(pro, HIGH);
        break;  
      case 2: //Settle 
             
        break;  
      case 3: //Chemical Injection
        digitalWrite(v6, HIGH);
        digitalWrite(v6a, HIGH);
        digitalWrite(v7, HIGH);
        digitalWrite(v7a, HIGH);
        digitalWrite(v9, HIGH);
        digitalWrite(pac, HIGH);
        digitalWrite(pcs, HIGH);
        digitalWrite(pro, HIGH);
        break;         
      case 4: //Slow Rinse
        digitalWrite(v6, HIGH);
        digitalWrite(v7, HIGH);
        digitalWrite(v9, HIGH);
        digitalWrite(pro, HIGH);
        break;  
      case 5: //Fast Rinse
        digitalWrite(v1, HIGH);
        digitalWrite(v3, HIGH);
        digitalWrite(v9, HIGH);
        digitalWrite(pro, HIGH);
        break;  
      case 6: //Partial Drain
        digitalWrite(v5, HIGH);
        digitalWrite(v10, HIGH);
        digitalWrite(pro, HIGH);
        break;  
      case 7: //Air Mix
        digitalWrite(v8, HIGH);
        digitalWrite(v10, HIGH);
        break;  
      case 8: //Air Mix Drain
        digitalWrite(v8, HIGH);
        digitalWrite(v9, HIGH);
        digitalWrite(v10, HIGH);
        break;              
      case 9: //Refill
        digitalWrite(v1, HIGH);
        digitalWrite(v10, HIGH);
        digitalWrite(pro, HIGH);
        break;  
      case 10: //Final Rinse
        digitalWrite(v1, HIGH);
        digitalWrite(v5, HIGH);
        digitalWrite(pro, HIGH);
        break;  
      case 11: //Stand by
      
        break;  
      case 12: //Service
        digitalWrite(v1, HIGH);
        digitalWrite(v2, HIGH);
        digitalWrite(pro, HIGH);
        break;           
    }
  }
  else if(eflag == 0 && tflag == 1){
    off_vp();
    if(stat == 1){
      switch(tparam){
        case 1: //Valve 1 ON
          digitalWrite(v1, HIGH);        
          break;  
        case 2: //Valve 2 ON
          digitalWrite(v2, HIGH);
          break;  
        case 3: //Valve 3 ON
          digitalWrite(v3, HIGH);
          break;         
        case 4: //Valve 4 ON
          digitalWrite(v4, HIGH);
          break;  
        case 5: //Valve 5 ON
          digitalWrite(v5, HIGH);
          break;  
        case 6: //Valve 6 ON
          digitalWrite(v6, HIGH);
          break;  
        case 7: //Valve 6A ON
          digitalWrite(v6a, HIGH);
          break;  
        case 8: //Valve 7 ON
          digitalWrite(v7, HIGH);
          break;              
        case 9: //Valve 7a ON
          digitalWrite(v7a, HIGH);
          break;  
        case 10: //Valve 8 ON
          digitalWrite(v8, HIGH);
          break;  
        case 11: //Valve 9 ON
          digitalWrite(v9, HIGH);
          break;  
        case 12: //Valve 10 ON
          digitalWrite(v10, HIGH);
          break;
        case 13: //Acid Pump ON
          digitalWrite(pac, HIGH);
          break;
        case 14: //Caustic Pump ON
          digitalWrite(pcs, HIGH);
          break;          
        case 15: //RO Transfer Pump ON
          digitalWrite(pro, HIGH);
          break;       
      } 
    }     
  }
}

void off_vp(){
  digitalWrite(v1, LOW);
  digitalWrite(v2, LOW);
  digitalWrite(v3, LOW);
  digitalWrite(v4, LOW);
  digitalWrite(v5, LOW);
  digitalWrite(v6, LOW);
  digitalWrite(v6a, LOW);
  digitalWrite(v7, LOW);
  digitalWrite(v7a, LOW);
  digitalWrite(v8, LOW);
  digitalWrite(v9, LOW);
  digitalWrite(v10, LOW);
  digitalWrite(pac, LOW);
  digitalWrite(pcs, LOW);
  digitalWrite(pro, LOW);  
}

void off_ind(){ 
  digitalWrite(ind1, LOW);
  digitalWrite(ind2, LOW);
  digitalWrite(ind3, LOW);
  digitalWrite(ind4, LOW);
  digitalWrite(ind5, LOW);
  digitalWrite(ind6, LOW);
  digitalWrite(ind7, LOW);
  digitalWrite(ind8, LOW);
  digitalWrite(ind9, LOW);
  digitalWrite(ind10, LOW); 
  digitalWrite(ind11, LOW);
  if(tflag != 0 || eflag != 0){    
    digitalWrite(ind14, HIGH);    
  }
  else{
    digitalWrite(ind14, LOW); 
  }
      
  if(rstat == 0){ 
    digitalWrite(ind12, LOW);   
    if(stp == 12){
      digitalWrite(ind13, HIGH);
    } 
    else{
      if(blnk > 5){
        digitalWrite(ind13, HIGH);       
      }
      else{
        digitalWrite(ind13, LOW);      
      }
    }
  }
  else{
    digitalWrite(ind12, HIGH);
    digitalWrite(ind13, LOW);        
  }   
  if(rdetect == 1){    
    digitalWrite(ind15, HIGH);   
  }
  else{  
    digitalWrite(ind15, LOW);
  }      
}

void check_bck(){
  if(bck == 0){           
    digitalWrite(bcklight, LOW);             
  }
  else if(bck == 1){
    digitalWrite(bcklight, HIGH);   
  }
  else{
    if(bck == 2){
      if(btout == 60){      
        digitalWrite(bcklight, LOW);      
      }
      else{      
        digitalWrite(bcklight, HIGH);      
      }
    }  
  }
}
    
void backwash(){
  stps = (F("BACKWASH"));        
  if(eflag == 0){
    tmp = s1;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();  
  valves = (F("3,4"));     
  pumps = (F("RO"));        
}

void settle(){
  stps = (F("SETTLE"));     
  if(eflag == 0){
    tmp = s2;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();           
  valves = (F("NONE"));     
  pumps = (F("NONE"));     
}

void chemical_injection(){                     
  stps = (F("CHEM INJECTION"));     
  if(eflag == 0){
    tmp = s3;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();  
  valves = (F("6,6A,7,7A,9"));     
  pumps = (F("ACID,CAUSTIC,RO"));    
}

void slow_rinse(){
  stps = (F("SLOW RINSE"));       
  if(eflag == 0){
    tmp = s4;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();           
  valves = (F("6,7,9"));     
  pumps = (F("RO"));    
}

void fast_rinse(){
  stps = (F("FAST RINSE"));      
  if(eflag == 0){
    tmp = s5;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();        
  valves = (F("1,3,9"));     
  pumps = (F("RO"));      
}

void partial_drain(){
  stps = (F("PARTIAL DRAIN"));       
  if(eflag == 0){
    tmp = s6;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();   
  valves = (F("5,10"));     
  pumps = (F("RO"));     
}

void air_mix(){
  stps = (F("AIR MIX"));       
  if(eflag == 0){
    tmp = s7;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();  
  valves = (F("8,10"));     
  pumps = (F("NONE"));    
}

void air_mix_drain(){
  stps = (F("AIR MIX DRAIN"));        
  if(eflag == 0){
    tmp = s8;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();  
  valves = (F("8,9,10"));     
  pumps = (F("NONE"));    
}

void refill(){
  stps = (F("REFILL"));      
  if(eflag == 0){
    tmp = s9;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();      
  valves = (F("1,10"));     
  pumps = (F("RO"));     
}

void final_rinse(){
  stps = (F("FINAL RINSE"));      
  if(eflag == 0){
    tmp = s10;
  }
  else{
    tmp = tmpval;   
  }
  dur_conv();      
  valves = (F("1,5"));     
  pumps = (F("RO"));    
}

void stand_by(){
  stps = (F("STAND BY"));         
  tmp = 0;
  dur_conv();  
  valves = (F("NONE"));     
  pumps = (F("NONE"));    
}

void service(){
  stps = (F("SERVICE"));      
  tmp = 0;
  dur_conv();         
  valves = (F("1,2"));     
  pumps = (F("RO"));     
}

void buzz_but(){
  //digitalWrite(buzz,HIGH);
  //delay(10);
  //digitalWrite(buzz,LOW);
  tone(buzz, 1000, 5);
}

void buzz_but2(){
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  tone(buzz, 1000, 25);
}

void buzz_rdy(){
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  //delay(50);
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  //delay(50);
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  tone(buzz, 1000, 25);
  delay(50);
  tone(buzz, 1000, 25);
  delay(50);
  tone(buzz, 1000, 25);  
}

void buzz_tout(){
  //digitalWrite(buzz,HIGH);
  //delay(300);
  //digitalWrite(buzz,LOW);
  //delay(25);
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  //delay(25);
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  tone(buzz, 1000, 75);
  delay(50);
  tone(buzz, 1000, 25);
  delay(50);
  tone(buzz, 1000, 25);
}

void buzz_lpress(){
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  //delay(100);
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);  
  tone(buzz, 1000, 25);  
  delay(100);
  tone(buzz, 1000, 25);    
}

void buzz_init(){
  //digitalWrite(buzz,HIGH);  
  //delay(300);
  //digitalWrite(buzz,LOW);
  tone(buzz, 1000, 75);
}

void buzz_inv(){
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  //delay(50);
  //digitalWrite(buzz,HIGH);
  //delay(100);
  //digitalWrite(buzz,LOW);
  tone(buzz, 1000, 25);
  delay(50);
  tone(buzz, 1000, 25);
}  

void dur_conv(){
  durm = (int)(tmp/60);
  durs = (int)(tmp%60);  
}

void load_settings(){
  s1 = EEPROMReadlong(1);  
  if(s1 < 0 || s1 > 60*minmax){
    s1 = 900;
    EEPROMWritelong(1, s1);    
  }      
  s2 = EEPROMReadlong(5);  
  if(s2 < 0 || s2 > 60*minmax){
    s2 = 300;
    EEPROMWritelong(5, s2);    
  }    
  s3 = EEPROMReadlong(9);  
  if(s3 < 0 || s3 > 60*minmax){
    s3 = 1800;
    EEPROMWritelong(9, s3);    
  }        
  s4 = EEPROMReadlong(13);  
  if(s4 < 0 || s4 > 60*minmax){
    s4 = 1800;
    EEPROMWritelong(13, s4);    
  }      
  s5 = EEPROMReadlong(17);  
  if(s5 < 0 || s5 > 60*minmax){
    s5 = 1800;
    EEPROMWritelong(17, s5);    
  }     
  s6 = EEPROMReadlong(21);  
  if(s6 < 0 || s6 > 60*minmax){
    s6 = 150;
    EEPROMWritelong(21, s6);    
  }         
  s7 = EEPROMReadlong(25);  
  if(s7 < 0 || s7 > 60*minmax){
    s7 = 1200;
    EEPROMWritelong(25, s7);    
  }     
  s8 = EEPROMReadlong(29);  
  if(s8 < 0 || s8 > 60*minmax){
    s8 = 300;
    EEPROMWritelong(29, s8);    
  }        
  s9 = EEPROMReadlong(33);  
  if(s9 < 0 || s9 > 60*minmax){
    s9 = 300;
    EEPROMWritelong(33, s9);    
  }         
  s10 = EEPROMReadlong(37);  
  if(s10 < 0 || s10 > 60*minmax){
    s10 = 1800; //Temp
    EEPROMWritelong(37, s10);    
  }       
  rdetect = EEPROM.read(42);     
  if(rdetect < 0 || rdetect > 1){
    rdetect = 0;
    EEPROM.update(42, rdetect);    
  }     
  notif = EEPROM.read(43);     
  if(notif < 0 || notif > 1){
    notif = 0;
    EEPROM.update(43, notif);    
  }  
  aural = EEPROM.read(44);     
  if(aural < 0 || aural > 1){
    aural = 0;
    EEPROM.update(44, aural);    
  } 
  bck = EEPROM.read(46);     
  if(bck < 0 || bck > 1){
    bck = 2;
    EEPROM.update(46, bck);   
    digitalWrite(bcklight, HIGH); 
  }  
}

void test_settings(){
  s1 = 10;     
  s2 = 10; 
  s3 = 10;    
  s4 = 10;        
  s5 = 10;      
  s6 = 10;        
  s7 = 10;       
  s8 = 10;           
  s9 = 10;       
  s10 = 10;        
  rdetect = 1;     
  notif = 1;       
  aural = 1;    
  bck = 2;
  rstat = 1;
}

void save_settings(){     
  EEPROMWritelong(1, s1);       
  EEPROMWritelong(5, s2);         
  EEPROMWritelong(9, s3);          
  EEPROMWritelong(13, s4);           
  EEPROMWritelong(17, s5);              
  EEPROMWritelong(21, s6);               
  EEPROMWritelong(25, s7);                
  EEPROMWritelong(29, s8);                
  EEPROMWritelong(33, s9);                 
  EEPROMWritelong(37, s10);  
  EEPROM.update(42, rdetect);   
  EEPROM.update(43, notif);
  EEPROM.update(44, aural);    
  EEPROM.update(46, bck);  
}

void EEPROMWritelong(int address, long value){  
  byte four = (value & 0xFF);
  byte three = ((value >> 8) & 0xFF);
  byte two = ((value >> 16) & 0xFF);
  byte one = ((value >> 24) & 0xFF);
  EEPROM.update(address, four);
  EEPROM.update(address + 1, three);
  EEPROM.update(address + 2, two);
  EEPROM.update(address + 3, one);
}

long EEPROMReadlong(long address){      
  long four = EEPROM.read(address);
  long three = EEPROM.read(address + 1);
  long two = EEPROM.read(address + 2);
  long one = EEPROM.read(address + 3);  
  return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}