munther_abdellatif

do_not_comp_prog_19_06_2020

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