    /* @file Idokapcsolo.ino
        || @version 1.0
        || @author kapu48
        || @contact
        ||
        || @description
        || | Demonstrates changing the keypad size and key values.
        || #
        */
        #include <Keypad.h>
        #include <Wire.h>
        #include "RTClib.h"
        #include <LiquidCrystal_I2C.h>
        #include <Keypad.h>
       
        LiquidCrystal_I2C lcd(0x27, 20, 4);
         RTC_DS1307 RTC;
         int _year, year_;
         int _month, month_;
         int _day=32, day_;
         int _hour, hour_;
         int _minute, minute_;
         int _second=60, second_;
         
         const int REL1 = 7;
         const int REL2 = 8;
         const int REL3 = 9;
         const int REL4 = 10;
         
        const byte ROWS = 4; //four rows
        const byte COLS = 1; //four columns
        //define the cymbols on the buttons of the keypads
        char hexaKeys[ROWS] = {'0','1','2','3'};
         
         
        byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
        byte colPins[1] = {8}; //connect to the column pinouts of the keypad
         
        //initialize an instance of class NewKeypad
        Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
        DateTime now_;
     
     //..............................................................setup()..............................................       
        void setup(){
          Serial.begin(9600);
         
         Wire.begin();
         RTC.begin();
         lcd.init();
         lcd.begin(20, 4);
         lcd.backlight();
         
        digitalWrite(REL1, HIGH);
        digitalWrite(REL2, HIGH);
        digitalWrite(REL3, HIGH);
        digitalWrite(REL4, HIGH);
         
         
        pinMode(REL1, OUTPUT);
        pinMode(REL2, OUTPUT);
        pinMode(REL3, OUTPUT);
        pinMode(REL4, OUTPUT);
         // Ezt csak 1* futatjukle, utánna megjegyzésbe rakjuk és ujra programozzuk
         RTC.adjust(DateTime(2016, 8, 14, 10, 0, 0));
         
        }
     
     //..............................................................loop()..............................................       
        void loop(){
          now_ = RTC.now();
        if(_day != now_.day()){         // LCD frissítés szükséges?       
          _day = now_.day();
          lcd.setCursor(0,0);
          lcd.print(now_.year());
          lcd.print(". ");
          if(now_.month() < 10) lcd.print(" ");
          lcd.print(now_.month());
          lcd.print(". ");
          if(now_.day() < 10) lcd.print(" ");
          lcd.print(now_.day());
          lcd.print(".  ");
          }
          
        if(_second != now_.second()){         // LCD frissítés szükséges? 
          _second = now_.second(); 
          lcd.setCursor(0,1);
          if(now_.hour() < 10) lcd.print(" ");
          lcd.print(now_.hour());
          lcd.print(": ");
          if(now_.minute() < 10) lcd.print(" ");
          lcd.print(now_.minute());
          lcd.print(": ");
          if(now_.second() < 10) lcd.print(" ");
          lcd.print(now_.second());
          lcd.print("   ");
          }
         
          char customKey = customKeypad.getKey();
         
          if (customKey){
            Serial.println(customKey);   
          }
          if (customKey == '0'){
            hour_setting();
          } else if (customKey == '1'){
            minute_setting();
          } else if (customKey == '2'){
            sec_settting();
          }else{date_setting();}
      
        }
         
      //.....................................................hour_setting().......................................................     
         void hour_setting(){
            char keypressed;
     
            hour_ =  now_.hour();
       
            lcd.setCursor(0,0);
            lcd.print("Set hour:          ");   
            lcd.setCursor(0,1);
            lcd.print("Ora =               "); 
            if(hour_ < 10) lcd.print(" ");
            lcd.print(hour_);       
         do {                                                    // ez az�rt kell, hogy addig nyomkodjon, mig jo v�laszt nem ad
               delay(100);
              lcd.setCursor(6,1);
              if(hour_ < 10) lcd.print(" ");
              lcd.print(hour_);
              keypressed =customKeypad.getKey();                   //keypressed v�ltoz� szint�n a setupban kell defini�lni
              if (keypressed=='1')                                //Annyit tudni kell itt, hogy most 1-et �rtam be, de neked pl az 1-es gomb megnyom�s�ra lehet nem ezt adja vissza, hanem azt amit te adt�l meg, sz�val az kell majd neki itt megadni
                {
                  if(hour_ < 24){
                      hour_++;                                 //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                   }else{
                      hour_ = 0;
                   }
                   
                 }
                if (keypressed=='2')                              //Annyit tudni kell itt, hogy most 2-et �rtam be, de neked pl az 2-es gomb megnyom�s�ra lehet nem ezt adja vissza, hanem azt amit te adt�l meg, sz�val az kell majd neki itt megadni
                {
                  if(hour_ > 0){
                      hour_--;                                 //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                   }else{
                      hour_ = 23;
                   }                                //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                 }
               //delay(100);
              } while(keypressed != '3');                         // Azaz m�g nem a h�rmas gombot nyomja
            now_ = RTC.now(); 
            year_ =  now_.year();
            month_ =  now_.month();
            day_ =  now_.day();
            //hour_ =  now_.hour();
            minute_ =  now_.minute();
            second_ =  now_.second();   
             RTC.adjust(DateTime(year_, month_, day_, hour_, minute_, second_));   
             _day=32;         // LCD frissítés kényszerítése
             _second=60;   
         }   
     
      //...........................................................minute_setting().................................................         
         void minute_setting()
         {
            char keypressed;
           
            minute_ =  now_.minute();
           
            lcd.setCursor(0,0);
            lcd.print("Set perc:          ");   
            lcd.setCursor(0,1);
            lcd.print("Perc = "); 
            if(minute_ < 10) lcd.print(" ");
            lcd.print(minute_);       
         do {                                                    // ez az�rt kell, hogy addig nyomkodjon, mig jo v�laszt nem ad
               delay(100);
              lcd.setCursor(7,1);
              if(minute_ < 10) lcd.print(" ");
              lcd.print(minute_);
              keypressed =customKeypad.getKey();                   //keypressed v�ltoz� szint�n a setupban kell defini�lni
              if (keypressed=='1')                                //Annyit tudni kell itt, hogy most 1-et �rtam be, de neked pl az 1-es gomb megnyom�s�ra lehet nem ezt adja vissza, hanem azt amit te adt�l meg, sz�val az kell majd neki itt megadni
                {
                  if(minute_ < 60){
                      minute_++;                                 //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                   }else{
                      minute_ = 0;
                   }
                   
                 }
                if (keypressed=='2')                              //Annyit tudni kell itt, hogy most 2-et �rtam be, de neked pl az 2-es gomb megnyom�s�ra lehet nem ezt adja vissza, hanem azt amit te adt�l meg, sz�val az kell majd neki itt megadni
                {
                  if(minute_ > 0){
                      minute_--;                                 //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                   }else{
                      minute_ = 59;
                   }                                //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                 }
               //delay(100);
              } while(keypressed != '3');                         // Azaz m�g nem a h�rmas gombot nyomja     
           now_ = RTC.now();
           year_ =  now_.year();
            month_ =  now_.month();
            day_ =  now_.day();
            hour_ =  now_.hour();
            //minute_ =  now_.minute();
            second_ =  now_.second();   
             RTC.adjust(DateTime(year_, month_, day_, hour_, minute_, second_));
              _day=32;         // LCD frissítés kényszerítése
             _second=60;
         }   
           
           
     //............................................................sec_settting()................................................     
        void sec_settting()
         {
            char keypressed;
           
            second_ =  now_.second();
           
            lcd.setCursor(0,0);
            lcd.print("Set second:         ");   
            lcd.setCursor(0,1);
            lcd.print("mPerc = ");
            if(second_ < 10) lcd.print(" ");
            lcd.print(second_);         
         do {                                                    // ez az�rt kell, hogy addig nyomkodjon, mig jo v�laszt nem ad
               delay(100);
              lcd.setCursor(8,1);
              if(second_ < 10) lcd.print(" ");
              lcd.print(second_);
              keypressed =customKeypad.getKey();                   //keypressed v�ltoz� szint�n a setupban kell defini�lni
              if (keypressed=='1')                                //Annyit tudni kell itt, hogy most 1-et �rtam be, de neked pl az 1-es gomb megnyom�s�ra lehet nem ezt adja vissza, hanem azt amit te adt�l meg, sz�val az kell majd neki itt megadni
                {
                  if(second_ < 60){
                      second_++;                                 //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                   }else{
                      second_ = 0;
                   }
                   
                 }
                if (keypressed=='2')                              //Annyit tudni kell itt, hogy most 2-et �rtam be, de neked pl az 2-es gomb megnyom�s�ra lehet nem ezt adja vissza, hanem azt amit te adt�l meg, sz�val az kell majd neki itt megadni
                {
                  if(second_ > 0){
                      second_--;                                 //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                   }else{
                      second_ = 59;
                   }                                //A sec v�ltoz�t a setupban int k�nt kell defini�lni
                 }
               //delay(100);
              } while(keypressed != '3');                         // Azaz m�g nem a h�rmas gombot nyomja     
            now_ = RTC.now();
            year_ =  now_.year();
            month_ =  now_.month();
            day_ =  now_.day();
            hour_ =  now_.hour();
            minute_ =  now_.minute();
            //second_ =  now_.second();   
            RTC.adjust(DateTime(year_, month_, day_, hour_, minute_, second_));   
             _day=32;         // LCD frissítés kényszerítése
             _second=60;
      }
       //............................................................date_settting()................................................
    void date_setting(){
      // TODO!
            char keypressed; 
           
            lcd.setCursor(0,0);
            lcd.print("Set date:           ");   
            lcd.setCursor(0,1);
           
            delay(100);
            //year_ = 
     
            //month_ = 
     
            //day_ =
           
            now_ = RTC.now();
           
            //year_ =  now_.year();
            //month_ =  now_.month();
            //day_ =  now_.day();
            hour_ =  now_.hour();
            minute_ =  now_.minute();
            second_ =  now_.second();   
            RTC.adjust(DateTime(year_, month_, day_, hour_, minute_, second_)); 
             _day=32;         // LCD frissítés kényszerítése
             _second=60;
    }
      

     
   
