munther_abdellatif

password_pump_filter26_06_2020

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