munther_abdellatif

start_pause_20_06_2020

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