munther_abdellatif

PID test2

Jul 4th, 2020
118
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // function
  2. void getKey();
  3. void shift_serial_test();
  4. void RTC_LCD__test();
  5. void valvecontrol();
  6. void setclock();
  7. void lcddisplay();
  8. void load();
  9. void save();
  10. void valvereset();
  11. void progreset();
  12. void checkprog();
  13. void runprogfun();
  14. void shiftout();
  15. void saverunprogdata();
  16. void loadrunprogdata();
  17. void check_off_timer();
  18. void fastcheck();
  19. void stopprog();
  20. void continueprog();
  21. void saveday();
  22. void loadday();
  23. void stopall();
  24. void setvalvetime();
  25. void readph();
  26. void checkfilter();
  27. void runfilter();
  28.  
  29. // PID controller
  30. float error =0;
  31. float errorsum=0;
  32. float lasterror=0;
  33. byte PIDtimer=0;
  34. byte PIDontime=0;
  35. unsigned long lastmillisPH=0;
  36. float PHp=0.5,PHi=0.5,PHd=0;
  37.  
  38. // PH calibration
  39. float STDPH1=7,STDPH2=4;      // PH for calibration STD 1,2
  40. float STDread1=2,STDread2=1;  // PH analog read for STD 1,2
  41. float PHa=3,PHb=1;            // PH equation constant
  42. byte  PH_calib_timer=120;    
  43. int   phanalog=0;
  44. float ph=0,ph1=0,ph2=0,ph3=0;
  45.  
  46. // filter
  47. bool filterstatus=0;
  48. byte filtercounter=0;
  49. unsigned long lastmillisf=0;
  50. int filterdelay=2000;
  51. // eeprom
  52. #include<EEPROM.h>
  53. byte savedone=0;
  54. byte eedata=0;
  55.  
  56. // keypad
  57. byte input=0;
  58. byte lastInput1=0,lastInput2=0,lastInput3=0,lastInput4=0;
  59. byte key=16; //10>>A 11>>B 12>>C 13>>D 14>>* 15>>#
  60.  
  61. // shift reg
  62.  
  63. byte clk=D3,latch=D4,data=D0;
  64. byte dataout1=0b11111111;
  65. byte dataout2=0b11111111;
  66. byte dataout3=0b00001111;
  67. byte x=0;
  68. byte r=0;
  69.  
  70. //lcd
  71. #include <LiquidCrystal_I2C.h>
  72. LiquidCrystal_I2C lcd (0x27,20,4);
  73.  
  74. // lock
  75. byte customChar[] = {0x0E,0x11,0x11,0x11,0x1F,0x1B,0x1B,0x1F};// lock
  76. byte locktime=1;
  77. byte locktimer=5;
  78. int password=1234;
  79. int interpassword=0;
  80.  
  81.  
  82. // display
  83. byte pointer1=0,pointer2=0,pointer3=0;
  84. byte number1=0,number2=0,number3=0,number4=0;
  85. bool timedis=0;
  86. bool main_page_data=0;
  87.  
  88. // valves data
  89. byte valvenum=1;
  90. byte valvedu[10]={0,0,0,0,0,0,0,0,0,0};
  91. byte valvePH[10]={0,0,0,0,0,0,0,0,0,0};
  92. byte valveCE[10]={0,0,0,0,0,0,0,0,0,0};
  93. byte fer1per[10]={0,0,0,0,0,0,0,0,0,0};
  94. byte fer2per[10]={0,0,0,0,0,0,0,0,0,0};
  95. byte fer3per[10]={0,0,0,0,0,0,0,0,0,0};
  96. byte fer4per[10]={0,0,0,0,0,0,0,0,0,0};
  97. byte fer5per[10]={0,0,0,0,0,0,0,0,0,0};
  98.  
  99. // test valve
  100. byte testvalve=0;
  101. bool valvestatus[10]={0,0,0,0,0,0,0,0,0,0};
  102. bool testvalvepage=0;
  103.  
  104. // program data
  105. byte prognum=1;
  106. bool progact[25]={0,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  107. bool progday1[25]={0,0,0,0,1,0,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//S
  108. bool progday2[25]={0,0,0,0,1,1,0,0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//M
  109. bool progday3[25]={0,0,1,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//T
  110. bool progday4[25]={0,0,0,0,0,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//W
  111. bool progday5[25]={0,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//T
  112. bool progday6[25]={0,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//F
  113. bool progday7[25]={0,0,1,0,0,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//S
  114. byte proghour[25]={0,0,0,0,12,10,0,4,0,0,8,1,0,1,0,1,0,1,0,1,0,1,0,1,0}; //hour
  115. byte progmin[25] ={0,0,15,0,30,1,0,1,0,0,10,1,0,15,0,1,0,31,0,1,0,1,0,1,0}; //min
  116. byte progvalve=0;
  117. bool progvalve1[25]={0,0,0,0,1,0,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  118. bool progvalve2[25]={0,0,0,0,1,1,0,0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  119. bool progvalve3[25]={0,0,1,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  120. bool progvalve4[25]={0,0,0,0,0,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  121. bool progvalve5[25]={0,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  122. bool progvalve6[25]={0,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  123. bool progvalve7[25]={0,0,1,0,0,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  124. bool progvalve8[25]={0,1,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,1,0};
  125. bool progvalve9[25]={0,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,0,0};
  126. bool progvalve10[25]={0,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
  127.  
  128. // run prog
  129. byte valverun=0;
  130. byte valvetimer[10]={0,0,0,0,0,0,0,0,0,0};
  131. bool on_off_timer=0;
  132. byte on_timer=0;
  133. int  off_timer=0;
  134. unsigned long lastmillis=0;
  135. unsigned long lastmillis1=0;
  136.  
  137. // system on off timer
  138. byte progyear=0;
  139. byte progmonth=0;
  140. byte progday=0;
  141. byte progh=0;
  142. byte progm=0;
  143. bool fastmode=0;
  144. bool progsta=0;// 0>>starting 1>>paused
  145. byte stopedprog=0;
  146.  
  147. // test prog
  148. byte testprog=0,runprog=0,lastrunprog=0;
  149.  
  150. // set clock
  151. byte setyear=20,setmonth=12,setday=6,sethour=17,setmin=29;
  152. // timer
  153. #include <PCF8563.h>
  154. PCF8563 pcf;
  155. byte day =5;
  156. String dayStr[7]={"Sunday","Monday","Tuesday","Wednesday","Thuresday","Friday","Saturday"};
  157. byte RTCday=0;
  158. byte lastRTCday=0;
  159. byte RTCsec=0;
  160. byte lastRTCsec=0;
  161. byte seccounter=0;
  162. byte RTCmin=0;
  163. byte lastRTCmin=0;
  164.  
  165. void setup() {
  166.   // pin
  167.   pinMode(clk, OUTPUT); pinMode(latch, OUTPUT) ; pinMode(data, OUTPUT);
  168.   Serial.begin(9600);
  169.    // shift reg
  170.   digitalWrite(latch,0);
  171.   shiftOut(data,clk,MSBFIRST,dataout3);
  172.   shiftOut(data,clk,MSBFIRST,dataout2);
  173.   shiftOut(data,clk,MSBFIRST,dataout1);
  174.   digitalWrite(latch,1);
  175.  
  176.  //timer
  177.   pcf.init();//initialize the clock
  178.   pcf.stopClock();//stop the clock
  179.   //set time
  180. /*pcf.setYear(20);//set year
  181.   pcf.setMonth(12);//set month
  182.   pcf.setDay(6);//set dat
  183.   pcf.setHour(17);//set hour
  184.   pcf.setMinut(29);//set minut
  185.   pcf.setSecond(50);//set second*/
  186.   pcf.startClock();//start the clock
  187.    Time nowTime = pcf.getTime();//get current time
  188.    RTCday=nowTime.day;
  189.    lastRTCday=RTCday;
  190.    RTCsec=nowTime.second;
  191.    lastRTCsec=RTCsec;
  192.    RTCmin=nowTime.minute;
  193.    lastRTCmin=RTCmin;
  194.    
  195.    //lcd
  196.   lcd.init();
  197.   lcd.backlight();
  198.   lcd.clear();
  199.   lcd.createChar(0, customChar);
  200.   lcd.home();
  201.   lcd.print (" loading data ");
  202.  
  203.   // eeprom
  204.   EEPROM.begin(1024);
  205.   load();
  206.   // key pad
  207.   Wire.begin();
  208.   Wire.beginTransmission(0x20);
  209.   Wire.write(0x00);      // IO0dir reg port A
  210.   Wire.write(0xff);      // input
  211.   Wire.endTransmission();
  212.   Wire.begin();
  213.   Wire.beginTransmission(0x20);
  214.   Wire.write(0x01);      // IO0dir reg port B
  215.   Wire.write(0x00);      // output
  216.   Wire.endTransmission();
  217.   Wire.beginTransmission(0x20);
  218.   Wire.write(0x0C);      // pull up res port A
  219.   Wire.write(0xff);      
  220.   Wire.endTransmission();
  221.   // load last prog data
  222.   loadrunprogdata();
  223.   check_off_timer();
  224.   lcd.clear();
  225.   // load day
  226.   loadday();
  227.  
  228. }
  229.  
  230. void loop() {
  231.   // RTC_LCD__test();
  232.   shift_serial_test ();
  233.   getKey();
  234.   lcddisplay();
  235.  
  236.   if      (progsta==0){      
  237.   if      (runprog==0 && fastmode==0){ checkprog();saverunprogdata();}
  238.   else if (runprog==0 && fastmode==1){ fastcheck();}
  239.   else if (runprog!=0 && fastmode==0){ runprogfun();saverunprogdata();}
  240.   else if (runprog!=0 && fastmode==1){ runprogfun();}
  241.   if (runprog!=0){if   (stopedprog==runprog){runprog=0;}
  242.                   else                      {stopedprog=0;}}
  243.   // timer
  244.   unsigned long currentmillis=millis();
  245.   if ((unsigned long)(currentmillis-lastmillis1)>=10){lastmillis1=currentmillis;readph();}}
  246.   // stop all
  247.   if (runprog==0 && testvalvepage==0){stopall();}
  248.   // filters
  249.   if       (filterstatus==0){checkfilter();}
  250.   else if  (filterstatus==1){runfilter();}
  251.   // main pump off
  252.   if (runprog==0){bitWrite(dataout3,0,1);shiftout();}
  253.   // read ph serial
  254.  
  255.   }
  256.  
  257. void getKey(){
  258.   // keypad read
  259.   key=16;
  260.   // Write :
  261.   Wire.beginTransmission(0x20);
  262.   Wire.write(0x13);      // port B data address
  263.   Wire.write(0b11111110);      // port B data
  264.   Wire.endTransmission();
  265.   // read
  266.   Wire.beginTransmission(0x20);
  267.   Wire.write(0x12);      // port A data address
  268.   Wire.endTransmission();
  269.   Wire.requestFrom(0x20,1); //request 1 byte
  270.   if(Wire.available()){
  271.     input=Wire.read();
  272.     if (input!=lastInput1){
  273.     if      ((input/16)==14){key=1;}
  274.     else if ((input/16)==13){key=2;}
  275.     else if ((input/16)==11){key=3;}
  276.     else if ((input/16)==7 ){key=10;}
  277.   }}
  278.   lastInput1=input;
  279.     // Write :
  280.   Wire.beginTransmission(0x20);
  281.   Wire.write(0x13);      // port B data address
  282.   Wire.write(0b11111101);      // port B data
  283.   Wire.endTransmission();
  284.   // read
  285.   Wire.beginTransmission(0x20);
  286.   Wire.write(0x12);      // port A data address
  287.   Wire.endTransmission();
  288.   Wire.requestFrom(0x20,1); //request 1 byte
  289.   if(Wire.available()){
  290.     input=Wire.read();
  291.     if (input!=lastInput2){
  292.     if      ((input/16)==14){key=4;}
  293.     else if ((input/16)==13){key=5;}
  294.     else if ((input/16)==11){key=6;}
  295.     else if ((input/16)==7 ){key=11;}}}
  296.     lastInput2=input;
  297.   // Write :
  298.   Wire.beginTransmission(0x20);
  299.   Wire.write(0x13);      // port B data address
  300.   Wire.write(0b11111011);      // port B data
  301.   Wire.endTransmission();
  302.   // read
  303.   Wire.beginTransmission(0x20);
  304.   Wire.write(0x12);      // port A data address
  305.   Wire.endTransmission();
  306.   Wire.requestFrom(0x20,1); //request 1 byte
  307.   if(Wire.available()){
  308.     input=Wire.read();
  309.     if (input!=lastInput3){
  310.     if      ((input/16)==14){key=7;}
  311.     else if ((input/16)==13){key=8;}
  312.     else if ((input/16)==11){key=9;}
  313.     else if ((input/16)==7 ){key=12;}}}
  314.     lastInput3=input;
  315.   // Write :
  316.   Wire.beginTransmission(0x20);
  317.   Wire.write(0x13);      // port B data address
  318.   Wire.write(0b11110111);      // port B data
  319.   Wire.endTransmission();
  320.   // read
  321.   Wire.beginTransmission(0x20);
  322.   Wire.write(0x12);      // port A data address
  323.   Wire.endTransmission();
  324.   Wire.requestFrom(0x20,1); //request 1 byte
  325.   if(Wire.available()){
  326.     input=Wire.read();
  327.     if (input!=lastInput4){
  328.     if      ((input/16)==14){key=14;}
  329.     else if ((input/16)==13){key=0;}
  330.     else if ((input/16)==11){key=15;}
  331.     else if ((input/16)==7 ){key=13;}}}
  332.     lastInput4=input;
  333. }
  334. void shift_serial_test (){
  335.    // reading serial
  336.   if(Serial.available()>0){
  337.     x=Serial.read();
  338.     if      (x=='a'){r=1;}
  339.     else if (x=='b'){r=2;}
  340.     else if (x=='c'){r=3;}
  341.     else {
  342.       if (r==1){bitWrite(dataout1,x-49,!bitRead(dataout1,x-49));r=0;}
  343.       if (r==2){bitWrite(dataout2,x-49,!bitRead(dataout2,x-49));r=0;}
  344.       if (r==3 && (x-49)<4){bitWrite(dataout3,x-49,!bitRead(dataout3,x-49));r=0;}
  345.     }
  346.  digitalWrite(latch,0);
  347.   shiftOut(data,clk,MSBFIRST,dataout3);
  348.   shiftOut(data,clk,MSBFIRST,dataout2);
  349.   shiftOut(data,clk,MSBFIRST,dataout1);
  350.   digitalWrite(latch,1);}
  351. }
  352. void valvecontrol (){
  353.  bitWrite(dataout1,0,!valvestatus[1]);
  354.  bitWrite(dataout1,1,!valvestatus[0]);
  355.  bitWrite(dataout2,0,!valvestatus[9]);
  356.  bitWrite(dataout2,1,!valvestatus[8]);
  357.  bitWrite(dataout2,2,!valvestatus[7]);
  358.  bitWrite(dataout2,3,!valvestatus[6]);
  359.  bitWrite(dataout2,4,!valvestatus[5]);
  360.  bitWrite(dataout2,5,!valvestatus[4]);
  361.  bitWrite(dataout2,6,!valvestatus[3]);
  362.  bitWrite(dataout2,7,!valvestatus[2]);
  363.   digitalWrite(latch,0);
  364.   shiftOut(data,clk,MSBFIRST,dataout3);
  365.   shiftOut(data,clk,MSBFIRST,dataout2);
  366.   shiftOut(data,clk,MSBFIRST,dataout1);
  367.   digitalWrite(latch,1);
  368.   }
  369. void RTC_LCD__test(){
  370.    lcd.setCursor(0,0);
  371.   Time nowTime = pcf.getTime();//get current time
  372.  
  373.   //print current time
  374.   lcd.print("date:");
  375.   lcd.print(nowTime.day);
  376.   RTCday=nowTime.day;
  377.   if (!(lastRTCday==RTCday)){day++;
  378.      if (day==7){day=0;}}
  379.   lastRTCday=RTCday;
  380.   lcd.print("/");
  381.   lcd.print(nowTime.month);
  382.   lcd.print("/");
  383.   lcd.print(nowTime.year);
  384.   lcd.setCursor(0,1);
  385.   lcd.print("time:");
  386.   lcd.print(nowTime.hour);
  387.   lcd.print(":");
  388.   lcd.print(nowTime.minute);
  389.   lcd.print(":");
  390.   lcd.print(nowTime.second);
  391.   lcd.setCursor(0,2);
  392.   lcd.print("day:");
  393.   lcd.print(dayStr [day]);
  394. }
  395. void setclock(){
  396.    //timer
  397.   pcf.init();//initialize the clock
  398.   pcf.stopClock();//stop the clock
  399.   //set time
  400.   if (pointer2==2){pcf.setYear(setyear);}//set year
  401.   if (pointer2==1){pcf.setMonth(setmonth);}//set month
  402.   if (pointer2==0){pcf.setDay(setday);}//set dat
  403.   if (pointer2==4){pcf.setHour(sethour);}//set hour
  404.   if (pointer2==5){pcf.setMinut(setmin);}//set minut
  405.   pcf.setSecond(0);//set second
  406.   pcf.startClock();//start the clock
  407.    Time nowTime = pcf.getTime();//get current time
  408. }
  409. void lcddisplay(){
  410.   // day timer
  411.   Time nowTime = pcf.getTime();//get current time
  412.   // day
  413.   RTCday=nowTime.day;
  414.   if (!(lastRTCday==RTCday)){day++; if (day==7){day=0;} saveday();}
  415.   lastRTCday=RTCday;
  416.  
  417.   if (pointer1==0){  // home
  418.   if (main_page_data==0){
  419.    lcd.setCursor(0,2);   lcd.print("valve  :");
  420.    lcd.setCursor(0,1);   lcd.print("program:");
  421.    lcd.setCursor(0,3);   lcd.print("PH:");
  422.    lcd.setCursor(10,3);   lcd.print("CE:");}
  423.     // sec
  424.   RTCsec=nowTime.second;
  425.   if (!(lastRTCsec==RTCsec)){if (locktimer>0){locktimer--;}
  426.   //Serial.println("ph="); Serial.println(ph);
  427.   //date and time
  428.   seccounter++; if (seccounter==5){timedis=!timedis;seccounter=0;}
  429.   lcd.setCursor(0,0);   lcd.print(dayStr [day]);
  430.   if (timedis==1){lcd.setCursor(9,0); if (nowTime.day<10){lcd.print("0");}   lcd.print(nowTime.day);
  431.                  lcd.print("/");      if (nowTime.month<10){lcd.print("0");} lcd.print(nowTime.month);
  432.                  lcd.print("/");      lcd.print("20");                       lcd.print(nowTime.year); }
  433.   else           {lcd.setCursor(9,0); if (nowTime.hour<10)  {lcd.print("0");}lcd.print(nowTime.hour);
  434.                   lcd.print(":");     if (nowTime.minute<10){lcd.print("0");}lcd.print(nowTime.minute);
  435.                   lcd.print(":");     if (nowTime.second<10){lcd.print("0");}lcd.print(nowTime.second);lcd.print("  ");}
  436.   // current prog
  437.   lcd.setCursor(8,1);  
  438.   if (runprog>0){lcd.print(runprog);lcd.print(" ");}
  439.   else          {lcd.print("   ");}
  440.   if (progsta==0){lcd.print("running");lcd.print(" ");}
  441.   else           {lcd.print("paused ");lcd.print(" ");}
  442.   // current valve
  443.   lcd.setCursor(8,2);if (valverun>0){lcd.print(valverun);}lcd.print("  ");
  444.   // ph
  445.   lcd.setCursor(3,3);
  446.   lcd.print(ph,1);lcd.print("/");
  447.   if (valverun>0){
  448.     if (valvePH[valverun-1]>9){lcd.print(valvePH[valverun-1]/10);lcd.print(".");lcd.print(valvePH[valverun-1]%10);}
  449.     else                      {lcd.print(valvePH[valverun-1]);lcd.print(".0");}}
  450.   else {lcd.print("   ");}  
  451.   // CE
  452.   lcd.setCursor(14,3);  
  453.   if (valverun>0){
  454.   if (valveCE[valverun-1]>9){lcd.print(valveCE[valverun-1]/10);lcd.print(".");lcd.print(valveCE[valverun-1]%10);lcd.print(" ");}
  455.   else                     {lcd.print(valveCE[valverun-1]);lcd.print(".0");}}
  456.   else {lcd.print("   ");}
  457.   //T
  458.   if (progsta==0){
  459.      if (valvetimer[valverun-1]>0 && runprog>0){lcd.setCursor(11,2);lcd.print(valvetimer[valverun-1]-1);lcd.print(":");lcd.print(59-nowTime.second);lcd.print("    ");}
  460.      else                          {
  461.       if (valverun==0 && runprog==0)   {lcd.setCursor(11,2);lcd.print("       ");}
  462.       else                             {lcd.setCursor(11,2);lcd.print("waiting");}}}
  463.  
  464.   }
  465.   main_page_data=1;
  466. if (key==10){ locktimer=0;main_page_data=0;key=16; }// lock
  467. if (locktimer>0){  
  468.   if      (key==12){pointer1=1;pointer2=0; lcd.clear();main_page_data=0;}//setting
  469.   else if (key==15){ lcd.clear();main_page_data=0;continueprog();}//start
  470.   else if (key==14){ lcd.clear();main_page_data=0;stopprog();     //paused  
  471.                     bitWrite(dataout3,0,1);shiftout();}// main pump off                
  472.   else if (key==0) { lcd.clear();main_page_data=0;stopedprog=runprog;stopall();}}//stop
  473. else {  
  474.   lcd.setCursor(19,0); lcd.write(0);// lock
  475.   if (key<16){pointer1=13;pointer2=0; lcd.clear();main_page_data=0;}key=16;}
  476.   lastRTCsec=RTCsec;}
  477.  
  478.   else if (pointer1==1){ //setting
  479.     if (main_page_data==0){
  480.   lcd.setCursor(0,0);   lcd.print("1_valve");  
  481.   lcd.setCursor(10,0);   lcd.print("2_program");
  482.   lcd.setCursor(0,1);   lcd.print("3_clock");
  483.   lcd.setCursor(10,1);   lcd.print("4_test_val");
  484.   lcd.setCursor(0,2);   lcd.print("5_test_pro");
  485.   lcd.setCursor(10,2);   lcd.print("6_reset");
  486.   lcd.setCursor(0,3);   lcd.print("7_calib");
  487.   lcd.setCursor(10,3);   lcd.print("8_other");}
  488.    main_page_data=1;
  489.   if      (key==13){pointer1=0;pointer2=0;key=16 ;lcd.clear();save();main_page_data=0;} // home
  490.   else if (key==1 ){pointer1=2;pointer2=0;key=16 ; lcd.clear();main_page_data=0;} // valve setting
  491.   else if (key==2 ){pointer1=4;pointer2=0;key=16; lcd.clear();main_page_data=0;} // program setting
  492.   else if (key==3 ){pointer1=7;pointer2=0;key=16; lcd.clear();main_page_data=0;} // time setting
  493.   else if (key==4 ){pointer1=8;pointer2=0;key=16; lcd.clear();main_page_data=0;testvalvepage=1;} // test valve
  494.   else if (key==5 ){pointer1=9;pointer2=0;key=16; lcd.clear();main_page_data=0;}// test prog
  495.   else if (key==6 ){pointer1=10;pointer2=0;key=16; lcd.clear();main_page_data=0;}// reset
  496.   else if (key==7 ){pointer1=11;pointer2=0;key=16; lcd.clear();main_page_data=0;}// calibration
  497.   else if (key==8 ){pointer1=12;pointer2=0;key=16; lcd.clear();main_page_data=0;}// other (screen lock setting)
  498.                   //pointer1=13 >> lock screen
  499.    }
  500.  
  501.   else if (pointer1==2){  // valve setting page 1
  502.    // num  
  503.   lcd.setCursor(0,0); lcd.print("valve num");
  504.   if (pointer2==0){lcd.print(">");
  505.    if      (key==10 && valvenum<10){valvenum++;}
  506.    else if (key==11 && valvenum>1) {valvenum--;}
  507.     if (key<10){
  508.     if      (pointer3==0){number1=key;valvenum=number1;                                                pointer3=1;}
  509.     else if (pointer3==1){number2=key;valvenum=number1*10+number2; if (valvenum>11){valvenum=number2;} pointer3=0;}}}
  510.   else {lcd.print(":");}
  511.   lcd.print(" "); lcd.print(valvenum);lcd.print(" ");
  512.   //duration
  513.  lcd.setCursor(0,1); lcd.print("duration ");
  514.  if (pointer2==1){lcd.print(">");
  515.     if (key<10){
  516.               if      (pointer3==0){number1=key;valvedu[valvenum-1]=number1;                 pointer3=1;}
  517.               else if (pointer3==1){number2=key;valvedu[valvenum-1]=number1*10+number2;
  518.                                    if (valvedu[valvenum-1]>60){valvedu[valvenum-1]=number2;} pointer3=0;}}}
  519.  else {lcd.print(":");}
  520.  lcd.print(valvedu[valvenum-1]);lcd.print("min   ");
  521.  // ph
  522.  lcd.setCursor(0,2); lcd.print("PH");
  523.  if (pointer2==2){lcd.print(">");
  524.     if (key<10){
  525.        if      (pointer3==0){number1=key;valvePH[valvenum-1]=number1;                        pointer3=1;}
  526.        else if (pointer3==1){number2=key;valvePH[valvenum-1]=number1*10+number2;             pointer3=2;}
  527.        else if (pointer3==2){number3=key;valvePH[valvenum-1]=number1*100+number2*10+number3;
  528.        if (valvePH[valvenum-1]>140){valvePH[valvenum-1]=number2+number3*0.1;} pointer3=0;}}}
  529.  else {lcd.print(":");}
  530.  if (valvePH[valvenum-1]>9){lcd.print(" ");lcd.print(valvePH[valvenum-1]/10);lcd.print(".");lcd.print(valvePH[valvenum-1]%10);lcd.print("  ");}
  531.  else                      {lcd.print(" ");lcd.print(valvePH[valvenum-1]);lcd.print(".0 ");}
  532.  // CE
  533.  lcd.setCursor(10,2); lcd.print("CE");
  534.   if (pointer2==3){lcd.print(">");
  535.       if (key<10){
  536.         if      (pointer3==0){number1=key;valveCE[valvenum-1]=number1;                        pointer3=1;}
  537.         else if (pointer3==1){number2=key;valveCE[valvenum-1]=number1*10+number2;             pointer3=2;}
  538.         else if (pointer3==2){number3=key;valveCE[valvenum-1]=number1*100+number2*10+number3; pointer3=0;}}}
  539.   else {lcd.print(":");}
  540.  if (valveCE[valvenum-1]>9){lcd.print(" ");lcd.print(valveCE[valvenum-1]/10);lcd.print(".");lcd.print(valveCE[valvenum-1]%10);lcd.print("  ");}
  541.  else                      {lcd.print(" ");lcd.print(valveCE[valvenum-1]);lcd.print(".0 ");}
  542.  lcd.setCursor(0,3); lcd.print("p 1/2");
  543.  if (key==13){pointer1=0; lcd.clear();save();}  // home
  544.  else if (key==12){pointer1=1; lcd.clear();}  // settings
  545.  else if (key==15 && pointer2<4){pointer2++;pointer3=0;number1=0;number2=0;number3=0;} // next
  546.  else if (key==14 && pointer2>0){pointer2--;pointer3=0;number1=0;number2=0;number3=0;} // Beck
  547.  else if (key==14 && pointer2==0){pointer2=5;pointer3=0;number1=0;number2=0;number3=0;} // Beck to page 2
  548.  if (pointer2==4){pointer1=3;pointer2=1;pointer3=0;number1=0;number2=0;number3=0; lcd.clear(); key=16;}//go to page 2
  549.  if (pointer2==5){pointer1=3;pointer2=6;pointer3=0;number1=0;number2=0;number3=0; lcd.clear();if (valvenum>1){valvenum--;}}//back to page 2
  550.  }
  551.  
  552.  else if (pointer1==3){ // valve setting page 2
  553.     // num
  554.    lcd.setCursor(0,0); lcd.print("valve num");
  555.    if (pointer2==1){lcd.print(">");
  556.      if      (key==10 && valvenum<10){valvenum++;}
  557.      else if (key==11 && valvenum>1) {valvenum--;}
  558.       if (key<10){
  559.         if      (pointer3==0){number1=key;valvenum=number1;                                                pointer3=1;}
  560.         else if (pointer3==1){number2=key;valvenum=number1*10+number2; if (valvenum>11){valvenum=number2;} pointer3=0;}}}
  561.      else {lcd.print(":");}
  562.    lcd.print(" ");lcd.print(valvenum);lcd.print(" ");
  563.    //F1
  564.    lcd.setCursor(0,1); lcd.print("F1");
  565.      if (pointer2==2){lcd.setCursor(2,1);lcd.print(">");
  566.       if (key<10){
  567.        if      (pointer3==0){number1=key;fer1per[valvenum-1]=number1;                        pointer3=1;}
  568.        else if (pointer3==1){number2=key;fer1per[valvenum-1]=number1*10+number2;             pointer3=2;}
  569.        else if (pointer3==2){number3=key;fer1per[valvenum-1]=number1*100+number2*10+number3; pointer3=0;}}
  570.        if (fer1per[valvenum-1]>101){fer1per[valvenum-1]=number2*10+number3;}}
  571.      else {lcd.print(":");}
  572.      lcd.print(fer1per[valvenum-1]);lcd.print("  ");
  573.     //F2
  574.    lcd.setCursor(7,1); lcd.print("F2");
  575.    if (pointer2==3){lcd.setCursor(9,1);lcd.print(">");
  576.       if (key<10){
  577.        if      (pointer3==0){number1=key;fer2per[valvenum-1]=number1;                        pointer3=1;}
  578.        else if (pointer3==1){number2=key;fer2per[valvenum-1]=number1*10+number2;             pointer3=2;}
  579.        else if (pointer3==2){number3=key;fer2per[valvenum-1]=number1*100+number2*10+number3; pointer3=0;}}
  580.        if (fer2per[valvenum-1]>101){fer2per[valvenum-1]=number2*10+number3;}}
  581.    else {lcd.print(":");}
  582.    lcd.print(fer2per[valvenum-1]);lcd.print("  ");
  583.    //F3
  584.    lcd.setCursor(14,1);lcd.print("F3");
  585.      if (pointer2==4){lcd.setCursor(16,1);lcd.print(">");
  586.       if (key<10){
  587.        if      (pointer3==0){number1=key;fer3per[valvenum-1]=number1;                        pointer3=1;}
  588.        else if (pointer3==1){number2=key;fer3per[valvenum-1]=number1*10+number2;             pointer3=2;}
  589.        else if (pointer3==2){number3=key;fer3per[valvenum-1]=number1*100+number2*10+number3; pointer3=0;}}
  590.        if (fer3per[valvenum-1]>101){fer3per[valvenum-1]=number2*10+number3;}}
  591.      else {lcd.print(":");}
  592.      lcd.print(fer3per[valvenum-1]);lcd.print("  ");
  593.     //F4  
  594.    lcd.setCursor(0,2); lcd.print("F4");
  595.      if (pointer2==5){lcd.setCursor(2,2);lcd.print(">");
  596.       if (key<10){
  597.        if      (pointer3==0){number1=key;key=16;fer4per[valvenum-1]=number1;                        pointer3=1;}
  598.        else if (pointer3==1){number2=key;key=16;fer4per[valvenum-1]=number1*10+number2;             pointer3=2;}
  599.        else if (pointer3==2){number3=key;key=16;fer4per[valvenum-1]=number1*100+number2*10+number3; pointer3=0;}}
  600.        if (fer4per[valvenum-1]>101){fer4per[valvenum-1]=number2*10+number3;}}
  601.      else{lcd.print(":");}
  602.    lcd.print(fer4per[valvenum-1]);lcd.print("  ");
  603.     //F5
  604.    lcd.setCursor(7,2); lcd.print("F5");
  605.    if (pointer2==6){lcd.setCursor(9,2);lcd.print(">");
  606.      if (key<10){
  607.        if      (pointer3==0){number1=key;key=16;fer5per[valvenum-1]=number1;                        pointer3=1;}
  608.        else if (pointer3==1){number2=key;key=16;fer5per[valvenum-1]=number1*10+number2;             pointer3=2;}
  609.        else if (pointer3==2){number3=key;key=16;fer5per[valvenum-1]=number1*100+number2*10+number3; pointer3=0;}}
  610.        if (fer5per[valvenum-1]>101){fer5per[valvenum-1]=number2*10+number3;}}
  611.    else {lcd.print(":");}  
  612.    lcd.print(fer5per[valvenum-1]);lcd.print("  ");
  613.    
  614.    lcd.setCursor(0,3); lcd.print("p 2/2");
  615.    if      (key==15 && pointer2<7){pointer2++;pointer3=0;number1=0;number2=0;number3=0;} // next
  616.    else if (key==14 && pointer2>0){pointer2--;pointer3=0;number1=0;number2=0;number3=0;} // Beck
  617.    else if (key==13){pointer1=0; lcd.clear();save();main_page_data=0;}  // home
  618.    else if (key==12){pointer1=1; lcd.clear();main_page_data=0;}  // settings
  619.    if (pointer2==0){pointer1=2;pointer2=3;pointer3=0;number1=0;number2=0;number3=0;lcd.clear();main_page_data=0;}  // page1
  620.    if (pointer2==7){pointer1=2;pointer2=0;pointer3=0;number1=0;number2=0;number3=0;lcd.clear();valvenum++;main_page_data=0;}  //page 1
  621.     }
  622.    
  623.   else if (pointer1==4){ // program setting page 1
  624.    // num
  625.    lcd.setCursor(0,0); lcd.print("prog num: ");
  626.    if (pointer2==0){lcd.print(">");
  627.       if (key<10){
  628.         if      (pointer3==0){number1=key;prognum=number1;                                              pointer3=1;}
  629.         else if (pointer3==1){number2=key;prognum=number1*10+number2; if (prognum>25){prognum=number2;} pointer3=0;}}
  630.       if      (key==10 && prognum<25){prognum++;}
  631.       else if (key==11 && prognum>1) {prognum--;}}
  632.    else {lcd.print(":");}
  633.    lcd.print(" ");lcd.print(prognum);lcd.print(" ");
  634.    // ativation
  635.    lcd.setCursor(0,1); lcd.print("Activation");
  636.    if (pointer2==1){lcd.print(">");
  637.    if (key==10 || key==11){progact[prognum-1]=!progact[prognum-1];}}
  638.    else {lcd.print(":");}
  639.    if      (progact[prognum-1]==1){lcd.print(" on ");}
  640.    else if (progact[prognum-1]==0){lcd.print(" off ");}
  641.    //day
  642.    lcd.setCursor(0,2); lcd.print("days      ");
  643.     if (pointer2==2){lcd.print(">");
  644.        if      (key==1){progday1[prognum-1]=!progday1[prognum-1];}
  645.        else if (key==2){progday2[prognum-1]=!progday2[prognum-1];}
  646.        else if (key==3){progday3[prognum-1]=!progday3[prognum-1];}
  647.        else if (key==4){progday4[prognum-1]=!progday4[prognum-1];}
  648.        else if (key==5){progday5[prognum-1]=!progday5[prognum-1];}
  649.        else if (key==6){progday6[prognum-1]=!progday6[prognum-1];}
  650.        else if (key==7){progday7[prognum-1]=!progday7[prognum-1];}}
  651.     else {lcd.print(":");}
  652.    if (progday1[prognum-1]==1){lcd.print("S");} else {lcd.print(" ");}
  653.    if (progday2[prognum-1]==1){lcd.print("M");} else {lcd.print(" ");}
  654.    if (progday3[prognum-1]==1){lcd.print("T");} else {lcd.print(" ");}
  655.    if (progday4[prognum-1]==1){lcd.print("W");} else {lcd.print(" ");}
  656.    if (progday5[prognum-1]==1){lcd.print("T");} else {lcd.print(" ");}
  657.    if (progday6[prognum-1]==1){lcd.print("F");} else {lcd.print(" ");}
  658.    if (progday7[prognum-1]==1){lcd.print("S");} else {lcd.print(" ");}
  659.    
  660.    lcd.setCursor(0,3); lcd.print("p 1/3");
  661.    if      (key==15 && pointer2<7){pointer2++;pointer3=0;number1=0;number2=0;number3=0;} // next
  662.    else if (key==14 && pointer2>0){pointer2--;pointer3=0;number1=0;number2=0;number3=0;} // Beck
  663.    else if (key==14 && pointer2==0){pointer2=4;pointer3=0;number1=0;number2=0;number3=0;main_page_data=0;} // Beck to page 3
  664.    else if (key==13){pointer1=0; lcd.clear();save();}  // home
  665.    else if (key==12){pointer1=1; lcd.clear();main_page_data=0;}  // settings
  666.    if (pointer2==3){pointer1=5;pointer2=1; lcd.clear(); key=16;main_page_data=0;}//page 2
  667.    if (pointer2==4){pointer1=6;pointer2=3; lcd.clear(); key=16;if (prognum>1){prognum--;}main_page_data=0;}//page 3
  668.   }
  669.    
  670.   else if (pointer1==5){ // program setting page 2
  671.      // num
  672.    lcd.setCursor(0,0); lcd.print("prog num ");
  673.    if (pointer2==1){lcd.print(">");
  674.      if (key<10){
  675.         if      (pointer3==0){number1=key;prognum=number1;                                              pointer3=1;}
  676.         else if (pointer3==1){number2=key;prognum=number1*10+number2; if (prognum>25){prognum=number2;} pointer3=0;}}
  677.      if      (key==10 && prognum<25){prognum++;}
  678.      else if (key==11 && prognum>1) {prognum--;}}
  679.    else {lcd.print(":");}
  680.    lcd.print(prognum);lcd.print(" ");
  681.    // valve num
  682.    lcd.setCursor(0,1);lcd.print("Write valve num:");
  683.    if (pointer2==2){lcd.setCursor(15,1); lcd.print(">");
  684.      if (key<10){
  685.         if      (pointer3==0){ number1=key;progvalve=number1;                    pointer3=1;}
  686.         else if (pointer3==1){ number2=key;progvalve=number1*10+number2;                   }}
  687.      if (key==10){pointer3=0;number1=0;number2=0;number3=0;key=16;lcd.setCursor(16,1);lcd.print("  ");
  688.          if      (progvalve==1){progvalve1[prognum-1]=!progvalve1[prognum-1];}
  689.          else if (progvalve==2){progvalve2[prognum-1]=!progvalve2[prognum-1];}
  690.          else if (progvalve==3){progvalve3[prognum-1]=!progvalve3[prognum-1];}
  691.          else if (progvalve==4){progvalve4[prognum-1]=!progvalve4[prognum-1];}
  692.          else if (progvalve==5){progvalve5[prognum-1]=!progvalve5[prognum-1];}
  693.          else if (progvalve==6){progvalve6[prognum-1]=!progvalve6[prognum-1];}
  694.          else if (progvalve==7){progvalve7[prognum-1]=!progvalve7[prognum-1];}
  695.          else if (progvalve==8){progvalve8[prognum-1]=!progvalve8[prognum-1];}
  696.          else if (progvalve==9){progvalve9[prognum-1]=!progvalve9[prognum-1];}
  697.          else if (progvalve==10){progvalve10[prognum-1]=!progvalve10[prognum-1];}
  698.          progvalve=0;} // next                          
  699.    }
  700.    lcd.setCursor(16,1); if (pointer3>0){lcd.print(progvalve);}lcd.print(" ");
  701.    lcd.setCursor(6,3);lcd.print(" V:");
  702.    
  703.    if (progvalve1[prognum-1]==1){lcd.print("1");} else {lcd.print(" ");}
  704.    if (progvalve2[prognum-1]==1){lcd.print("2");} else {lcd.print(" ");}
  705.    if (progvalve3[prognum-1]==1){lcd.print("3");} else {lcd.print(" ");}
  706.    if (progvalve4[prognum-1]==1){lcd.print("4");} else {lcd.print(" ");}
  707.    if (progvalve5[prognum-1]==1){lcd.print("5");} else {lcd.print(" ");}
  708.    if (progvalve6[prognum-1]==1){lcd.print("6");} else {lcd.print(" ");}
  709.    if (progvalve7[prognum-1]==1){lcd.print("7");} else {lcd.print(" ");}
  710.    if (progvalve8[prognum-1]==1){lcd.print("8");} else {lcd.print(" ");}
  711.    if (progvalve9[prognum-1]==1){lcd.print("9");} else {lcd.print(" ");}
  712.    if (progvalve10[prognum-1]==1){lcd.print("10");} else {lcd.print("  ");}
  713.    
  714.    lcd.setCursor(0,3); lcd.print("p 2/3");
  715.    lcd.setCursor(0,2);lcd.print("Then pess UP ");
  716.    if      (key==15 && pointer2<3){pointer2++;pointer3=0;number1=0;number2=0;number3=0;} // next
  717.    else if (key==14 && pointer2>0){pointer2--;pointer3=0;number1=0;number2=0;number3=0;} // Beck
  718.    else if (key==13){pointer1=0; lcd.clear();save();main_page_data=0;}  // home
  719.    else if (key==12){pointer1=1; lcd.clear();main_page_data=0;}  // settings
  720.    if (pointer2==0){pointer1=4;pointer2=2;pointer3=0;number1=0;number2=0;number3=0;lcd.clear();main_page_data=0;}  // page 1
  721.    if (pointer2==3){pointer1=6;pointer2=1;pointer3=0;number1=0;number2=0;number3=0;lcd.clear();key=16;main_page_data=0;}  // page 3
  722.    }
  723.    
  724.    else if (pointer1==6){ // program setting page 3
  725.      // num
  726.      lcd.setCursor(0,0); lcd.print("prog num  ");
  727.      if (pointer2==1){lcd.print(">");
  728.        if (key<10){
  729.          if      (pointer3==0){number1=key;prognum=number1;                                              pointer3=1;}
  730.          else if (pointer3==1){number2=key;prognum=number1*10+number2; if (prognum>25){prognum=number2;} pointer3=0;}}
  731.        if      (key==10 && prognum<25){prognum++;}
  732.        else if (key==11 && prognum>1) {prognum--;}}
  733.      else {lcd.print(":");}
  734.      lcd.print(prognum);lcd.print(" ");
  735.      //hour
  736.      lcd.setCursor(0,1);lcd.print("start hour");
  737.      if (pointer2==2){lcd.print(">");
  738.            if (key<10){
  739.               if      (pointer3==0){number1=key;proghour[prognum-1]=number1;                                                          pointer3=1;}
  740.               else if (pointer3==1){number2=key;proghour[prognum-1]=number1*10+number2; if (proghour[prognum-1]>23){proghour[prognum-1]=number2;} pointer3=0;}}}
  741.      else {lcd.print(":");}
  742.      lcd.print(proghour[prognum-1]);lcd.print("h  ");
  743.      //min
  744.      lcd.setCursor(0,2);lcd.print("start min ");
  745.      if (pointer2==3){lcd.print(">");
  746.            if (key<10){
  747.               if      (pointer3==0){number1=key;progmin[prognum-1]=number1;                                                         pointer3=1;}
  748.               else if (pointer3==1){number2=key;progmin[prognum-1]=number1*10+number2; if (progmin[prognum-1]>59){progmin[prognum-1]=number2;} pointer3=0;}}}
  749.      else {lcd.print(":");}
  750.      lcd.print(progmin[prognum-1]);lcd.print("min  ");
  751.      lcd.setCursor(0,3); lcd.print("p 3/3");
  752.      if      (key==15 && pointer2<5){pointer2++;pointer3=0;number1=0;number2=0;number3=0;} // next
  753.      else if (key==14 && pointer2>0){pointer2--;pointer3=0;number1=0;number2=0;number3=0;} // Beck
  754.      else if (key==13){pointer1=0; lcd.clear();save();main_page_data=0;}  // home
  755.      else if (key==12){pointer1=1; lcd.clear();main_page_data=0;}  // settings
  756.      if (pointer2==0){pointer1=5;pointer2=2;pointer3=0;number1=0;number2=0;number3=0;lcd.clear();main_page_data=0;}            // page 2
  757.      if (pointer2==4){pointer1=4;pointer2=1;pointer3=0;number1=0;number2=0;number3=0;lcd.clear();prognum++;main_page_data=0;}  // page 1  
  758.    }  
  759.    
  760.   else if (pointer1==7){ // clock setting
  761.       // date
  762.       lcd.setCursor(0,0); lcd.print("data");
  763.        if (pointer2==0){lcd.print(">");
  764.        if (key<10){
  765.               if      (pointer3==0){number1=key;setday=number1;                                            pointer3=1;}
  766.               else if (pointer3==1){number2=key;setday=number1*10+number2; if (setday>31){setday=number2;} pointer3=0;}
  767.               setclock();}}
  768.        else {lcd.print(":");}
  769.       lcd.print("    ");
  770.       if (nowTime.day<10){lcd.print("0");lcd.print(nowTime.day);}
  771.       else               {lcd.print(nowTime.day);}
  772.       if (pointer2==1){lcd.print(">");
  773.         if (key<10){
  774.               if      (pointer3==0){number1=key;setmonth=number1;                                            pointer3=1;}
  775.               else if (pointer3==1){number2=key;setmonth=number1*10+number2; if (setmonth>12){setmonth=number2;} pointer3=0;}
  776.               setclock();}}
  777.       else {lcd.print("/");}
  778.       if (nowTime.month<10){lcd.print("0");lcd.print(nowTime.month); }
  779.       else                 {lcd.print(nowTime.month);}
  780.       if (pointer2==2){lcd.print(">");
  781.       if (key<10){
  782.               if      (pointer3==0){number1=key;setyear=number1;             pointer3=1;}
  783.               else if (pointer3==1){number2=key;setyear=number1*10+number2;  pointer3=0;}
  784.               setclock();}}
  785.       else {lcd.print("/");}
  786.       lcd.print("20");
  787.       if(nowTime.year>9){lcd.print(nowTime.year);}
  788.       else              {lcd.print("0");lcd.print(nowTime.year);}
  789.      // day
  790.       lcd.setCursor(0,1); lcd.print("day ");
  791.       if (pointer2==3){lcd.print(">"); if (key<8){day=key-1;}}
  792.       else {lcd.print(":");}
  793.       lcd.print("    "); lcd.print(dayStr[day]); lcd.print("    ");
  794.       // hour
  795.       lcd.setCursor(0,2); lcd.print("hour");
  796.        if (pointer2==4){lcd.print(">");
  797.        if (key<10){
  798.               if      (pointer3==0){number1=key;sethour=number1;                                              pointer3=1;}
  799.               else if (pointer3==1){number2=key;sethour=number1*10+number2; if (sethour>23){sethour=number2;} pointer3=0;}
  800.               setclock();}}
  801.        else{lcd.print(":");}
  802.        lcd.print("    ");lcd.print(nowTime.hour); lcd.print("h  ");
  803.       // min
  804.       lcd.setCursor(0,3); lcd.print("min ");
  805.           if (pointer2==5){lcd.print(">");
  806.           if (key<10){
  807.               if      (pointer3==0){number1=key;setmin=number1;                                            pointer3=1;}
  808.               else if (pointer3==1){number2=key;setmin=number1*10+number2; if (setmin>60){setmin=number2;} pointer3=0;}
  809.               setclock();}}
  810.           else {lcd.print(":");}
  811.           lcd.print("    ");lcd.print(nowTime.minute); lcd.print("min  ");
  812.       if      (key==15 && pointer2<5){pointer2++;number1=0;number2=0;} // next
  813.       else if (key==14 && pointer2>0){pointer2--;} // Beck
  814.       else if (key==13){pointer1=0; lcd.clear();main_page_data=0;saveday();}  // home
  815.       else if (key==12){pointer1=1; lcd.clear();main_page_data=0;}  // settings
  816.       }
  817.      
  818. else if (pointer1==8){ // test valve
  819.     lcd.setCursor(0,0); lcd.print("choose valve num  ");
  820.     lcd.setCursor(0,1); lcd.print("then press next >");
  821.     if (testvalve>0){lcd.print(testvalve);}lcd.print("  ");
  822.     if (key<10){
  823.     if      (pointer3==0){number1=key;testvalve=number1;              pointer3=1;}
  824.     else if (pointer3==1){number2=key;testvalve=number1*10+number2;pointer3=0;}}
  825.    
  826.     if (runprog==0){
  827.                  if (key==15){pointer3=0;valvestatus[testvalve-1]=!valvestatus[testvalve-1];testvalve=0;valvecontrol ();}
  828.                  lcd.setCursor(0,3);lcd.print("                    ");} // next
  829.     else       { lcd.setCursor(0,3);lcd.print("  wait current prog ");}
  830.    lcd.setCursor(0,2);
  831.    if (valvestatus[0]==1){lcd.print("1 ");} else {lcd.print("  ");}
  832.    if (valvestatus[1]==1){lcd.print("2 ");} else {lcd.print("  ");}
  833.    if (valvestatus[2]==1){lcd.print("3 ");} else {lcd.print("  ");}
  834.    if (valvestatus[3]==1){lcd.print("4 ");} else {lcd.print("  ");}
  835.    if (valvestatus[4]==1){lcd.print("5 ");} else {lcd.print("  ");}
  836.    if (valvestatus[5]==1){lcd.print("6 ");} else {lcd.print("  ");}
  837.    if (valvestatus[6]==1){lcd.print("7 ");} else {lcd.print("  ");}
  838.    if (valvestatus[7]==1){lcd.print("8 ");} else {lcd.print("  ");}
  839.    if (valvestatus[8]==1){lcd.print("9 ");} else {lcd.print("  ");}
  840.    if (valvestatus[9]==1){lcd.print("10");} else {lcd.print("  ");}
  841.     if (key==13){pointer1=0; lcd.clear();main_page_data=0;}                   // home
  842.     if (key==12){pointer1=1; lcd.clear();main_page_data=0;}                    // settings
  843.     }
  844.    
  845.    if (pointer1==9){ // test prog
  846.     lcd.setCursor(0,0); lcd.print("choose prog num  ");
  847.     lcd.setCursor(0,1); lcd.print("then press next >");
  848.     if (testprog>0){lcd.print(testprog);}lcd.print("  ");
  849.     lcd.setCursor(0,2); lcd.print("prog num:");
  850.     if (runprog>0){lcd.print(runprog);lcd.print(" "); lcd.setCursor(0,3); lcd.print("is running");}
  851.     if (key<10){
  852.     if      (pointer3==0){number1=key;testprog=number1;            pointer3=1;}
  853.     else if (pointer3==1){number2=key;testprog=number1*10+number2;pointer3=0;}}
  854.       if      (key==15){pointer3=0;runprog=testprog;setvalvetime();runprogfun();} // next
  855.       else if (key==13){pointer1=0; lcd.clear();main_page_data=0;}  // home
  856.       else if (key==12){pointer1=1; lcd.clear();main_page_data=0;}  // settings
  857.   }
  858.    
  859.    else if (pointer1==10){ // reset
  860.     if (pointer2==0){
  861.        lcd.setCursor(0,0); lcd.print("1-reset valve seting");
  862.        lcd.setCursor(0,1); lcd.print("2-reset prog  seting");
  863.        lcd.setCursor(0,2); lcd.print("3-reset all   seting");
  864.        if      (key==1){pointer2=1;lcd.clear();}
  865.        else if (key==2){pointer2=2;lcd.clear();}
  866.        else if (key==3){pointer2=3;lcd.clear();}
  867.        }
  868.      if (pointer2==1){
  869.       lcd.setCursor(0,0); lcd.print("     valve reset    ");
  870.       lcd.setCursor(0,1); lcd.print("press next to reset ");
  871.       lcd.setCursor(0,2); lcd.print("press back to cancel");
  872.       if (key==15){pointer2=0;lcd.clear();valvereset();}//next
  873.       else if (key==14){pointer2=0; lcd.clear();}  //back
  874.      }
  875.      if (pointer2==2){
  876.       lcd.setCursor(0,0); lcd.print("     prog reset     ");
  877.       lcd.setCursor(0,1); lcd.print("press next to reset ");
  878.       lcd.setCursor(0,2); lcd.print("press back to cancel");
  879.       if (key==15){pointer2=0;lcd.clear();progreset();}//next
  880.       else if (key==14){pointer2=0; lcd.clear();}    //back
  881.      }
  882.      if (pointer2==3){
  883.       lcd.setCursor(0,0); lcd.print("     reset all     ");
  884.       lcd.setCursor(0,1); lcd.print("press next to reset");
  885.       lcd.setCursor(0,2); lcd.print("press back to cancel");
  886.       if (key==15){pointer2=0;lcd.clear();progreset(); valvereset();}//next
  887.       else if (key==14){pointer2=0; lcd.clear();}    // back
  888.      }
  889.       if (key==13){pointer1=0; lcd.clear();main_page_data=0;}  // home
  890.       else if (key==12){pointer1=1; lcd.clear();main_page_data=0;}// settings
  891.    }
  892.    
  893.    else if (pointer1==11){ // calibration
  894.    
  895.       if (pointer2==0){ // calibration main page
  896.           lcd.setCursor(0,0);lcd.print("1_PH calib");
  897.           if (key==1){pointer2=1;lcd.clear();main_page_data=0;} // ph calibration page 1
  898.           }
  899.       // PH
  900.       else if (pointer2==1){ // ph calibration STD1 page 1
  901.           lcd.setCursor(0,0);lcd.print("put PH sensor on");
  902.           lcd.setCursor(0,1);lcd.print("STD1 ( PH=");lcd.print(STDPH1);lcd.print(")");
  903.           lcd.setCursor(0,2);lcd.print("then press next ");
  904.           if (key==15){pointer2=2; lcd.clear();PH_calib_timer=60;} // next
  905.           if (key==14){pointer2=0; lcd.clear();PH_calib_timer=60;} // back
  906.           }
  907.        else if (pointer2==2){ // ph wait page 2
  908.           lcd.setCursor(0,0);lcd.print("please wait");
  909.           //timer
  910.           pcf.getTime();//get current time
  911.           RTCsec=nowTime.second;
  912.           if (!(lastRTCsec==RTCsec)){PH_calib_timer--;
  913.           lcd.setCursor(19-PH_calib_timer%20,1);lcd.print(".");
  914.           if ((PH_calib_timer%20)==0){lcd.setCursor(0,1);lcd.print("                    ");}
  915.             if (PH_calib_timer<1){pointer2=3;readph(); STDread1=phanalog;lcd.clear();}}
  916.           lastRTCsec=RTCsec;}
  917.         else if (pointer2==3){ // ph calibration STD2 page 3
  918.           lcd.setCursor(0,0);lcd.print("put PH sensor on");
  919.           lcd.setCursor(0,1);lcd.print("STD2 ( PH=");lcd.print(STDPH2);lcd.print(")");
  920.           lcd.setCursor(0,2);lcd.print("then press next ");
  921.           if (key==15){pointer2=4; lcd.clear();PH_calib_timer=60;} // next
  922.           if (key==14){pointer2=1; lcd.clear();PH_calib_timer=60;} // back
  923.         }
  924.         else if (pointer2==4){ // ph wait page 4
  925.           lcd.setCursor(0,0);lcd.print("please wait");
  926.           //timer
  927.           pcf.getTime();//get current time
  928.           RTCsec=nowTime.second;
  929.           if (!(lastRTCsec==RTCsec)){PH_calib_timer--;
  930.           lcd.setCursor(19-PH_calib_timer%20,1);lcd.print(".");
  931.           if ((PH_calib_timer%20)==0){lcd.setCursor(0,1);lcd.print("                    ");}
  932.             if (PH_calib_timer<1){pointer2=5;readph(); STDread2=phanalog;lcd.clear();
  933.           // calculate PH constant
  934.           PHa=3/(STDread1-STDread2);
  935.           PHb=7-STDread1*PHa;}}
  936.           lastRTCsec=RTCsec;}
  937.          
  938.          else if (pointer2==5){//ph calibration done page 5
  939.           lcd.setCursor(0,0);lcd.print("PH calibration done");
  940.           lcd.setCursor(0,1);lcd.print("press next to exit");
  941.           lcd.setCursor(0,3);lcd.print("PH=");lcd.print(PHa);lcd.print("*A+");lcd.print(PHb);
  942.           if (key==15){pointer2=0; lcd.clear();PH_calib_timer=60;
  943.  
  944.           } // next
  945.           if (key==14){pointer2=3; lcd.clear();PH_calib_timer=60;} // back
  946.          }
  947.          
  948.     if (key==13){pointer1=0; lcd.clear();main_page_data=0;}  // home
  949.     else if (key==12){pointer1=1; lcd.clear();main_page_data=0;}// settings      
  950.    }
  951.    
  952.    else if (pointer1==12){ //other (lock setting)
  953.     lcd.setCursor(0,0);lcd.print("screen lock setting");
  954.     lcd.setCursor(0,1);lcd.print("password    ");
  955.     if (pointer2==0){lcd.print(">");} else {lcd.print(":");}lcd.print(password);lcd.print("   ");
  956.     if (pointer2==0){
  957.      if (key<10){
  958.        if      (pointer3==0){number1=key;password=number1;                                       pointer3=1;}
  959.        else if (pointer3==1){number2=key;password=number1*10+number2;                            pointer3=2;}
  960.        else if (pointer3==2){number3=key;password=number1*100+number2*10+number3;                pointer3=3;}
  961.        else if (pointer3==3){number4=key;password=number1*1000+number2*100+number3*10+number4;   pointer3=0;}}}
  962.     lcd.setCursor(0,2);lcd.print("locking time");
  963.     if (pointer2==1){lcd.print(">");} else {lcd.print(":");} lcd.print(locktime);lcd.print("   ");
  964.     if (pointer2==1){
  965.      if (key<10){
  966.        if      (pointer3==0){number1=key;locktime=number1;                                         pointer3=1;}
  967.        else if (pointer3==1){number2=key;locktime=number1*10+number2;if (locktime==0){locktime=1;} pointer3=0;}}
  968.        }
  969.        
  970.       if (key==13){pointer1=0; lcd.clear();main_page_data=0;save();}  // home
  971.       else if (key==12){pointer1=1; lcd.clear();main_page_data=0;save();}// settings
  972.       else if (key==15 && pointer2<1){pointer2++;pointer3=0;number1=0;number2=0;number3=0;} // next
  973.       else if (key==14 && pointer2>0){pointer2--;pointer3=0;number1=0;number2=0;number3=0;} // Beck
  974.    }
  975.    
  976.    if (pointer1==13){  // lock
  977.       lcd.setCursor(0,0); lcd.print("The screen is locked");
  978.       lcd.setCursor(0,1); lcd.print("Inter the password: ");
  979.       if (key<10){
  980.        if      (pointer2==0){number1=key;interpassword=number1;                                    lcd.setCursor(8,2); lcd.print("*   ");   pointer2=1;}
  981.        else if (pointer2==1){number2=key;interpassword=number1*10+number2;                         lcd.setCursor(8,2); lcd.print("**  ");   pointer2=2;}
  982.        else if (pointer2==2){number3=key;interpassword=number1*100+number2*10+number3;             lcd.setCursor(8,2); lcd.print("*** ");   pointer2=3;}
  983.        else if (pointer2==3){number4=key;interpassword=number1*1000+number2*100+number3*10+number4;lcd.setCursor(8,2); lcd.print("****");
  984.        if ( password==interpassword){locktimer=locktime*60;lcd.clear();pointer1=0;main_page_data=0;} // go home
  985.        else                         {lcd.setCursor(0,3); lcd.print(" incorrect password ");}interpassword=0;                                pointer2=0;}
  986.        }
  987.    }
  988. key=16;  
  989. }
  990. void save(){
  991.   // valve
  992. lcd.setCursor(0,0);lcd.print("   saving data    ");
  993. for (int i=0;i<10;i++){
  994. lcd.setCursor(i,1);lcd.print(".");
  995. EEPROM.write(i,valvedu[i]);EEPROM.commit();
  996. EEPROM.write(i+10,fer1per[i]);EEPROM.commit();
  997. EEPROM.write(i+20,fer2per[i]);EEPROM.commit();
  998. EEPROM.write(i+30,fer3per[i]);EEPROM.commit();
  999. EEPROM.write(i+40,fer4per[i]);EEPROM.commit();
  1000. EEPROM.write(i+50,fer5per[i]);EEPROM.commit();
  1001. EEPROM.write(i+60,valvestatus[i]);EEPROM.commit();}
  1002.   // prog
  1003. for (int i=0;i<25;i++){
  1004. lcd.setCursor(i+10,1);lcd.print(".");
  1005. EEPROM.write(i+75,progact[i]);EEPROM.commit();
  1006. EEPROM.write(i+100, progday1[i]);EEPROM.commit();
  1007. EEPROM.write(i+125, progday2[i]);EEPROM.commit();
  1008. EEPROM.write(i+150, progday3[i]);EEPROM.commit();
  1009. EEPROM.write(i+175, progday4[i]);EEPROM.commit();
  1010. EEPROM.write(i+200, progday5[i]);EEPROM.commit();
  1011. EEPROM.write(i+225, progday6[i]);EEPROM.commit();
  1012. EEPROM.write(i+250, progday7[i]);EEPROM.commit();
  1013. EEPROM.write(i+275, proghour[i]);EEPROM.commit();
  1014. EEPROM.write(i+300, progmin[i]);EEPROM.commit();
  1015. EEPROM.write(i+325, progvalve1[i]);EEPROM.commit();
  1016. EEPROM.write(i+350, progvalve2[i]);EEPROM.commit();
  1017. EEPROM.write(i+375, progvalve3[i]);EEPROM.commit();
  1018. EEPROM.write(i+400, progvalve4[i]);EEPROM.commit();
  1019. EEPROM.write(i+425, progvalve5[i]);EEPROM.commit();
  1020. EEPROM.write(i+450, progvalve6[i]);EEPROM.commit();
  1021. EEPROM.write(i+475, progvalve7[i]);EEPROM.commit();
  1022. EEPROM.write(i+500, progvalve8[i]);EEPROM.commit();
  1023. EEPROM.write(i+525, progvalve9[i]);EEPROM.commit();
  1024. EEPROM.write(i+550, progvalve10[i]);EEPROM.commit();}
  1025. //PH EC
  1026. for (int i=0;i<10;i++){
  1027. EEPROM.write(i+600,valvePH[i]);EEPROM.commit();
  1028. EEPROM.write(i+640,valveCE[i]);EEPROM.commit();}
  1029. // password
  1030. EEPROM.put(668,password);EEPROM.commit();
  1031. EEPROM.write(672,locktime);EEPROM.commit();
  1032. // ph calibration
  1033. EEPROM.put(673,PHa);EEPROM.commit(); // a
  1034. EEPROM.put(677,PHb);EEPROM.commit(); // b
  1035. lcd.clear();
  1036. }
  1037. void load(){
  1038.    // valve
  1039.  lcd.setCursor(0,0);lcd.print("   loading data    ");
  1040.  for (int i=0;i<10;i++) {
  1041.  lcd.setCursor(i,1);lcd.print(".");
  1042.  valvedu[i]=EEPROM.read(i);
  1043.  fer1per[i]=EEPROM.read(i+10);
  1044.  fer2per[i]=EEPROM.read(i+20);
  1045.  fer3per[i]=EEPROM.read(i+30);
  1046.  fer4per[i]=EEPROM.read(i+40);
  1047.  fer5per[i]=EEPROM.read(i+50);
  1048.  valvestatus[i]=EEPROM.read(i+60);}
  1049.   // prog
  1050.  for (int i=0;i<25;i++) {
  1051.  lcd.setCursor(i+10,1);lcd.print(".");
  1052.  progact[i]=EEPROM.read(i+75);
  1053.  progday1[i]=EEPROM.read(i+100);
  1054.  progday2[i]=EEPROM.read(i+125);
  1055.  progday3[i]=EEPROM.read(i+150);
  1056.  progday4[i]=EEPROM.read(i+175);
  1057.  progday5[i]=EEPROM.read(i+200);
  1058.  progday6[i]=EEPROM.read(i+225);
  1059.  progday7[i]=EEPROM.read(i+250);
  1060.  proghour[i]=EEPROM.read(i+275);
  1061.  progmin[i]=EEPROM.read(i+300);
  1062.  progvalve1[i]=EEPROM.read(i+325);
  1063.  progvalve2[i]=EEPROM.read(i+350);
  1064.  progvalve3[i]=EEPROM.read(i+375);
  1065.  progvalve4[i]=EEPROM.read(i+400);
  1066.  progvalve5[i]=EEPROM.read(i+425);
  1067.  progvalve6[i]=EEPROM.read(i+450);
  1068.  progvalve7[i]=EEPROM.read(i+475);
  1069.  progvalve8[i]=EEPROM.read(i+500);
  1070.  progvalve9[i]=EEPROM.read(i+525);
  1071.  progvalve10[i]=EEPROM.read(i+550);}
  1072.  //PH EC
  1073.  for (int i=0;i<10;i++){
  1074.   valvePH[i]=EEPROM.read(i+600);
  1075.   valveCE[i]=EEPROM.read(i+640);}
  1076.    // password
  1077.    EEPROM.get(668,password);
  1078.    locktime=EEPROM.read(672);
  1079.    // ph calibration
  1080.    EEPROM.get(673,PHa);EEPROM.commit(); // a
  1081.    EEPROM.get(677,PHb);EEPROM.commit(); // b
  1082.  lcd.clear();
  1083. }
  1084. void valvereset(){
  1085.  // valves data
  1086.  for (int i=0;i<10;i++){
  1087.  valvedu[i]=0; valvePH[i]=0; valveCE[i]=0;fer1per[i]=0;
  1088.  fer2per[i]=0; fer3per[i]=0;fer4per[i]=0; fer5per[i]=0;}
  1089.  save();
  1090. }
  1091. void progreset(){
  1092.  // program data
  1093.  for (int i=0;i<25;i++){
  1094.  progact[i]=0;progday1[i]=0;progday2[i]=0;progday3[i]=0;
  1095.  progday4[i]=0;progday5[i]=0;progday6[i]=0;progday7[i]=0;
  1096.  proghour[i]=0;progmin[i]=0;progvalve1[i]=0;progvalve2[i]=0;
  1097.  progvalve3[i]=0;progvalve4[i]=0;progvalve5[i]=0;progvalve6[i]=0;
  1098.  progvalve7[i]=0;progvalve8[i]=0;progvalve9[i]=0;progvalve10[i]=0;}
  1099. save();
  1100. }
  1101. void checkprog(){
  1102.   valverun=0;runprog=0;
  1103.   // read time
  1104.     Time nowTime = pcf.getTime();//get current time
  1105.   // check active prog
  1106.   for (int i=0;i<25;i++){ // i:prog num
  1107.     if (progact[i]==1){
  1108.       // check the day
  1109.       if (day==0){// sunday
  1110.         // ckeck if the day active and check hour and min
  1111.           if (progday1[i]==1 && proghour[i]==nowTime.hour && progmin[i]==nowTime.minute){runprog=i+1;}}
  1112.       else if (day==1){//monday
  1113.         // ckeck if the day active and check hour and min
  1114.           if (progday2[i]==1 && proghour[i]==nowTime.hour && progmin[i]==nowTime.minute){runprog=i+1;}}
  1115.       else if (day==2){//tuesday
  1116.          // ckeck if the day active and check hour and min
  1117.           if (progday3[i]==1 && proghour[i]==nowTime.hour && progmin[i]==nowTime.minute){runprog=i+1;}}
  1118.       else if (day==3){//wednesday
  1119.         // ckeck if the day active and check hour and min
  1120.           if (progday4[i]==1 && proghour[i]==nowTime.hour && progmin[i]==nowTime.minute){runprog=i+1;}}
  1121.       else if (day==4){//theresday
  1122.          // ckeck if the day active and check hour and min
  1123.           if (progday5[i]==1 && proghour[i]==nowTime.hour && progmin[i]==nowTime.minute){runprog=i+1;}}
  1124.       else if (day==5){//friday
  1125.         // ckeck if the day active and check hour and min
  1126.           if (progday6[i]==1 && proghour[i]==nowTime.hour && progmin[i]==nowTime.minute){runprog=i+1;}}
  1127.       else if (day==6){//saturday
  1128.         // ckeck if the day active and check hour and min
  1129.           if (progday7[i]==1 && proghour[i]==nowTime.hour && progmin[i]==nowTime.minute){runprog=i+1;}}
  1130.     }
  1131.   }
  1132. valverun=0;
  1133. // set prog timers
  1134. if (progvalve1[runprog-1]==1){valvetimer[0]=valvedu[0]+1;}  else {valvetimer[0]=0;}
  1135. if (progvalve2[runprog-1]==1){valvetimer[1]=valvedu[1]+1;}  else {valvetimer[1]=0;}
  1136. if (progvalve3[runprog-1]==1){valvetimer[2]=valvedu[2]+1;}  else {valvetimer[2]=0;}
  1137. if (progvalve4[runprog-1]==1){valvetimer[3]=valvedu[3]+1;}  else {valvetimer[3]=0;}
  1138. if (progvalve5[runprog-1]==1){valvetimer[4]=valvedu[4]+1;}  else {valvetimer[4]=0;}
  1139. if (progvalve6[runprog-1]==1){valvetimer[5]=valvedu[5]+1;}  else {valvetimer[5]=0;}
  1140. if (progvalve7[runprog-1]==1){valvetimer[6]=valvedu[6]+1;}  else {valvetimer[6]=0;}
  1141. if (progvalve8[runprog-1]==1){valvetimer[7]=valvedu[7]+1;}  else {valvetimer[7]=0;}
  1142. if (progvalve9[runprog-1]==1){valvetimer[8]=valvedu[8]+1;}  else {valvetimer[8]=0;}
  1143. if (progvalve10[runprog-1]==1){valvetimer[9]=valvedu[9]+1;}  else {valvetimer[9]=0;}
  1144. if (pointer1!=8){stopall();}//check valve
  1145.  }
  1146.  
  1147. void runprogfun(){
  1148.   // timer
  1149.   Time nowTime = pcf.getTime();//get current time
  1150.   // min
  1151.   RTCmin=nowTime.minute;
  1152.   if (!(lastRTCmin==RTCmin)){
  1153.     // main pump on
  1154.     bitWrite(dataout3,0,0);shiftout();
  1155.    
  1156.     // valves
  1157.     int totaltime=0;
  1158.     if  (valvetimer[0]>0)                {valvestatus[0]=1; bitWrite(dataout1,1,!valvestatus[0]);shiftout();valvetimer[0]--;valverun=1;}
  1159.     totaltime=totaltime+valvetimer[0];
  1160.     if  (valvetimer[1]>0 && totaltime==0){valvestatus[1]=1; bitWrite(dataout1,0,!valvestatus[1]);shiftout();valvetimer[1]--;valverun=2;}
  1161.     totaltime=totaltime+valvetimer[1];
  1162.     if  (valvetimer[2]>0 && totaltime==0){valvestatus[2]=1; bitWrite(dataout2,7,!valvestatus[2]);shiftout();valvetimer[2]--;valverun=3;}
  1163.     totaltime=totaltime+valvetimer[2];
  1164.     if  (valvetimer[3]>0 && totaltime==0){valvestatus[3]=1; bitWrite(dataout2,6,!valvestatus[3]);shiftout();valvetimer[3]--;valverun=4;}
  1165.     totaltime=totaltime+valvetimer[3];
  1166.     if  (valvetimer[4]>0 && totaltime==0){valvestatus[4]=1; bitWrite(dataout2,5,!valvestatus[4]);shiftout();valvetimer[4]--;valverun=5;}
  1167.     totaltime=totaltime+valvetimer[4];
  1168.     if  (valvetimer[5]>0 && totaltime==0){valvestatus[5]=1; bitWrite(dataout2,4,!valvestatus[5]);shiftout();valvetimer[5]--;valverun=6;}
  1169.     totaltime=totaltime+valvetimer[5];
  1170.     if  (valvetimer[6]>0 && totaltime==0){valvestatus[6]=1; bitWrite(dataout2,3,!valvestatus[6]);shiftout();valvetimer[6]--;valverun=7;}
  1171.     totaltime=totaltime+valvetimer[6];
  1172.     if  (valvetimer[7]>0 && totaltime==0){valvestatus[7]=1; bitWrite(dataout2,2,!valvestatus[7]);shiftout();valvetimer[7]--;valverun=8;}
  1173.     totaltime=totaltime+valvetimer[7];
  1174.     if  (valvetimer[8]>0 && totaltime==0){valvestatus[8]=1; bitWrite(dataout2,1,!valvestatus[8]);shiftout();valvetimer[8]--;valverun=9;}
  1175.     totaltime=totaltime+valvetimer[8];
  1176.     if  (valvetimer[9]>0 && totaltime==0){valvestatus[9]=1; bitWrite(dataout2,0,!valvestatus[9]);shiftout();valvetimer[9]--;valverun=10;}
  1177.     if (valvetimer[0]==0){valvestatus[0]=0; bitWrite(dataout1,1,!valvestatus[0]);shiftout();
  1178.     if (valvetimer[1]==0){valvestatus[1]=0; bitWrite(dataout1,0,!valvestatus[1]);shiftout();
  1179.     if (valvetimer[2]==0){valvestatus[2]=0; bitWrite(dataout2,7,!valvestatus[2]);shiftout();
  1180.     if (valvetimer[3]==0){valvestatus[3]=0; bitWrite(dataout2,6,!valvestatus[3]);shiftout();
  1181.     if (valvetimer[4]==0){valvestatus[4]=0; bitWrite(dataout2,5,!valvestatus[4]);shiftout();
  1182.     if (valvetimer[5]==0){valvestatus[5]=0; bitWrite(dataout2,4,!valvestatus[5]);shiftout();
  1183.     if (valvetimer[6]==0){valvestatus[6]=0; bitWrite(dataout2,3,!valvestatus[6]);shiftout();
  1184.     if (valvetimer[7]==0){valvestatus[7]=0; bitWrite(dataout2,2,!valvestatus[7]);shiftout();
  1185.     if (valvetimer[8]==0){valvestatus[8]=0; bitWrite(dataout2,1,!valvestatus[8]);shiftout();
  1186.     if (valvetimer[9]==0){valvestatus[9]=0; bitWrite(dataout2,0,!valvestatus[9]);shiftout();
  1187.     lastrunprog=runprog;valverun=0;runprog=0;testprog=0;}}}}}}}}}}}
  1188.     lastRTCmin=RTCmin;
  1189.    
  1190.     // fertilizers
  1191.     for (int i=0; i<10 ;i++){
  1192.       if (valvestatus[i]==1){
  1193.     unsigned long currentmillis=millis();
  1194.     if      (on_off_timer==0){
  1195.               if (fer1per[valverun-1]>on_timer){bitWrite(dataout1,7,0);}
  1196.               else                             {bitWrite(dataout1,7,1);}
  1197.               if (fer2per[valverun-1]>on_timer){bitWrite(dataout1,6,0);}
  1198.               else                             {bitWrite(dataout1,6,1);}
  1199.               if (fer3per[valverun-1]>on_timer){bitWrite(dataout1,5,0);}
  1200.               else                             {bitWrite(dataout1,5,1);}
  1201.               if (fer4per[valverun-1]>on_timer){bitWrite(dataout1,4,0);}
  1202.               else                             {bitWrite(dataout1,4,1);}
  1203.               if (fer5per[valverun-1]>on_timer){bitWrite(dataout1,3,0);}
  1204.               else                             {bitWrite(dataout1,3,1);}
  1205.             if ((unsigned long)(currentmillis-lastmillis)>=50){
  1206.               on_timer++;if (on_timer>100){on_timer=0;on_off_timer=1;}
  1207.               lastmillis=currentmillis;}
  1208.               shiftout();}
  1209.     else if (on_off_timer==1){
  1210.             bitWrite(dataout1,7,1);bitWrite(dataout1,5,1);bitWrite(dataout1,4,1);bitWrite(dataout1,3,1);bitWrite(dataout1,6,1);shiftout();
  1211.             if ((unsigned long)(currentmillis-lastmillis)>=50){
  1212.               off_timer++;if (off_timer>100){off_timer=0;on_off_timer=0;}
  1213.               lastmillis=currentmillis;}}i=10;}}
  1214.  
  1215.    // PID CONTROL PH
  1216.  
  1217.  
  1218.    unsigned long currentmillisPH=millis();
  1219.       if ((unsigned long)(currentmillisPH-lastmillisPH)>=10){PIDtimer++; lastmillisPH=currentmillisPH;}
  1220.       if (PIDtimer>100){PIDtimer=0;
  1221.        // ph controller
  1222.        error = valvePH[valverun-1]-ph;
  1223.        errorsum=errorsum+error;
  1224.        PIDontime=ph*(-14.29)+128.6-(PHp*error+PHi*errorsum+PHd*(error-lasterror));
  1225.        lasterror=error;
  1226.       Serial.print("error sum :");Serial.print(errorsum);Serial.print("error :");Serial.print(error);Serial.print("ph on time :");Serial.println(PIDontime);}
  1227.       else if (PIDtimer>PIDontime) {bitWrite(dataout1,2,1);shiftout();}//off
  1228.       else if (PIDtimer<PIDontime) {bitWrite(dataout1,2,0);shiftout();}//on
  1229.        
  1230. }
  1231.  
  1232. void shiftout(){
  1233.      digitalWrite(latch,0);
  1234.     shiftOut(data,clk,MSBFIRST,dataout3);
  1235.     shiftOut(data,clk,MSBFIRST,dataout2);
  1236.     shiftOut(data,clk,MSBFIRST,dataout1);
  1237.     digitalWrite(latch,1);}
  1238.    
  1239. void saverunprogdata(){
  1240. Time nowTime = pcf.getTime();//get current time
  1241. progyear=nowTime.year;
  1242. progmonth=nowTime.month;
  1243. progday=nowTime.day;
  1244. progh=nowTime.hour;
  1245. progm=nowTime.minute;
  1246. for (int i=0;i<10;i++) {EEPROM.write(650+i,valvetimer[i]);EEPROM.commit();}
  1247. EEPROM.write(661,runprog);EEPROM.commit();
  1248. EEPROM.write(662,progyear);EEPROM.commit();
  1249. EEPROM.write(663,progmonth);EEPROM.commit();
  1250. EEPROM.write(664,progday);EEPROM.commit();
  1251. EEPROM.write(665,progh);EEPROM.commit();
  1252. EEPROM.write(666,progm);EEPROM.commit();
  1253.  // 667 for day
  1254.  
  1255. }
  1256.  
  1257. void loadrunprogdata(){
  1258.  lcd.setCursor(0,0);lcd.print("   loading data    ");
  1259.  for (int i=0;i<10;i++){valvetimer[i]= EEPROM.read(650+i);}
  1260.  runprog=EEPROM.read(661);
  1261.  progyear=EEPROM.read(662);
  1262.  progmonth=EEPROM.read(663);
  1263.  progday=EEPROM.read(664);
  1264.  progh=EEPROM.read(665);
  1265.  progm=EEPROM.read(666);
  1266.  // 667 for day
  1267.  Serial.print("runprog:"); Serial.print(runprog);
  1268.  Serial.print("date"); Serial.print(progday);Serial.print("/");
  1269.  Serial.print(progmonth);Serial.print("/");
  1270.  Serial.print(progyear);Serial.print("/");
  1271.  Serial.print("hour:"); Serial.print(progh);
  1272.  Serial.print("min"); Serial.print(progm);
  1273.  Serial.print("valve1du");Serial.println(valvetimer[0]);
  1274.  lcd.clear();}
  1275.  
  1276. void check_off_timer(){
  1277.     Time nowTime = pcf.getTime();//get current time
  1278.   if (progyear==nowTime.year &&  progmonth==nowTime.month && progday==nowTime.day){
  1279.     byte timediff=(nowTime.hour*60+nowTime.minute)-(progh*60+progm);
  1280.     if (timediff<180){fastmode=1;}
  1281.     else {fastmode=0;}
  1282.   Serial.println(timediff);}}
  1283.  
  1284. void fastcheck(){
  1285. valverun=0;runprog=0;
  1286.   // check active prog
  1287.   for (int i=0;i<25;i++){ // i:prog num
  1288.     if (progact[i]==1){
  1289.       // check the day
  1290.       if (day==0){// sunday
  1291.         // ckeck if the day active and check hour and min
  1292.           if (progday1[i]==1 && proghour[i]==progh && progmin[i]==progm){runprog=i+1;}}
  1293.       else if (day==1){//monday
  1294.         // ckeck if the day active and check hour and min
  1295.           if (progday2[i]==1 && proghour[i]==progh && progmin[i]==progm){runprog=i+1;}}
  1296.       else if (day==2){//tuesday
  1297.          // ckeck if the day active and check hour and min
  1298.           if (progday3[i]==1 && proghour[i]==progh && progmin[i]==progm){runprog=i+1;}}
  1299.       else if (day==3){//wednesday
  1300.         // ckeck if the day active and check hour and min
  1301.           if (progday4[i]==1 && proghour[i]==progh && progmin[i]==progm){runprog=i+1;}}
  1302.       else if (day==4){//theresday
  1303.          // ckeck if the day active and check hour and min
  1304.           if (progday5[i]==1 && proghour[i]==progh && progmin[i]==progm){runprog=i+1;}}
  1305.       else if (day==5){//friday
  1306.         // ckeck if the day active and check hour and min
  1307.           if (progday6[i]==1 && proghour[i]==progh && progmin[i]==progm){runprog=i+1;}}
  1308.       else if (day==6){//saturday
  1309.         // ckeck if the day active and check hour and min
  1310.           if (progday7[i]==1 && proghour[i]==progh && progmin[i]==progm){runprog=i+1;}}
  1311.     }
  1312.   }
  1313. valverun=0;
  1314. if (runprog==lastrunprog){runprog=0;} // not repeat prog
  1315. // set prog timer
  1316. if (runprog!=0){
  1317. if (progvalve1[runprog-1]==1){valvetimer[0]=valvedu[0]+1;}  else {valvetimer[0]=0;}
  1318. if (progvalve2[runprog-1]==1){valvetimer[1]=valvedu[1]+1;}  else {valvetimer[1]=0;}
  1319. if (progvalve3[runprog-1]==1){valvetimer[2]=valvedu[2]+1;}  else {valvetimer[2]=0;}
  1320. if (progvalve4[runprog-1]==1){valvetimer[3]=valvedu[3]+1;}  else {valvetimer[3]=0;}
  1321. if (progvalve5[runprog-1]==1){valvetimer[4]=valvedu[4]+1;}  else {valvetimer[4]=0;}
  1322. if (progvalve6[runprog-1]==1){valvetimer[5]=valvedu[5]+1;}  else {valvetimer[5]=0;}
  1323. if (progvalve7[runprog-1]==1){valvetimer[6]=valvedu[6]+1;}  else {valvetimer[6]=0;}
  1324. if (progvalve8[runprog-1]==1){valvetimer[7]=valvedu[7]+1;}  else {valvetimer[7]=0;}
  1325. if (progvalve9[runprog-1]==1){valvetimer[8]=valvedu[8]+1;}  else {valvetimer[8]=0;}
  1326. if (progvalve10[runprog-1]==1){valvetimer[9]=valvedu[9]+1;}  else {valvetimer[9]=0;} }
  1327. progm++;if (progm>60){progm=0;progh++;}
  1328. check_off_timer(); if (pointer1!=8){stopall();}//check valve
  1329. }
  1330.  
  1331. void stopprog(){
  1332. progsta=1;
  1333. //stop all valve
  1334.     valvestatus[0]=0; bitWrite(dataout1,1,!valvestatus[0]);
  1335.     valvestatus[1]=0; bitWrite(dataout1,0,!valvestatus[1]);
  1336.     valvestatus[2]=0; bitWrite(dataout2,7,!valvestatus[2]);
  1337.     valvestatus[3]=0; bitWrite(dataout2,6,!valvestatus[3]);
  1338.     valvestatus[4]=0; bitWrite(dataout2,5,!valvestatus[4]);
  1339.     valvestatus[5]=0; bitWrite(dataout2,4,!valvestatus[5]);
  1340.     valvestatus[6]=0; bitWrite(dataout2,3,!valvestatus[6]);
  1341.     valvestatus[7]=0; bitWrite(dataout2,2,!valvestatus[7]);
  1342.     valvestatus[8]=0; bitWrite(dataout2,1,!valvestatus[8]);
  1343.     valvestatus[9]=0; bitWrite(dataout2,0,!valvestatus[9]);
  1344. //stop all fertilizers
  1345.    bitWrite(dataout1,7,1);
  1346.    bitWrite(dataout1,6,1);
  1347.    bitWrite(dataout1,5,1);
  1348.    bitWrite(dataout1,4,1);
  1349.    bitWrite(dataout1,3,1);
  1350.    shiftout();
  1351.    saverunprogdata();
  1352.    }
  1353.    
  1354. void continueprog(){
  1355. loadrunprogdata(); check_off_timer(); progsta=0;
  1356. }
  1357. void saveday (){
  1358. EEPROM.write(667,day);EEPROM.commit();
  1359. }
  1360. void loadday (){
  1361. day=EEPROM.read(667);
  1362. }
  1363. void stopall(){
  1364. //stop valve
  1365.     valvestatus[0]=0; bitWrite(dataout1,1,!valvestatus[0]);
  1366.     valvestatus[1]=0; bitWrite(dataout1,0,!valvestatus[1]);
  1367.     valvestatus[2]=0; bitWrite(dataout2,7,!valvestatus[2]);
  1368.     valvestatus[3]=0; bitWrite(dataout2,6,!valvestatus[3]);
  1369.     valvestatus[4]=0; bitWrite(dataout2,5,!valvestatus[4]);
  1370.     valvestatus[5]=0; bitWrite(dataout2,4,!valvestatus[5]);
  1371.     valvestatus[6]=0; bitWrite(dataout2,3,!valvestatus[6]);
  1372.     valvestatus[7]=0; bitWrite(dataout2,2,!valvestatus[7]);
  1373.     valvestatus[8]=0; bitWrite(dataout2,1,!valvestatus[8]);
  1374.     valvestatus[9]=0; bitWrite(dataout2,0,!valvestatus[9]);
  1375. //stop all fertilizers
  1376.    bitWrite(dataout1,7,1);
  1377.    bitWrite(dataout1,6,1);
  1378.    bitWrite(dataout1,5,1);
  1379.    bitWrite(dataout1,4,1);
  1380.    bitWrite(dataout1,3,1);
  1381. // stop pump
  1382. bitWrite(dataout3,0,1);
  1383.    shiftout();
  1384. }
  1385. void setvalvetime(){
  1386. if (progvalve1[runprog-1]==1){valvetimer[0]=valvedu[0]+1;}  else {valvetimer[0]=0;}
  1387. if (progvalve2[runprog-1]==1){valvetimer[1]=valvedu[1]+1;}  else {valvetimer[1]=0;}
  1388. if (progvalve3[runprog-1]==1){valvetimer[2]=valvedu[2]+1;}  else {valvetimer[2]=0;}
  1389. if (progvalve4[runprog-1]==1){valvetimer[3]=valvedu[3]+1;}  else {valvetimer[3]=0;}
  1390. if (progvalve5[runprog-1]==1){valvetimer[4]=valvedu[4]+1;}  else {valvetimer[4]=0;}
  1391. if (progvalve6[runprog-1]==1){valvetimer[5]=valvedu[5]+1;}  else {valvetimer[5]=0;}
  1392. if (progvalve7[runprog-1]==1){valvetimer[6]=valvedu[6]+1;}  else {valvetimer[6]=0;}
  1393. if (progvalve8[runprog-1]==1){valvetimer[7]=valvedu[7]+1;}  else {valvetimer[7]=0;}
  1394. if (progvalve9[runprog-1]==1){valvetimer[8]=valvedu[8]+1;}  else {valvetimer[8]=0;}
  1395. if (progvalve10[runprog-1]==1){valvetimer[9]=valvedu[9]+1;}  else {valvetimer[9]=0;}
  1396. }
  1397. void readph(){
  1398. bitWrite(dataout3,4,1);   shiftout();  // enable ph sensor analog
  1399. phanalog=analogRead(A0);               // read analog
  1400. bitWrite(dataout3,4,0);   shiftout();  // desable ph sensor analog
  1401. ph2=ph1;ph3=ph2;ph1=phanalog*-0.02778+22.75;
  1402. if ((ph1-ph2)<0.2 && (ph2-ph1)<0.2 && (ph1-ph3)<0.2 && (ph3-ph1)<0.2 && (ph2-ph3)<0.2 && (ph3-ph2)<0.2 )
  1403. {ph=(ph1+ph2+ph3)/3;}
  1404. }
  1405. void checkfilter(){
  1406.    // reading serial
  1407.   if(Serial.available()>0){
  1408.    x=Serial.read();
  1409.    if (x=='f' && bitRead(dataout3,0)==0){filterstatus=1;filtercounter=0;}}
  1410. }
  1411. void runfilter(){
  1412.     if      (filtercounter==0){filterdelay=5000;bitWrite(dataout3,3,1);bitWrite(dataout3,2,1);bitWrite(dataout3,1,1);shiftout();}//delay  
  1413.     else if (filtercounter==1){filterdelay=20000;bitWrite(dataout3,3,0);bitWrite(dataout3,2,1);bitWrite(dataout3,1,1);shiftout();}//start filter 1
  1414.     else if (filtercounter==2){filterdelay=5000;bitWrite(dataout3,3,1);bitWrite(dataout3,2,1);bitWrite(dataout3,1,1);shiftout();}//delay
  1415.     else if (filtercounter==3){filterdelay=20000;bitWrite(dataout3,3,1);bitWrite(dataout3,2,0);bitWrite(dataout3,1,1);shiftout();}//start filter 2
  1416.     else if (filtercounter==4){filterdelay=5000;bitWrite(dataout3,3,1);bitWrite(dataout3,2,1);bitWrite(dataout3,1,1);shiftout();}//delay
  1417.     else if (filtercounter==5){filterdelay=20000;bitWrite(dataout3,3,1);bitWrite(dataout3,2,1);bitWrite(dataout3,1,0);shiftout();}//start filter 3
  1418.     else if (filtercounter==6){filterdelay=5000;bitWrite(dataout3,3,1);bitWrite(dataout3,2,1);bitWrite(dataout3,1,1);shiftout();}//delay
  1419.     else if (filtercounter==7){filtercounter=0 ;bitWrite(dataout3,3,1);bitWrite(dataout3,2,1);bitWrite(dataout3,1,1);shiftout();filterstatus=0;}//stop
  1420.    
  1421.     unsigned long currentmillis=millis();
  1422.     if ((unsigned long)(currentmillis-lastmillisf)>=filterdelay){filtercounter++;lastmillisf=currentmillis;}  
  1423.    
  1424. }
RAW Paste Data