munther_abdellatif

our project

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