// #include <LiquidCrystal.h>
// #define en 8
// #define rs 9
// #define d4 10
// #define d5 11
// #define d6 12
// #define d7 13
// #define FAN 3
// #define COOLER 2
// #define PUMP 5
// // #define alarm 4
// #define UP_butt A1
// #define DOWN_butt A2
// #define Set_butt A3
// #define buzzer 6
// LiquidCrystal lcd( rs, en ,d4, d5, d6, d7);
// bool up;
// bool down ;
// bool start_timer =false;
// bool set_Hours= false;
// bool set_minute= false;
// bool runn = false;
// bool set_H = false;
// bool set_M = false;
// String time_stat = " ";
// int HT = 0 ;
// int MT = 0;
// int ST = 0;
// byte flag_page ;
// unsigned long prev_up;
// unsigned long prev_dw;
// unsigned long prev = 0 ;
// unsigned long previ =0;
// unsigned long prevtime;
// static unsigned long prevtimebuzzer = 0;
// byte bazzcnt= 0;
// bool bazzend = false ;
// int buz_state = LOW;
// bool swipress[2] = {LOW , LOW };
// byte lastButton[2] = {0 , 0};
// unsigned long lasbutStat[2] = {0 ,0 };
// unsigned long prevtim[2] = {0 ,0 } ;
// bool swipress_1 = false;
// byte lastButton_1;
// unsigned long lasbutStat_1 = 0;
// unsigned long lasbutStat_h = 0;
// bool same_state = false;
// bool same_state_1 = false;
// byte selector ;
// bool selected ;
// bool flage_t_co = false ;
// bool flage_t_fa = false ;
// bool flage_t_pu = false ;
// bool flg_cooler = false ;
// // ///////////SETUP///////////////////////////
// void setup(){
// lcd.begin(20,4);
// Serial.begin(9600);
// pinMode(UP_butt,INPUT_PULLUP);
// pinMode(DOWN_butt,INPUT_PULLUP);
// pinMode(Set_butt,INPUT_PULLUP);
// pinMode(COOLER,OUTPUT);
// }
// // ///////////////////LOOP//////////////////////////
// void loop(){
// debounce_1(UP_butt,0);
// debounce_1(DOWN_butt,1);
// up_down();
// Debounce() ;
// if(flag_page ){
// // press_page();
// // variable();
// // if( flg_cooler ){
// timerr();
// // flage_t_pu = false;
// // swipress_1 = false;
// // }
// }
// if(!flag_page){
// if (start_timer){
// countdown();
// }
// }
// if(swipress_1 && runn == true && !flag_page ){
// Serial.println("cut");
// start_timer = false;
// ST = 0;
// set_minute = false ;
// set_Hours = false;
// runn = false;
// bazzend = false;
// bazzcnt= 0 ;
// time_stat = "OFF";
// digitalWrite(COOLER, LOW);
// swipress_1 =false;
// }
// if(millis() - prev >50){
// Display();
// prev= millis();
// }
// }
// // /////////////////////////////////////////////
// #define same_place 100
// // ///////// func for display //////////////////
// void lcd_print( String Value, int row , int col ) {
// if(row != same_place || col != same_place) lcd.setCursor(col, row);
// lcd.print(Value);
// }
// // /////////////// LCD Display //////////////////
// void Display(){
// if(!flag_page ){
// // if (selector == 1) {
// // lcd_print(">", 0, 0);
// // lcd_print(" ", 1, 0);
// // lcd_print(" ", 2, 0);
// // }
// lcd_print("CO: ", 0, 0);
// lcd_print(time_stat , 0 ,7);
// lcd.print(" ");
// if(HT<10) lcd.print("0");
// lcd.print(HT ,DEC);
// lcd.print(":");
// if(MT <10) lcd.print("0");
// lcd.print(MT, DEC);
// lcd.print(":");
// if(ST <10 ) lcd.print("0");
// lcd.print(ST,DEC);
// // lcd.print(" ");
// // if(H[0] <10) lcd.print("0");
// // lcd.print(H[0] ,DEC);
// // lcd.print(":");
// // if(M[0] <10) lcd.print("0");
// // lcd.print(M[0], DEC);
// // lcd.print(":");
// // if(S[0] <10 ) lcd.print("0");
// // lcd.print(S[0],DEC);
// // if (selector == 2) {
// // lcd_print(" ", 0, 0);
// // lcd_print(">", 1, 0);
// // lcd_print(" ", 2, 0);
// // }
// lcd_print("Fa: ", 1,0);
// // lcd.print(" ");
// // if(H[1] <10) lcd.print("0");
// // lcd.print(H[1] ,DEC);
// // lcd.print(":");
// // if(M[1] <10) lcd.print("0");
// // lcd.print(M[1], DEC);
// // lcd.print(":");
// // if(S[1] <10 ) lcd.print("0");
// // lcd.print(S[1],DEC);
// // // lcd_print(led, 2, 4);
// // if (selector == 3) {
// // lcd_print(" ", 0, 0);
// // lcd_print(" ", 1, 0);
// // lcd_print(">", 2, 0);
// // }
// lcd_print("Pu: ", 2,0);
// // lcd.print(" ");
// // if(H[2] <10) lcd.print("0");
// // lcd.print(H[2] ,DEC);
// // lcd.print(":");
// // if(M[2] <10) lcd.print("0");
// // lcd.print(M[2], DEC);
// // lcd.print(":");
// // if(S[2] <10 ) lcd.print("0");
// // lcd.print(S[2],DEC);
// // lcd.print(" ");
// }
// if (flag_page ){
// if (selector == 1) {
// lcd_print(">", 0, 0);
// lcd_print(" ", 1, 0);
// lcd_print(" ", 2, 0);
// }
// lcd_print("T_co: ", 0,1);
// lcd_print(time_stat , 0 ,7);
// lcd.print(" ");
// if(HT<10) lcd.print("0");
// lcd.print(HT ,DEC);
// lcd.print(":");
// if(MT <10) lcd.print("0");
// lcd.print(MT, DEC);
// lcd.print(":");
// if(ST <10 ) lcd.print("0");
// lcd.print(ST,DEC);
// if (selector == 2) {
// lcd_print(" ", 0, 0);
// lcd_print(">", 1, 0);
// lcd_print(" ", 2, 0);
// }
// lcd_print("T_fa: ", 1,1);
// lcd_print(time_stat , 1 ,7);
// lcd.print(" ");
// if(HT<10) lcd.print("0");
// lcd.print(HT ,DEC);
// lcd.print(":");
// if(MT <10) lcd.print("0");
// lcd.print(MT, DEC);
// lcd.print(":");
// if(ST <10 ) lcd.print("0");
// lcd.print(ST,DEC);
// if (selector == 3) {
// lcd_print(" ", 0, 0);
// lcd_print(" ", 1, 0);
// lcd_print(">", 2, 0);
// }
// lcd_print("T_pu: ", 2,1);
// lcd_print(time_stat , 2 ,7);
// lcd.print(" ");
// if(HT<10) lcd.print("0");
// lcd.print(HT ,DEC);
// lcd.print(":");
// if(MT <10) lcd.print("0");
// lcd.print(MT, DEC);
// lcd.print(":");
// if(ST <10 ) lcd.print("0");
// lcd.print(ST,DEC);
// }
// }
// // ///////////////// Debounce //////////////////////////
// void Debounce() {
// if (millis() - lasbutStat_1 >= 50) {
// byte buttonState_1 = digitalRead(Set_butt);
// if (buttonState_1 == LOW && lastButton_1 == HIGH) { // rising
// swipress_1 = true;
// lasbutStat_h = millis();
// } else if (buttonState_1 == LOW && lastButton_1 == LOW) { // same
// if (millis() - lasbutStat_h >= 1000 && same_state_1 == false) {
// swipress_1 = false;
// // set1 = false;
// // set2 = false;
// // set3 = false;
// // set4 = false;
// // set5 = false;
// // set11 = false;
// // set21 = false;
// // runn = false;
// set_Hours = false;
// set_minute = false;
// selected = false;
// selector = false;
// flag_page = !flag_page;
// same_state_1 = true;
// lcd.clear();
// }
// } else if (buttonState_1 == HIGH && lastButton_1 == LOW) { // failling
// same_state_1 = false;
// }
// lasbutStat_1 = millis();
// lastButton_1 = buttonState_1;
// }
// }
// // //////////// Debounce /////////////////
// void debounce_1(int button, int index){
// if (millis() - lasbutStat[index] >= 50){
// byte buttonState = digitalRead(button);
// if(buttonState == LOW && lastButton[index] == HIGH){ //rising
// swipress[index] = true;
// previ = millis();
// }
// else if(buttonState == LOW && lastButton[index] == LOW && same_state == false ){ //same
// swipress[index] = false;
// if(millis()-previ >1000){
// same_state = true;
// }
// }
// else if (buttonState == HIGH && lastButton[index] == LOW ){ // failling
// same_state = false;
// }
// lasbutStat[index] = millis();
// lastButton[index] = buttonState;
// }
// }
// // ////////////////up&down//////////
// void up_down (){
// if(swipress[0] || (same_state == true && !digitalRead(UP_butt)) ){
// if(millis()-prev_up > 50){
// up =true;
// prev_up = millis();
// }
// swipress[0] = false ;
// }
// else if(swipress[1] || (same_state == true && !digitalRead(DOWN_butt))){
// if(millis()- prev_dw > 50){
// down = true;
// prev_dw = millis();
// }
// swipress[1] = false ;
// }
// }
// // /////////////////COUNTDOWN//////////
// void countdown(){
// if(start_timer==true){
// if(MT==0 && HT>= 1){
// MT=60;
// HT=HT-1;
// }
// if(ST==0 && MT>=1){
// ST=60;
// MT=MT-1;
// }
// if(millis()-prevtime >=1000){
// if(ST>=1){
// ST=ST-1;
// digitalWrite(COOLER, HIGH);
// ST=constrain(ST,0,60);
// }
// prevtime=millis();
// }
// if(HT == 0 && MT ==0 && ST == 0){
// if(millis() - prevtimebuzzer >= 500 && !bazzend){
// if (buz_state == LOW) {
// buz_state = HIGH;
// }
// else {
// buz_state = LOW;
// bazzcnt = bazzcnt + 1;
// }
// digitalWrite(buzzer ,buz_state);
// if(bazzcnt == 3){
// bazzend = true;
// bazzcnt = 0;
// digitalWrite(buzzer ,LOW);
// }
// if(bazzend == true){
// time_stat = "DN ";
// digitalWrite(COOLER, LOW);
// }
// prevtimebuzzer = millis();
// }
// }
// }
// }
// ////////////Timer /////////////////
// void timerr(){
// if (!set_H && !set_M && !set_minute ) time_stat="OFF";
// if(swipress_1 /*&& flage_t_co */&& flag_page && set_Hours == false && set_minute== false ){
// set_H = 1;
// Serial.println("hhhhhhh");
// swipress_1 =false;
// }
// if (set_H) {
// if (up ) {
// HT++;
// up = false;
// }
// if (down ) {
// HT--;
// down= false;
// }
// HT = constrain(HT , 0 , 99);
// time_stat=" H ";
// set_Hours=true;
// }
// if(swipress_1 /*&& flage_t_co */ && flag_page &&set_Hours == true && !set_M && set_minute== false ){
// set_M = 1;
// set_H= 0;
// swipress_1 =false;
// }
// if (set_M) {
// if (up) {
// MT++;
// up = false;
// }
// if (down) {
// MT--;
// down = false;
// }
// MT=constrain(MT,0,59);
// time_stat=" M ";
// set_minute= true;
// if(swipress_1/*&& flage_t_co */&& set_minute== true && set_Hours == true && set_M && !set_H && !runn ){
// start_timer= true;
// set_H=0;
// set_M=0;
// set_Hours =0;
// time_stat="ON ";
// runn = true;
// swipress_1 =false;
// }
// }
// }
// // ////////////////////////////
// // ////////////// Variable for Screen1///////////////////
// void variable(){
// if (up && !selected ){
// selector++;
// up = false;
// }
// if (down && !selected ) {
// selector--;
// down = false;
// }
// // ////////constrian for control the sel from 1 to 4////////////////
// selector = constrain(selector, 1, 3);
// if(flage_t_co){
// // flg_cooler = true;
// timerr();
// swipress_1 = false;
// }
// if(flage_t_fa){
// timerr();
// swipress_1 = false;
// }
// if(flage_t_pu){
// timerr();
// swipress_1 = false;
// }
// }
// ////////////// press on pages///////////////
// void press_page(){
// if(swipress_1 && flag_page){
// if(selector == 1){
// flage_t_co = !flage_t_co;
// swipress_1 = false;
// }
// else if(selector == 2){
// flage_t_fa = !flage_t_fa;
// swipress_1 = false;
// }
// else if(selector == 3){
// flage_t_pu = !flage_t_pu;
// swipress_1 = false;
// }
// selected = !selected;
// }
// }
// #include <LiquidCrystal.h>
// #define en 8
// #define rs 9
// #define d4 10
// #define d5 11
// #define d6 12
// #define d7 13
// #define FAN 3
// #define COOLER 2
// #define PUMP 5
// // #define alarm 4
// #define UP_butt A1
// #define DOWN_butt A2
// #define Set_butt A3
// #define buzzer 6
// LiquidCrystal lcd( rs, en ,d4, d5, d6, d7);
// bool up;
// bool down ;
// // bool start_timer =false;
// // bool set_Hours= false;
// // bool set_minute= false;
// // bool runn = false;
// bool start_timer[3] ={false , false , false};
// bool set_Hours[3]= {false , false , false};
// bool set_minute[3]= {false , false , false};
// bool runn[3] = {false , false ,false};
// // bool set_H = false;
// // bool set_M = false;
// bool set_H [3] = {false , false ,false};
// bool set_M [3] = {false , false ,false};
// // String time_stat = " ";
// String time_stat[3] = {" "," "," "};
// // int HT = 0 ;
// // int MT = 0;
// // int ST = 0;
// int HT[3] = {0 , 0 , 0} ;
// int MT[3] = {0 , 0 , 0};
// int ST[3] = {0 , 0 , 0};
// bool flage_t[3] = {false , false , false} ;
// // bool flage_t_co = false ;
// // bool flage_t_fa = false ;
// // bool flage_t_pu = false ;
// byte flag_page ;
// unsigned long prev_up;
// unsigned long prev_dw;
// unsigned long prev_millis = 0 ;
// unsigned long prev = 0 ;
// unsigned long previ =0;
// unsigned long prevtime[3]={0,0,0};
// static unsigned long prevtimebuzzer = 0;
// byte bazzcnt= 0;
// bool bazzend = false ;
// int buz_state = LOW;
// bool swipress[2] = {LOW , LOW };
// byte lastButton[2] = {0 , 0};
// unsigned long lasbutStat[2] = {0 ,0 };
// unsigned long prevtim[2] = {0 ,0 } ;
// bool swipress_1 = false;
// byte lastButton_1;
// unsigned long lasbutStat_1 = 0;
// unsigned long lasbutStat_h = 0;
// bool same_state = false;
// bool same_state_1 = false;
// byte selector ;
// bool selected ;
// bool flag_dn[3] = {0,0,0};
// bool flag_1m = false ;
// bool flag_11m = false ;
// // ///////////SETUP///////////////////////////
// void setup(){
// lcd.begin(20,4);
// Serial.begin(9600);
// pinMode(UP_butt,INPUT_PULLUP);
// pinMode(DOWN_butt,INPUT_PULLUP);
// pinMode(Set_butt,INPUT_PULLUP);
// pinMode(COOLER,OUTPUT);
// pinMode(FAN,OUTPUT);
// pinMode(PUMP,OUTPUT);
// // prev_millis = millis();
// }
// // ///////////////////LOOP//////////////////////////
// void loop(){
// debounce_1(UP_butt,0);
// debounce_1(DOWN_butt,1);
// Debounce();
// if(flag_page){
// up_down();
// variable();
// }
// if(flage_t[0] && flag_page){
// timerr(0);
// }
// if(flage_t[1] && flag_page){
// timerr(1);
// }
// if(flage_t[2] && flag_page){
// timerr(2);
// }
// if(swipress_1 && flag_page){
// if(selector == 1){
// flage_t[0] = !flage_t[0];
// swipress_1 = false;
// }
// if(selector == 2){
// flage_t[1] = !flage_t[1];
// swipress_1 = false;
// }
// if(selector == 3){
// flage_t[2] = !flage_t[2];
// swipress_1 = false;
// }
// selected = !selected;
// }
// // (1000*60*1);
// if (start_timer[0] ){
// countdown(0,COOLER);
// if(millis()-prev_millis>=3000){
// flag_1m = true;
// // prev_millis = millis();
// }
// }
// if (start_timer[1] && flag_1m == true ){
// countdown(1,FAN);
// }
// if ( start_timer[2] && flag_dn[1] == 1 && flag_dn[0] == 1){
// countdown(2,PUMP);
// }
// if(flag_dn[2] == true){
// if(millis() - prevtimebuzzer >= 500 && !bazzend){
// if (buz_state == LOW) {
// buz_state = HIGH;
// }
// else {
// buz_state = LOW;
// bazzcnt = bazzcnt + 1;
// }
// digitalWrite(buzzer ,buz_state);
// if(bazzcnt == 3){
// bazzcnt = 0;
// digitalWrite(buzzer ,LOW);
// flag_dn[2] = false;
// }
// prevtimebuzzer = millis();
// }
// }
// if(swipress_1 && runn[0] == true && !flag_page ){
// Serial.println("cut");
// start_timer[0] = false;
// ST[0] = 0;
// MT[0] = 0;
// HT[0] = 0;
// start_timer[1] = false;
// ST[1] = 0;
// MT[1] = 0;
// HT[1] = 0;
// start_timer[2] = false;
// ST[2] = 0;
// MT[2] = 0;
// HT[2] = 0;
// set_minute[0] = false ;
// set_Hours[0] = false;
// runn [0]= false;
// set_minute[1] = false ;
// set_Hours[1] = false;
// runn [1]= false;
// set_minute[2] = false ;
// set_Hours[2] = false;
// runn [2]= false;
// flag_dn[0] = 0;
// flag_dn[1] = 0;
// flag_dn[2] = 0;
// bazzend = false;
// bazzcnt= 0 ;
// time_stat[0] = "OFF";
// time_stat[1] = "OFF";
// time_stat[2] = "OFF";
// digitalWrite(COOLER, LOW);
// digitalWrite(FAN, LOW);
// digitalWrite(PUMP, LOW);
// swipress_1 =false;
// }
// if(millis() - prev >50){
// Display();
// prev= millis();
// }
// }
// // /////////////////////////////////////////////
// #define same_place 100
// // ///////// func for display //////////////////
// void lcd_print( String Value, int row , int col ) {
// if(row != same_place || col != same_place) lcd.setCursor(col, row);
// lcd.print(Value);
// }
// // /////////////// LCD Display //////////////////
// void Display(){
// if(!flag_page ){
// lcd_print("CO: ", 0, 0);
// lcd_print(time_stat[0] , 0 ,7);
// lcd.print(" ");
// if(HT[0] < 10) lcd.print("0");
// lcd.print(HT[0] ,DEC);
// lcd.print(":");
// if(MT[0] < 10) lcd.print("0");
// lcd.print(MT[0], DEC);
// lcd.print(":");
// if(ST[0] < 10 ) lcd.print("0");
// lcd.print(ST[0],DEC);
// lcd_print("Fa: ", 1,0);
// lcd_print(time_stat[1] , 1 ,7);
// lcd.print(" ");
// if(HT[1] < 10) lcd.print("0");
// lcd.print(HT[1] ,DEC);
// lcd.print(":");
// if(MT[1] < 10) lcd.print("0");
// lcd.print(MT[1], DEC);
// lcd.print(":");
// if(ST[1] < 10 ) lcd.print("0");
// lcd.print(ST[1],DEC);
// lcd_print("Pu: ", 2,0);
// lcd_print(time_stat[2] , 2 ,7);
// lcd.print(" ");
// if(HT[2] < 10) lcd.print("0");
// lcd.print(HT[2] ,DEC);
// lcd.print(":");
// if(MT[2] < 10) lcd.print("0");
// lcd.print(MT[2], DEC);
// lcd.print(":");
// if(ST[2] < 10 ) lcd.print("0");
// lcd.print(ST[2],DEC);
// }
// if (flag_page ){
// if (selector == 1) {
// lcd_print(">", 0, 0);
// lcd_print(" ", 1, 0);
// lcd_print(" ", 2, 0);
// }
// lcd_print("T_co: ", 0,1);
// lcd_print(time_stat[0] , 0 ,7);
// lcd.print(" ");
// if(HT[0] < 10) lcd.print("0");
// lcd.print(HT[0] ,DEC);
// lcd.print(":");
// if(MT[0] < 10) lcd.print("0");
// lcd.print(MT[0], DEC);
// lcd.print(":");
// if(ST[0] < 10 ) lcd.print("0");
// lcd.print(ST[0],DEC);
// if (selector == 2) {
// lcd_print(" ", 0, 0);
// lcd_print(">", 1, 0);
// lcd_print(" ", 2, 0);
// }
// lcd_print("T_fa: ", 1,1);
// lcd_print(time_stat[1] , 1 ,7);
// lcd.print(" ");
// if(HT[1] < 10) lcd.print("0");
// lcd.print(HT[1] ,DEC);
// lcd.print(":");
// if(MT[1] < 10) lcd.print("0");
// lcd.print(MT[1], DEC);
// lcd.print(":");
// if(ST[1] < 10 ) lcd.print("0");
// lcd.print(ST[1],DEC);
// if (selector == 3) {
// lcd_print(" ", 0, 0);
// lcd_print(" ", 1, 0);
// lcd_print(">", 2, 0);
// }
// lcd_print("T_pu: ", 2,1);
// lcd_print(time_stat[2] , 2 ,7);
// lcd.print(" ");
// if(HT[2] <10) lcd.print("0");
// lcd.print(HT[2] ,DEC);
// lcd.print(":");
// if(MT[2] < 10) lcd.print("0");
// lcd.print(MT[2], DEC);
// lcd.print(":");
// if(ST[2] < 10 ) lcd.print("0");
// lcd.print(ST[2],DEC);
// }
// }
// // ///////////////// Debounce //////////////////////////
// void Debounce() {
// if (millis() - lasbutStat_1 >= 50) {
// byte buttonState_1 = digitalRead(Set_butt);
// if (buttonState_1 == LOW && lastButton_1 == HIGH) { // rising
// swipress_1 = true;
// lasbutStat_h = millis();
// } else if (buttonState_1 == LOW && lastButton_1 == LOW) { // same
// if (millis() - lasbutStat_h >= 1000 && same_state_1 == false) {
// swipress_1 = false;
// // runn = false;
// set_Hours[0] = false;
// set_minute[0] = false;
// set_Hours[1] = false;
// set_minute[1] = false;
// set_Hours[2] = false;
// set_minute[2] = false;
// selected = false;
// selector = false;
// flage_t[0] = false;
// flage_t[1] = false;
// flage_t[2] = false;
// flag_page = !flag_page;
// same_state_1 = true;
// lcd.clear();
// }
// } else if (buttonState_1 == HIGH && lastButton_1 == LOW) { // failling
// same_state_1 = false;
// }
// lasbutStat_1 = millis();
// lastButton_1 = buttonState_1;
// }
// }
// // //////////// Debounce /////////////////
// void debounce_1(int button, int index){
// if (millis() - lasbutStat[index] >= 50){
// byte buttonState = digitalRead(button);
// if(buttonState == LOW && lastButton[index] == HIGH){ //rising
// swipress[index] = true;
// previ = millis();
// }
// else if(buttonState == LOW && lastButton[index] == LOW && same_state == false ){ //same
// swipress[index] = false;
// if(millis()-previ >1000){
// same_state = true;
// }
// }
// else if (buttonState == HIGH && lastButton[index] == LOW ){ // failling
// same_state = false;
// }
// lasbutStat[index] = millis();
// lastButton[index] = buttonState;
// }
// }
// // ////////////////up&down//////////
// void up_down (){
// if(swipress[0] || (same_state == true && !digitalRead(UP_butt)) ){
// if(millis()-prev_up > 50){
// up =true;
// prev_up = millis();
// }
// swipress[0] = false ;
// }
// else if(swipress[1] || (same_state == true && !digitalRead(DOWN_butt))){
// if(millis()- prev_dw > 50){
// down = true;
// prev_dw = millis();
// }
// swipress[1] = false ;
// }
// }
// // /////////////////COUNTDOWN//////////
// void countdown(int indexx ,int butt){
// if(!flag_page){
// if(start_timer[indexx]==true){
// if(HT[indexx] == 0 && MT[indexx] == 0 && ST[indexx] == 0){
// flag_dn[indexx] = 1;
// time_stat[indexx] = "DN ";
// digitalWrite(butt, LOW);
// }
// if(MT[indexx]==0 && HT[indexx]>= 1){
// MT[indexx]=60;
// HT[indexx]=HT[indexx] - 1;
// }
// if(ST[indexx]==0 && MT[indexx]>=1){
// ST[indexx]=60;
// MT[indexx]=MT[indexx] - 1;
// }
// if(millis()-prevtime[indexx] >=1000){
// if(ST[indexx] >= 1){
// ST[indexx]=ST[indexx] - 1;
// digitalWrite(butt, HIGH);
// ST[indexx]=constrain(ST[indexx],0,60);
// }
// prevtime[indexx]=millis();
// }
// // if(HT[indexx] == 0 && MT[indexx] == 0 && ST[indexx] == 0){
// // flag_dn[indexx] = 1;
// // time_stat[indexx] = "DN ";
// // digitalWrite(butt, LOW);
// // if(millis() - prevtimebuzzer >= 500 && !bazzend){
// // if (buz_state == LOW) {
// // buz_state = HIGH;
// // }
// // else {
// // buz_state = LOW;
// // bazzcnt = bazzcnt + 1;
// // }
// // digitalWrite(buzzer ,buz_state);
// // if(bazzcnt == 3){
// // bazzend = true;
// // bazzcnt = 0;
// // digitalWrite(buzzer ,LOW);
// // }
// // if(bazzend == true){
// // time_stat[indexx] = "DN ";
// // digitalWrite(butt, LOW);
// // }
// // prevtimebuzzer = millis();
// // }
// // }
// }
// }
// }
// ////////////Timer /////////////////
// void timerr(int indexx){
// if(flage_t[indexx]){
// if (!set_H[indexx] && !set_M[indexx] && !set_minute[indexx] ) time_stat[indexx]="OFF";
// if(swipress_1 && flag_page && set_Hours[indexx] == false && set_minute[indexx]== false ){
// set_H[indexx] = 1;
// Serial.println("H");
// swipress_1 =false;
// }
// if (set_H[indexx]) {
// if (up ) {
// HT[indexx]++;
// up = false;
// }
// if (down ) {
// HT[indexx]--;
// down= false;
// }
// HT[indexx] = constrain(HT[indexx] , 0 , 99);
// time_stat[indexx]=" H ";
// set_Hours[indexx]=true;
// }
// if(swipress_1 && flag_page && set_Hours[indexx] == true && !set_M[indexx] && set_minute[indexx]== false ){
// Serial.println("M");
// set_M[indexx] = 1;
// set_H[indexx]= 0;
// swipress_1 =false;
// }
// if (set_M[indexx]) {
// if (up) {
// MT[indexx]++;
// up = false;
// }
// if (down) {
// MT[indexx]--;
// down = false;
// }
// MT[indexx]=constrain(MT[indexx], 0 , 59);
// time_stat[indexx]=" M ";
// set_minute[indexx]= true;
// }
// if(swipress_1 && set_minute[indexx]== true && set_Hours[indexx] == true && set_M[indexx] && !set_H[indexx] && !runn[indexx] ){
// start_timer[indexx]= true;
// set_H[indexx]=0;
// set_M[indexx]=0;
// set_Hours[indexx] =0;
// time_stat[indexx]="ON ";
// runn[indexx] = true;
// swipress_1 =false;
// selected = !selected;
// }
// }
// }
// // ////////////////////////////
// // ////////////// Variable for Screen1///////////////////
// void variable(){
// if (up && !selected ){
// selector++;
// up = false;
// }
// if (down && !selected ) {
// selector--;
// down = false;
// }
// // ////////constrian for control the sel from 1 to 4////////////////
// selector = constrain(selector, 1, 3);
// // if(flage_t[0]){
// // timerr(0);
// // // swipress_1 = false;
// // }
// // if(flage_t[1]){
// // timerr(1);
// // // swipress_1 = false;
// // }
// // if(flage_t[2]){
// // timerr(2);
// // // swipress_1 = false;
// // }
// }
// ////////////// press on pages///////////////
// void press_page(){
// if(swipress_1 && flag_page){
// if(selector == 1){
// flage_t[0] = !flage_t[0];
// swipress_1 = false;
// }
// if(selector == 2){
// flage_t[1] = !flage_t[1];
// swipress_1 = false;
// }
// if(selector == 3){
// flage_t[2] = !flage_t[2];
// swipress_1 = false;
// }
// selected = !selected;
// }
// }
// #define en 8
// #define rs 9
// #define d4 10
// #define d5 11
// #define d6 12
// #define d7 13
// #include <LiquidCrystal.h>
// #include <EEPROM.h>
// #define rs 9
// #define en 8
// #define d4 10
// #define d5 11
// #define d6 12
// #define d7 13
// #define FAN 3
// #define COOLER 2
// #define PUMP 5
// #define alarm 4
// #define UP_butt A1
// #define DOWN_butt A2
// #define Set_butt A3
// #define buzzer 6
// LiquidCrystal lcd( rs, en ,d4, d5, d6, d7);
// bool up;
// bool down ;
// // bool start_timer =false;
// // bool set_Hours= false;
// // bool set_minute= false;
// // bool runn = false;
// bool start_timer[3] ={false , false , false};
// bool set_Hours[3]= {false , false , false};
// bool set_minute[3]= {false , false , false};
// bool runn[3] = {false , false ,false};
// // bool set_H = false;
// // bool set_M = false;
// bool set_H [3] = {false , false ,false};
// bool set_M [3] = {false , false ,false};
// // String time_stat = " ";
// String time_stat[3] = {" "," "," "};
// // int HT = 0 ;
// // int MT = 0;
// // int ST = 0;
// int HT[3] = {0 , 0 , 0} ;
// int MT[3] = {0 , 0 , 0};
// int ST[3] = {0 , 0 , 0};
// bool flage_t[3] = {false , false , false} ;
// // bool flage_t_co = false ;
// // bool flage_t_fa = false ;
// // bool flage_t_pu = false ;
// byte flag_page ;
// unsigned long prev_up;
// unsigned long prev_dw;
// unsigned long prev_millis = 0 ;
// unsigned long prev_millis_1 = 0 ;
// unsigned long prev = 0 ;
// unsigned long previ =0;
// unsigned long prevtime[3]={0,0,0};
// static unsigned long prevtimebuzzer = 0;
// byte bazzcnt= 0;
// bool bazzend = false ;
// int buz_state = LOW;
// bool swipress[2] = {LOW , LOW };
// byte lastButton[2] = {0 , 0};
// unsigned long lasbutStat[2] = {0 ,0 };
// unsigned long prevtim[2] = {0 ,0 } ;
// bool swipress_1 = false;
// byte lastButton_1;
// unsigned long lasbutStat_1 = 0;
// unsigned long lasbutStat_h = 0;
// bool same_state = false;
// bool same_state_1 = false;
// byte selector ;
// bool selected ;
// bool flag_dn[3] = {0,0,0};
// bool flag_1m = false ;
// bool flag_11m = false ;
// bool full = false ;
// // ///////////SETUP///////////////////////////
// void setup(){
// lcd.begin(20,4);
// Serial.begin(9600);
// pinMode(UP_butt,INPUT_PULLUP);
// pinMode(DOWN_butt,INPUT_PULLUP);
// pinMode(Set_butt,INPUT_PULLUP);
// pinMode(COOLER,OUTPUT);
// pinMode(FAN,OUTPUT);
// pinMode(PUMP,OUTPUT);
// pinMode(alarm,INPUT);
// lcd.setCursor(2,1);
// lcd.print("<---CHEM TECH--->");
// lcd.setCursor(4, 2);
// lcd.print("--ICE MAKER--");
// delay(3000);
// lcd.clear();
// // EEPROM.get(0, MT[0]);
// // EEPROM.get(5, MT[1]);
// // EEPROM.get(10, MT[2]);
// // EEPROM.get(20, ST[0]);
// // EEPROM.get(25, ST[1]);
// // EEPROM.get(30, ST[2]);
// // EEPROM.get(40, HT[0]);
// // EEPROM.get(45, HT[1]);
// // EEPROM.get(50, HT[2]);
// EEPROM.put(0,0);
// EEPROM.put(5,0);
// EEPROM.put(10,0);
// EEPROM.put(20,0);
// EEPROM.put(25,0);
// EEPROM.put(30,0);
// EEPROM.put(40,0);
// EEPROM.put(45,0);
// EEPROM.put(50,0);
// }
// // ///////////////////LOOP//////////////////////////
// void loop(){
// debounce_1(UP_butt,0);
// debounce_1(DOWN_butt,1);
// Debounce();
// if(flag_page){
// up_down();
// variable();
// }
// if(flage_t[0] && flag_page){
// timerr(0);
// }
// if(flage_t[1] && flag_page){
// timerr(1);
// }
// if(flage_t[2] && flag_page){
// timerr(2);
// }
// if(swipress_1 && flag_page){
// if(selector == 1){
// flage_t[0] = !flage_t[0];
// swipress_1 = false;
// }
// if(selector == 2){
// flage_t[1] = !flage_t[1];
// swipress_1 = false;
// }
// if(selector == 3){
// flage_t[2] = !flage_t[2];
// swipress_1 = false;
// }
// selected = !selected;
// }
// if(analogRead(alarm)) full = true;
// if(millis()-prev_millis_1>=(60000) && !flag_page){
// flag_1m = true;
// prev_millis_1 = millis();
// // prev_millis = millis();
// }
// if (start_timer[0] && flag_1m == true ){
// countdown(0,COOLER);
// digitalWrite(COOLER, HIGH);
// // if(millis()-prev_millis>=(60000)){
// // flag_11m = true;
// // prev_millis = millis();
// // }
// }
// if ( start_timer[0] && flag_1m == true && !flag_page){
// if(millis()-prev_millis>=(60000)){
// flag_11m = true;
// prev_millis = millis();
// }
// }
// // if(millis()-prev_millis>=(60000)){
// // flag_11m = true;
// // // prev_millis = millis();
// // }
// // if(ST[0]==60) flag_11m = true;
// if (start_timer[1] && flag_11m == true){
// countdown(1,FAN);
// digitalWrite(FAN, HIGH);
// }
// if ( start_timer[2] && flag_dn[1] == 1 && flag_dn[0] == 1){
// countdown(2,PUMP);
// digitalWrite(PUMP, HIGH);
// flag_11m = false;
// }
// if (flag_dn[0] == 1) digitalWrite(COOLER, LOW);
// if (flag_dn[1] == 1) digitalWrite(FAN, LOW);
// if(flag_dn[2] == true){
// digitalWrite(PUMP, LOW);
// if(millis() - prevtimebuzzer >= 500 && !bazzend){
// if (buz_state == LOW) {
// buz_state = HIGH;
// }
// else {
// buz_state = LOW;
// bazzcnt = bazzcnt + 1;
// }
// digitalWrite(buzzer ,buz_state);
// lcd.setCursor(0, 3);
// lcd.print("Pre SET to Stop Buzz");
// prevtimebuzzer = millis();
// }
// }
// if(swipress_1 && runn[0] == true && !flag_page ){
// Serial.println("cut");
// start_timer[0] = false;
// ST[0] = 0;
// // MT[0] = 0;
// // HT[0] = 0;
// EEPROM.put(0, MT[0]);
// EEPROM.put(20, ST[0]);
// EEPROM.put(40, HT[0]);
// start_timer[1] = false;
// ST[1] = 0;
// // MT[1] = 0;
// // HT[1] = 0;
// EEPROM.put(5, MT[1]);
// EEPROM.put(25, ST[1]);
// EEPROM.put(45, HT[1]);
// start_timer[2] = false;
// ST[2] = 0;
// // MT[2] = 0;
// // HT[2] = 0;
// EEPROM.put(10, MT[2]);
// EEPROM.put(30, ST[2]);
// EEPROM.put(50, HT[2]);
// set_minute[0] = false ;
// set_Hours[0] = false;
// runn [0]= false;
// set_minute[1] = false ;
// set_Hours[1] = false;
// runn [1]= false;
// set_minute[2] = false ;
// set_Hours[2] = false;
// runn [2]= false;
// flag_dn[0] = 0;
// flag_dn[1] = 0;
// flag_dn[2] = 0;
// bazzend = false;
// bazzcnt= 0 ;
// digitalWrite(buzzer, LOW);
// time_stat[0] = "OFF";
// time_stat[1] = "OFF";
// time_stat[2] = "OFF";
// digitalWrite(COOLER, LOW);
// digitalWrite(FAN, LOW);
// digitalWrite(PUMP, LOW);
// flag_1m = false;
// flag_11m = false;
// swipress_1 =false;
// }
// if(swipress_1 && !flag_page && time_stat[0] == "OFF" && (MT[0]>=1 || HT[0]>=1)) {
// start_timer[0]= true;
// set_minute[0] = true ;
// set_Hours[0] = true;
// runn [0]= true;
// start_timer[1]= true;
// set_minute[1] = true ;
// set_Hours[1] = true;
// runn [1]= true;
// start_timer[2]= true;
// set_minute[2] = true ;
// set_Hours[2] = true;
// runn [2]= true;
// time_stat[0] = "ON ";
// time_stat[1] = "ON ";
// time_stat[2] = "ON ";
// // digitalWrite(COOLER, LOW);
// // digitalWrite(FAN, LOW);
// // digitalWrite(PUMP, LOW);
// countdown(0,COOLER);
// swipress_1 = false;
// }
// if(millis() - prev >50){
// Display();
// prev= millis();
// }
// }
// // /////////////////////////////////////////////
// #define same_place 100
// // ///////// func for display //////////////////
// void lcd_print( String Value, int row , int col ) {
// if(row != same_place || col != same_place) lcd.setCursor(col, row);
// lcd.print(Value);
// }
// // /////////////// LCD Display //////////////////
// void Display(){
// if(!flag_page ){
// lcd_print("COOL:", 0, 0);
// lcd_print(time_stat[0] , 0 ,7);
// lcd.print(" ");
// if(HT[0] < 10) lcd.print("0");
// lcd.print(HT[0] ,DEC);
// lcd.print(":");
// if(MT[0] < 10) lcd.print("0");
// lcd.print(MT[0], DEC);
// lcd.print(":");
// if(ST[0] < 10 ) lcd.print("0");
// lcd.print(ST[0],DEC);
// lcd_print("Fan: ", 1,0);
// lcd_print(time_stat[1] , 1 ,7);
// lcd.print(" ");
// if(HT[1] < 10) lcd.print("0");
// lcd.print(HT[1] ,DEC);
// lcd.print(":");
// if(MT[1] < 10) lcd.print("0");
// lcd.print(MT[1], DEC);
// lcd.print(":");
// if(ST[1] < 10 ) lcd.print("0");
// lcd.print(ST[1],DEC);
// lcd_print("PUMP: ", 2,0);
// lcd_print(time_stat[2] , 2 ,7);
// lcd.print(" ");
// if(HT[2] < 10) lcd.print("0");
// lcd.print(HT[2] ,DEC);
// lcd.print(":");
// if(MT[2] < 10) lcd.print("0");
// lcd.print(MT[2], DEC);
// lcd.print(":");
// if(ST[2] < 10 ) lcd.print("0");
// lcd.print(ST[2],DEC);
// }
// if (flag_page ){
// if (selector == 1) {
// lcd_print(">", 0, 0);
// lcd_print(" ", 1, 0);
// lcd_print(" ", 2, 0);
// }
// lcd_print("T_co: ", 0,1);
// lcd_print(time_stat[0] , 0 ,7);
// lcd.print(" ");
// if(HT[0] < 10) lcd.print("0");
// lcd.print(HT[0] ,DEC);
// lcd.print(":");
// if(MT[0] < 10) lcd.print("0");
// lcd.print(MT[0], DEC);
// lcd.print(":");
// if(ST[0] < 10 ) lcd.print("0");
// lcd.print(ST[0],DEC);
// if (selector == 2) {
// lcd_print(" ", 0, 0);
// lcd_print(">", 1, 0);
// lcd_print(" ", 2, 0);
// }
// lcd_print("T_fa: ", 1,1);
// lcd_print(time_stat[1] , 1 ,7);
// lcd.print(" ");
// if(HT[1] < 10) lcd.print("0");
// lcd.print(HT[1] ,DEC);
// lcd.print(":");
// if(MT[1] < 10) lcd.print("0");
// lcd.print(MT[1], DEC);
// lcd.print(":");
// if(ST[1] < 10 ) lcd.print("0");
// lcd.print(ST[1],DEC);
// if (selector == 3) {
// lcd_print(" ", 0, 0);
// lcd_print(" ", 1, 0);
// lcd_print(">", 2, 0);
// }
// lcd_print("T_pu: ", 2,1);
// lcd_print(time_stat[2] , 2 ,7);
// lcd.print(" ");
// if(HT[2] <10) lcd.print("0");
// lcd.print(HT[2] ,DEC);
// lcd.print(":");
// if(MT[2] < 10) lcd.print("0");
// lcd.print(MT[2], DEC);
// lcd.print(":");
// if(ST[2] < 10 ) lcd.print("0");
// lcd.print(ST[2],DEC);
// }
// }
// // ///////////////// Debounce for swip_setting//////////////////////////
// void Debounce() {
// if (millis() - lasbutStat_1 >= 50) {
// byte buttonState_1 = digitalRead(Set_butt);
// if (buttonState_1 == LOW && lastButton_1 == HIGH) { // rising
// swipress_1 = true;
// lasbutStat_h = millis();
// } else if (buttonState_1 == LOW && lastButton_1 == LOW) { // same
// if (millis() - lasbutStat_h >= 1000 && same_state_1 == false) {
// swipress_1 = false;
// // runn = false;
// set_Hours[0] = false;
// set_minute[0] = false;
// set_Hours[1] = false;
// set_minute[1] = false;
// set_Hours[2] = false;
// set_minute[2] = false;
// selected = false;
// selector = false;
// flage_t[0] = false;
// flage_t[1] = false;
// flage_t[2] = false;
// flag_page = !flag_page;
// same_state_1 = true;
// lcd.clear();
// }
// } else if (buttonState_1 == HIGH && lastButton_1 == LOW) { // failling
// same_state_1 = false;
// }
// lasbutStat_1 = millis();
// lastButton_1 = buttonState_1;
// }
// }
// // //////////// Debounce for swip up_down /////////////////
// void debounce_1(int button, int index){
// if (millis() - lasbutStat[index] >= 50){
// byte buttonState = digitalRead(button);
// if(buttonState == LOW && lastButton[index] == HIGH){ //rising
// swipress[index] = true;
// previ = millis();
// }
// else if(buttonState == LOW && lastButton[index] == LOW && same_state == false ){ //same
// swipress[index] = false;
// if(millis()-previ >1000){
// same_state = true;
// }
// }
// else if (buttonState == HIGH && lastButton[index] == LOW ){ // failling
// same_state = false;
// }
// lasbutStat[index] = millis();
// lastButton[index] = buttonState;
// }
// }
// // ////////////////up&down//////////
// void up_down (){
// if(swipress[0] || (same_state == true && !digitalRead(UP_butt)) ){
// if(millis()-prev_up > 50){
// up =true;
// prev_up = millis();
// }
// swipress[0] = false ;
// }
// else if(swipress[1] || (same_state == true && !digitalRead(DOWN_butt))){
// if(millis()- prev_dw > 50){
// down = true;
// prev_dw = millis();
// }
// swipress[1] = false ;
// }
// }
// // /////////////////COUNTDOWN//////////
// void countdown(int indexx ,int butt){
// if(!flag_page){
// if(start_timer[indexx]==true){
// if(HT[indexx] == 0 && MT[indexx] == 0 && ST[indexx] == 0){
// flag_dn[indexx] = 1;
// time_stat[indexx] = "DN ";
// digitalWrite(butt, LOW);
// }
// if(MT[indexx]==0 && HT[indexx]>= 1){
// MT[indexx]=60;
// HT[indexx]=HT[indexx] - 1;
// }
// if(ST[indexx]==0 && MT[indexx]>=1){
// ST[indexx]=60;
// MT[indexx]=MT[indexx] - 1;
// }
// if(millis()-prevtime[indexx] >=1000){
// if(ST[indexx] >= 1){
// ST[indexx]=ST[indexx] - 1;
// // digitalWrite(butt, HIGH);
// ST[indexx]=constrain(ST[indexx],0,60);
// }
// prevtime[indexx]=millis();
// }
// // if(HT[indexx] == 0 && MT[indexx] == 0 && ST[indexx] == 0){
// // flag_dn[indexx] = 1;
// // time_stat[indexx] = "DN ";
// // digitalWrite(butt, LOW);
// // if(millis() - prevtimebuzzer >= 500 && !bazzend){
// // if (buz_state == LOW) {
// // buz_state = HIGH;
// // }
// // else {
// // buz_state = LOW;
// // bazzcnt = bazzcnt + 1;
// // }
// // digitalWrite(buzzer ,buz_state);
// // if(bazzcnt == 3){
// // bazzend = true;
// // bazzcnt = 0;
// // digitalWrite(buzzer ,LOW);
// // }
// // if(bazzend == true){
// // time_stat[indexx] = "DN ";
// // digitalWrite(butt, LOW);
// // }
// // prevtimebuzzer = millis();
// // }
// // }
// }
// }
// }
// ////////////Timer /////////////////
// void timerr(int indexx){
// if(flage_t[indexx]){
// if (!set_H[indexx] && !set_M[indexx] && !set_minute[indexx] ) time_stat[indexx]="OFF";
// if(swipress_1 && flag_page && set_Hours[indexx] == false && set_minute[indexx]== false ){
// set_H[indexx] = 1;
// Serial.println("H");
// swipress_1 =false;
// }
// if (set_H[indexx]) {
// if (up ) {
// HT[indexx]++;
// up = false;
// }
// if (down ) {
// HT[indexx]--;
// down= false;
// }
// HT[indexx] = constrain(HT[indexx] , 0 , 99);
// time_stat[indexx]=" H ";
// set_Hours[indexx]=true;
// }
// if(swipress_1 && flag_page && set_Hours[indexx] == true && !set_M[indexx] && set_minute[indexx]== false ){
// Serial.println("M");
// set_M[indexx] = 1;
// set_H[indexx]= 0;
// swipress_1 =false;
// }
// if (set_M[indexx]) {
// if (up) {
// MT[indexx]++;
// up = false;
// }
// if (down) {
// MT[indexx]--;
// down = false;
// }
// MT[indexx]=constrain(MT[indexx], 0 , 59);
// time_stat[indexx]=" M ";
// set_minute[indexx]= true;
// }
// if(swipress_1 && set_minute[indexx]== true && set_Hours[indexx] == true && set_M[indexx] && !set_H[indexx] && !runn[indexx] ){
// start_timer[indexx]= true;
// set_H[indexx]=0;
// set_M[indexx]=0;
// set_Hours[indexx] =0;
// time_stat[indexx]="ON ";
// runn[indexx] = true;
// swipress_1 =false;
// selected = !selected;
// }
// }
// }
// // ////////////////////////////
// // ////////////// Variable for Screen1///////////////////
// void variable(){
// if (up && !selected ){
// selector++;
// up = false;
// }
// if (down && !selected ) {
// selector--;
// down = false;
// }
// // ////////constrian for control the sel from 1 to 3////////////////
// selector = constrain(selector, 1, 3);
// // if(flage_t[0]){
// // timerr(0);
// // // swipress_1 = false;
// // }
// // if(flage_t[1]){
// // timerr(1);
// // // swipress_1 = false;
// // }
// // if(flage_t[2]){
// // timerr(2);
// // // swipress_1 = false;
// // }
// }
// ////////////// press on pages///////////////
// void press_page(){
// if(swipress_1 && flag_page){
// if(selector == 1){
// flage_t[0] = !flage_t[0];
// swipress_1 = false;
// }
// if(selector == 2){
// flage_t[1] = !flage_t[1];
// swipress_1 = false;
// }
// if(selector == 3){
// flage_t[2] = !flage_t[2];
// swipress_1 = false;
// }
// selected = !selected;
// }
// }
#include <LiquidCrystal.h>
#include <EEPROM.h>
// #define rs 8
// #define en 9
#define d4 10
#define d5 11
#define d6 12
#define d7 13
#define rs 9
#define en 8
#define FAN 3
#define COOLER 2
#define PUMP 5
#define alarm 4
#define UP_butt A1
#define DOWN_butt A2
#define Set_butt A3
#define buzzer 6
LiquidCrystal lcd( rs, en ,d4, d5, d6, d7);
bool up;
bool down ;
bool start_timer[3] ={false , false , false};
bool set_Hours[3]= {false , false , false};
bool set_minute[3]= {false , false , false};
bool runn[3] = {false , false ,false};
bool set_H [3] = {false , false ,false};
bool set_M [3] = {false , false ,false};
String time_stat[3] = {" "," "," "};
int HT[3] = {0 , 0 , 0} ;
int MT[3] = {0 , 0 , 0};
int ST[3] = {0 , 0 , 0};
bool flage_t[3] = {false , false , false} ;
byte flag_page ;
unsigned long prev_up;
unsigned long prev_dw;
unsigned long prev_millis = 0 ;
unsigned long prev_millis_1 = 0 ;
unsigned long prev = 0 ;
unsigned long previ =0;
unsigned long prevtime[3]={0,0,0};
static unsigned long prevtimebuzzer = 0;
byte bazzcnt= 0;
bool bazzend = false ;
int buz_state = LOW;
bool swipress[2] = {LOW , LOW };
byte lastButton[2] = {0 , 0};
unsigned long lasbutStat[2] = {0 ,0 };
unsigned long prevtim[2] = {0 ,0 } ;
bool swipress_1 = false;
byte lastButton_1;
unsigned long lasbutStat_1 = 0;
unsigned long lasbutStat_h = 0;
bool same_state = false;
bool same_state_1 = false;
byte selector ;
bool selected ;
bool flag_dn[3] = {0,0,0};
bool flag_1m = false ;
bool flag_11m = false ;
bool full = false ;
// ///////////SETUP///////////////////////////
void setup(){
lcd.begin(20,4);
Serial.begin(9600);
pinMode(UP_butt,INPUT_PULLUP);
pinMode(DOWN_butt,INPUT_PULLUP);
pinMode(Set_butt,INPUT_PULLUP);
pinMode(COOLER,OUTPUT);
pinMode(FAN,OUTPUT);
pinMode(PUMP,OUTPUT);
pinMode(alarm,INPUT);
lcd.setCursor(2,1);
lcd.print("<---CHEM TECH--->");
lcd.setCursor(4, 2);
lcd.print("--ICE MAKER--");
delay(3000);
lcd.clear();
// EEPROM.get(0, MT[0]);
// EEPROM.get(5, MT[1]);
// EEPROM.get(10, MT[2]);
// EEPROM.get(20, ST[0]);
// EEPROM.get(25, ST[1]);
// EEPROM.get(30, ST[2]);
// EEPROM.get(40, HT[0]);
// EEPROM.get(45, HT[1]);
// EEPROM.get(50, HT[2]);
EEPROM.put(0,0);
EEPROM.put(5,0);
EEPROM.put(10,0);
EEPROM.put(20,0);
EEPROM.put(25,0);
EEPROM.put(30,0);
EEPROM.put(40,0);
EEPROM.put(45,0);
EEPROM.put(50,0);
}
// ///////////////////LOOP//////////////////////////
void loop(){
debounce_1(UP_butt,0);
debounce_1(DOWN_butt,1);
Debounce();
if(flag_page){
up_down();
variable();
}
if(flage_t[0] && flag_page){
timerr(0);
// swipress_1 = false;
}
if(flage_t[1] && flag_page){
timerr(1);
// swipress_1 = false;
}
if(flage_t[2] && flag_page){
timerr(2);
// swipress_1 = false;
}
if(swipress_1 && flag_page){
if(selector == 1){
flage_t[0] = !flage_t[0];
swipress_1 = false;
}
if(selector == 2){
flage_t[1] = !flage_t[1];
swipress_1 = false;
}
if(selector == 3){
flage_t[2] = !flage_t[2];
swipress_1 = false;
}
selected = !selected;
}
if(analogRead(alarm)) full = true;
if(millis()-prev_millis_1>=(59000) && !flag_page){
flag_1m = true;
prev_millis_1 = millis();
}
else {
digitalWrite(COOLER, LOW);
}
if (start_timer[0] && flag_1m == true && !flag_page){
countdown(0,COOLER);
time_stat[0] = "ON ";
digitalWrite(COOLER, HIGH);
}
if(ST[0]==1) flag_11m = true;
if (start_timer[1] && flag_11m == true && !flag_page){
countdown(1,FAN);
time_stat[1] = "ON ";
}
if (flag_dn[0] == 1){
// digitalWrite(COOLER, LOW);
time_stat[0] = "DN ";
}
if (flag_dn[1] == 1){
digitalWrite(FAN, LOW);
time_stat[1] = "DN ";
}
if ( start_timer[2] && flag_dn[1] == 1 && flag_dn[0] == 1){
countdown(2,PUMP);
time_stat[2] = "ON ";
// digitalWrite(COOLER, LOW);
// digitalWrite(FAN, LOW);
flag_11m = false;
}
if(flag_dn[2] == true){
time_stat[2] = "DN ";
if(millis() - prevtimebuzzer >= 500 && !bazzend){
if (buz_state == LOW) {
buz_state = HIGH;
}
else {
buz_state = LOW;
bazzcnt = bazzcnt + 1;
}
digitalWrite(buzzer ,buz_state);
lcd.setCursor(0, 3);
lcd.print("SET to Stop Buzzer");
prevtimebuzzer = millis();
}
}
if(swipress_1 && start_timer[0] == true && !flag_page ){
Serial.println("cut");
start_timer[0] = false;
ST[0] = 0;
EEPROM.put(0, MT[0]);
EEPROM.put(20, ST[0]);
EEPROM.put(40, HT[0]);
start_timer[1] = false;
ST[1] = 0;
EEPROM.put(5, MT[1]);
EEPROM.put(25, ST[1]);
EEPROM.put(45, HT[1]);
start_timer[2] = false;
ST[2] = 0;
EEPROM.put(10, MT[2]);
EEPROM.put(30, ST[2]);
EEPROM.put(50, HT[2]);
set_minute[0] = false ;
set_Hours[0] = false;
runn [0]= false;
set_minute[1] = false ;
set_Hours[1] = false;
runn [1]= false;
set_minute[2] = false ;
set_Hours[2] = false;
runn [2]= false;
flag_dn[0] = 0;
flag_dn[1] = 0;
flag_dn[2] = 0;
bazzend = false;
bazzcnt= 0 ;
digitalWrite(buzzer, LOW);
time_stat[0] = "OFF";
time_stat[1] = "OFF";
time_stat[2] = "OFF";
digitalWrite(COOLER, LOW);
digitalWrite(FAN, LOW);
digitalWrite(PUMP, LOW);
flag_1m = false;
flag_11m = false;
swipress_1 =false;
}
if(swipress_1 && !flag_page && time_stat[0] == "OFF" && (MT[0]>=1 || HT[0]>=1)) {
start_timer[0]= true;
set_minute[0] = true ;
set_Hours[0] = true;
runn [0]= true;
start_timer[1]= true;
set_minute[1] = true ;
set_Hours[1] = true;
runn [1]= true;
start_timer[2]= true;
set_minute[2] = true ;
set_Hours[2] = true;
runn [2]= true;
time_stat[0] = "SET";
// time_stat[1] = "ON ";
// time_stat[2] = "ON ";
// countdown(0,COOLER);
swipress_1 = false;
}
if(millis() - prev >50){
Display();
prev= millis();
}
}
// /////////////////////////////////////////////
#define same_place 100
// ///////// func for display //////////////////
void lcd_print( String Value, int row , int col ) {
if(row != same_place || col != same_place) lcd.setCursor(col, row);
lcd.print(Value);
}
// /////////////// LCD Display //////////////////
void Display(){
if(!flag_page ){
lcd_print("COOLER:", 0, 0);
lcd_print(time_stat[0] , 0 ,7);
lcd.print(" ");
if(HT[0] < 10) lcd.print("0");
lcd.print(HT[0] ,DEC);
lcd.print(":");
if(MT[0] < 10) lcd.print("0");
lcd.print(MT[0], DEC);
lcd.print(":");
if(ST[0] < 10 ) lcd.print("0");
lcd.print(ST[0],DEC);
lcd_print("Fan: ", 1,0);
lcd_print(time_stat[1] , 1 ,7);
lcd.print(" ");
if(HT[1] < 10) lcd.print("0");
lcd.print(HT[1] ,DEC);
lcd.print(":");
if(MT[1] < 10) lcd.print("0");
lcd.print(MT[1], DEC);
lcd.print(":");
if(ST[1] < 10 ) lcd.print("0");
lcd.print(ST[1],DEC);
lcd_print("PUMP: ", 2,0);
lcd_print(time_stat[2] , 2 ,7);
lcd.print(" ");
if(HT[2] < 10) lcd.print("0");
lcd.print(HT[2] ,DEC);
lcd.print(":");
if(MT[2] < 10) lcd.print("0");
lcd.print(MT[2], DEC);
lcd.print(":");
if(ST[2] < 10 ) lcd.print("0");
lcd.print(ST[2],DEC);
}
if (flag_page ){
lcd_print("...SET the Timer...", 3, 0);
if (selector == 1) {
lcd_print(">", 0, 0);
lcd_print(" ", 1, 0);
lcd_print(" ", 2, 0);
}
lcd_print("T_co: ", 0,1);
lcd_print(time_stat[0] , 0 ,7);
lcd.print(" ");
if(HT[0] < 10) lcd.print("0");
lcd.print(HT[0] ,DEC);
lcd.print(":");
if(MT[0] < 10) lcd.print("0");
lcd.print(MT[0], DEC);
lcd.print(":");
if(ST[0] < 10 ) lcd.print("0");
lcd.print(ST[0],DEC);
if (selector == 2) {
lcd_print(" ", 0, 0);
lcd_print(">", 1, 0);
lcd_print(" ", 2, 0);
}
lcd_print("T_fa: ", 1,1);
lcd_print(time_stat[1] , 1 ,7);
lcd.print(" ");
if(HT[1] < 10) lcd.print("0");
lcd.print(HT[1] ,DEC);
lcd.print(":");
if(MT[1] < 10) lcd.print("0");
lcd.print(MT[1], DEC);
lcd.print(":");
if(ST[1] < 10 ) lcd.print("0");
lcd.print(ST[1],DEC);
if (selector == 3) {
lcd_print(" ", 0, 0);
lcd_print(" ", 1, 0);
lcd_print(">", 2, 0);
}
lcd_print("T_pu: ", 2,1);
lcd_print(time_stat[2] , 2 ,7);
lcd.print(" ");
if(HT[2] <10) lcd.print("0");
lcd.print(HT[2] ,DEC);
lcd.print(":");
if(MT[2] < 10) lcd.print("0");
lcd.print(MT[2], DEC);
lcd.print(":");
if(ST[2] < 10 ) lcd.print("0");
lcd.print(ST[2],DEC);
}
}
// ///////////////// Debounce for swip_setting//////////////////////////
void Debounce() {
if (millis() - lasbutStat_1 >= 50) {
byte buttonState_1 = digitalRead(Set_butt);
if (buttonState_1 == LOW && lastButton_1 == HIGH) { // rising
swipress_1 = true;
lasbutStat_h = millis();
} else if (buttonState_1 == LOW && lastButton_1 == LOW) { // same
if (millis() - lasbutStat_h >= 1000 && same_state_1 == false) {
swipress_1 = false;
runn[0] = false;
set_Hours[0] = false;
set_minute[0] = false;
set_M[0] = false ;
set_H[0] = false ;
runn[1] = false;
set_Hours[1] = false;
set_minute[1] = false;
set_M[0] = false ;
set_H[0] = false ;
runn[2] = false;
set_Hours[2] = false;
set_minute[2] = false;
set_M[0] = false ;
set_H[0] = false ;
selected = false;
selector = false;
flage_t[0] = false;
flage_t[1] = false;
flage_t[2] = false;
flag_page = !flag_page;
same_state_1 = true;
lcd.clear();
}
} else if (buttonState_1 == HIGH && lastButton_1 == LOW) { // failling
same_state_1 = false;
}
lasbutStat_1 = millis();
lastButton_1 = buttonState_1;
}
}
// //////////// Debounce for swip up_down /////////////////
void debounce_1(int button, int index){
if (millis() - lasbutStat[index] >= 50){
byte buttonState = digitalRead(button);
if(buttonState == LOW && lastButton[index] == HIGH){ //rising
swipress[index] = true;
previ = millis();
}
else if(buttonState == LOW && lastButton[index] == LOW && same_state == false ){ //same
swipress[index] = false;
if(millis()-previ >1000){
same_state = true;
}
}
else if (buttonState == HIGH && lastButton[index] == LOW ){ // failling
same_state = false;
}
lasbutStat[index] = millis();
lastButton[index] = buttonState;
}
}
// ////////////////up&down//////////
void up_down (){
if(swipress[0] || (same_state == true && !digitalRead(UP_butt)) ){
if(millis()-prev_up > 50){
up =true;
prev_up = millis();
}
swipress[0] = false ;
}
else if(swipress[1] || (same_state == true && !digitalRead(DOWN_butt))){
if(millis()- prev_dw > 50){
down = true;
prev_dw = millis();
}
swipress[1] = false ;
}
}
// /////////////////COUNTDOWN//////////
void countdown(int indexx ,int butt){
if(!flag_page){
if(start_timer[indexx]==true){
if(HT[indexx] == 0 && MT[indexx] == 0 && ST[indexx] == 0){
flag_dn[indexx] = 1;
Serial.println("DONE EL DONE");
time_stat[indexx] = "DN ";
digitalWrite(butt, LOW);
}
if(MT[indexx]==0 && HT[indexx]>= 1){
MT[indexx]=60;
HT[indexx]=HT[indexx] - 1;
}
if(ST[indexx]==0 && MT[indexx]>=1){
ST[indexx]=60;
MT[indexx]=MT[indexx] - 1;
}
if(millis()-prevtime[indexx] >=1000){
if(ST[indexx] >= 1){
ST[indexx]=ST[indexx] - 1;
digitalWrite(butt, HIGH);
ST[indexx]=constrain(ST[indexx],0,60);
}
prevtime[indexx]=millis();
}
// if(HT[indexx] == 0 && MT[indexx] == 0 && ST[indexx] == 0){
// flag_dn[indexx] = 1;
// time_stat[indexx] = "DN ";
// digitalWrite(butt, LOW);
// if(millis() - prevtimebuzzer >= 500 && !bazzend){
// if (buz_state == LOW) {
// buz_state = HIGH;
// }
// else {
// buz_state = LOW;
// bazzcnt = bazzcnt + 1;
// }
// digitalWrite(buzzer ,buz_state);
// if(bazzcnt == 3){
// bazzend = true;
// bazzcnt = 0;
// digitalWrite(buzzer ,LOW);
// }
// if(bazzend == true){
// time_stat[indexx] = "DN ";
// digitalWrite(butt, LOW);
// }
// prevtimebuzzer = millis();
// }
// }
}
}
}
////////////Timer /////////////////
void timerr(int indexx){
if(flage_t[indexx]){
if (!set_H[indexx] && !set_M[indexx] && !set_minute[indexx] ) time_stat[indexx]="OFF";
if(swipress_1 && flag_page && set_Hours[indexx] == false && set_minute[indexx]== false ){
set_H[indexx] = 1;
Serial.println("H");
swipress_1 =false;
}
if (set_H[indexx]) {
if (up ) {
HT[indexx]++;
up = false;
}
if (down ) {
HT[indexx]--;
down= false;
}
HT[indexx] = constrain(HT[indexx] , 0 , 99);
time_stat[indexx]=" H ";
set_Hours[indexx]=true;
}
if(swipress_1 && flag_page && set_Hours[indexx] == true && !set_M[indexx] && set_minute[indexx]== false ){
Serial.println("M");
set_M[indexx] = 1;
set_H[indexx]= 0;
swipress_1 =false;
}
if (set_M[indexx]) {
if (up) {
MT[indexx]++;
up = false;
}
if (down) {
MT[indexx]--;
down = false;
}
MT[indexx]=constrain(MT[indexx], 0 , 59);
time_stat[indexx]=" M ";
set_minute[indexx]= true;
}
if(swipress_1 && set_minute[indexx]== true && set_Hours[indexx] == true && set_M[indexx] && !set_H[indexx] && !runn[indexx] ){
start_timer[indexx]= true;
set_H[indexx]=0;
set_M[indexx]=0;
set_Hours[indexx] =0;
time_stat[indexx]="SET";
runn[indexx] = true;
swipress_1 =false;
// selected = !selected;
}
}
}
// ////////////////////////////
// ////////////// Variable for Screen1///////////////////
void variable(){
if (up && !selected ){
selector++;
up = false;
}
if (down && !selected ) {
selector--;
down = false;
}
// ////////constrian for control the sel from 1 to 3////////////////
selector = constrain(selector, 1, 3);
// if(flage_t[0]){
// timerr(0);
// // swipress_1 = false;
// }
// if(flage_t[1]){
// timerr(1);
// // swipress_1 = false;
// }
// if(flage_t[2]){
// timerr(2);
// // swipress_1 = false;
// }
}
////////////// press on pages///////////////
void press_page(){
if(swipress_1 && flag_page){
if(selector == 1){
flage_t[0] = !flage_t[0];
swipress_1 = false;
}
if(selector == 2){
flage_t[1] = !flage_t[1];
swipress_1 = false;
}
if(selector == 3){
flage_t[2] = !flage_t[2];
swipress_1 = false;
}
selected = !selected;
}
}