munther_abdellatif

screen lock and PH calibration

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