////////////////////////////////////////////////////////////////////////////////
// Name:       FloraGardenWatering01                                          //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=18             //
//             Robot that waters the plants in my garden                      //
// Platform:   Arduino Uno Rev3                                               //
// Created by: HARB rboek2@gmail.com may 2018 GPL copyrights                  //
////////////////////////////////////////////////////////////////////////////////



// SET PRECOMPILER OPTIONS *****************************************************
//Define the needed header files for the precompiler, no charge if not used ----
#include <DS1302.h>                  //Thanks to copyright (c) 2009, Matt Sparks
#include <EveryTimer.h>          //Thanks to copyright (c) 2018 Alessio Leoncini

// Define precompiler variables ------------------------------------------------
//Declare pins
#define kCePin       4                          //DS1302 Chip Enable, orange RST
#define kSclkPin     5                         //DS1302  Serial Clock, white CLK
#define kIoPin       6                         //DS1302 Input/Output, yellow DAT
#define pinLedRed    9                               //Pin 9 is PWM port for red
#define pinLedGreen 10                            //Pin 10 is PWM port for green
#define pinLedBlue  11                             //Pin 11 is PWM port for blue
#define pinRelay1   A0                       //Use pin A0 as DIO port for relay1
#define pinRelay2   A1                       //Use pin A1 as DIO port for relay2
#define pinRelay3   A2                       //Use pin A2 as DIO port for relay3
#define pinRelay4   A3                       //Use pin A3 as DIO port for relay4
//End of pin declarations
#define PERIOD_MS 5000 //Defines time before the interrupt is called = 5 seconds
EveryTimer timer;      //Used to set up an interrupted timer to switch waterpump
DS1302 rtc(kCePin, kIoPin, kSclkPin);                  //DS1302 create an object
//END OF PRECOMPILER OPTIONS ---------------------------------------------------



//DECLARE VARIABLES ************************************************************
bool   ledOnBoardVal = LOW;         //Choose HIGH=on or LOW=off for on-board LED
word   counter1      =   0;    //Counter to be used in subroutines to count down
String propaclock1;  //DS1302 without time-zone format PgSql yyyy-mm-dd hh:mm:ss
int    currentHour;                     //As in timetxt to compare with readHour
int    readHour;                        //As in t.hr to compare with currentHour
char   buf[50];                                          //Needed to read DS1302
//END OF DECLARE VARIABLES -----------------------------------------------------



void setup() { //Setup runs once ***********************************************
  disableJtag();   //Disable jtag to free port C, enabled by default in some ATs
  Serial.begin(57600);  //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  digitalWrite(LED_BUILTIN, LOW);               //Arduino onboard LED switch OFF

  pinMode (pinLedRed, OUTPUT);                               //LED 3 colors: RED
  digitalWrite(pinLedRed, LOW);                           //Red HIGH=on, LOW=off
  pinMode (pinLedGreen, OUTPUT);                           //LED 3 colors: GREEN
  digitalWrite(pinLedGreen, LOW);                       //Green HIGH=on, LOW=off
  pinMode (pinLedBlue, OUTPUT);                             //LED 3 colors: BLUE
  digitalWrite(pinLedBlue, LOW);                         //Blue HIGH=on, LOW=off
 
  pinMode(pinRelay1, OUTPUT);                  //Set pin relay1 as an output pin
  digitalWrite(pinRelay1, HIGH);        //Set output pin relay1 HIGH=OFF, low=on
  pinMode(pinRelay2, OUTPUT);                  //Set pin relay2 as an output pin
  digitalWrite(pinRelay2, HIGH);        //Set output pin relay2 HIGH=OFF, low=on
  pinMode(pinRelay3, OUTPUT);                  //Set pin relay3 as an output pin
  digitalWrite(pinRelay3, HIGH);        //Set output pin relay3 HIGH=OFF, low=on
  pinMode(pinRelay4, OUTPUT);                  //Set pin relay4 as an output pin
  digitalWrite(pinRelay4, HIGH);        //Set output pin relay4 HIGH=OFF, low=on

  rtc.writeProtect(false);                                              //DS1302
  rtc.halt(false);                                                      //DS1302
  DS1302setSystemTime();                       //Read system time and set DS1302

  timer.Every(PERIOD_MS, toggleLedOnBoard);           //Schedule interrupt timer
    
  testLeds(100);        //Turn on and off all colors of 3 color LED for (waitms)
  //testRelais(1000);           //Turn on and off all relais 1 time for (waitms)
  //Serial.println("Setup finished");
}//--(end setup )---------------------------------------------------------------



void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  // 1) FIRST STEP IS TO READ ALL SENSORS --------------------------------------
  readTime();        //DS1302 Reading the time and format results into variables
  timer.Update();                        //Refresh interrupt call back mechanism
  // 2) SECOND STEP IS TO CALCULATE THE DESIRED SETTING OF THE SWITCHES --------
  // 3) THIRD STEP IS TO SET THE SWITCHES AS DESIRED ---------------------------
  // 4) LAST STEP IS TO COMMUNICATE TO THE SERIAL PORT AND FINISH LOOP ---------
  //Serial.println(buf);                  //Show the current time in the monitor
  pumpWater(10);                //Turn on the waterpump for (x) seconds = relay2
  delayMinutes(120);                          //Do nothing for (minutes) minutes
  //toggleLedOnBoard();         //Toggles the LED_BUILTIN on-board LED on or off
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER



////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO                                          //
// Board -Atmel- PIN - IDE - Function          - Connection               ALT //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  -                          TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  -                          TWI //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - LED_BUILTIN Arduino      SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       -                          SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - LED 3-color blue         PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - LED 3-color green        PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       - LED 3-color red          PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  -                          DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      -                          DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                          PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                          PWM //
// ~4    -   6 - PD4 -   4 - PCINT20/XCK/T0    - DS1320 RST Green         PWM //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 - DS1320 DAT Blue          INT //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      - DS1320 CLK Yellow        INT //
// TX->1 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor           TXD //
// RX<-0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor           RCD //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V    -   7 - VCC -      - VCC              - Output to breadboard     VCC //
// RES   -   1 - RES -      - PCINT14/RESET    -                          RES //
// 3.3V  -     -     -     -                   -                              //
// 5V    -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// Vin   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       -                          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       -                          ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      -                          ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      -                          ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  -                          TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  -                          TWI //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP in 8 bits bytes:                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500)                          //
// On-Chip Debug Enabled: off                            (OCDEN=0)            //
// JTAG Interface Enabled: off                           (JTAGEN=0)           //
// Preserve EEPROM mem through the Chip Erase cycle: On  (EESAVE = 0)         //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00)          //
// Boot Reset vector Enabled, default address=$0000      (BOOTSTR=0)          //
// CKOPT fuse (operation dependent of CKSEL fuses        (CKOPT=0)            //
// Brown-out detection level at VCC=2,7V;                (BODLEVEL=1)         //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11)  //
// LOCKBITS (are dangerous to change, since they cannot be reset)             //
// Mode 1: No memory lock features enabled                                    //
// Application Protect Mode 1: No lock on SPM and LPM in Application Section  //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section  //
////////////////////////////////////////////////////////////////////////////////



void disableJtag(void) { //Disable jtag to free port C, enabled by default *****
#if defined(JTD)                           //Not all AVR controller include jtag
  MCUCR |= ( 1 << JTD );                                //Write twice to disable
  MCUCR |= ( 1 << JTD );                                       //So stutter once
#endif                                            //End of conditional compiling
} //Exit disableJtag -----------------------------------------------------------



void toggleLedOnBoard(void){ //Toggles the LED_BUILTIN on-board LED on or off **
  ledOnBoardVal = !ledOnBoardVal;                                 //Toggle value
  digitalWrite(LED_BUILTIN, ledOnBoardVal);     //Set Arduino boards onboard LED
} //Exit toggleLedOnBoard ------------------------------------------------------



void DS1302setSystemTime(void) { //Read system time and set DS1302 *************
  String strTime = (__TIME__);              //Read the system time into a string
  String hoursSet = strTime.substring(0, 2); //Select the current hour as string
  int hours = hoursSet.toInt();     //Convert the string to an integer as needed
  String minutesSet = strTime.substring(3, 5);      //Select the current minutes
  int minutes = minutesSet.toInt(); //Convert the string to an integer as needed
  String secondsSet = strTime.substring(6, 8);      //Select the current seconds
  int seconds = secondsSet.toInt(); //Convert the string to an integer as needed
  Time t(2018, 05, 23, hours, minutes, seconds, Time::kWednesday); //Time object
  rtc.time(t);            //Set the date (manual) time (automatic) on the DS1302
} //Exit DS1302setSystemTime ---------------------------------------------------



void delayMinutes(word minutes){ //Do nothing for (minutes) minutes ************
  while (minutes > 0){                      //Test if we have waited long enough
    minutes--;                        //Count down the number of minutes desired
    counter1 = 60;           //10 means that blue LED will flash every 6 seconds
    while (counter1 > 0){                 //Test if we have counted down to zero
      counter1--;                               //Equals counter1 = counter1 - 1
      delay(1000);                                        //This waits 6 seconds
      timer.Update();                    //Refresh interrupt call back mechanism
      flashLedBlue(10);               //Signal that program is running waitstate
    }                                    //Try again if counter1 is already zero
  }                                       //Try again if minutes is already zero
} //Exit delayMinutes ----------------------------------------------------------



void readTime(){ //DS1302 Reading the time and format results into variables ***
   Time t = rtc.time();            //Get the current time and date from the chip
   snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",       //Format as
           t.yr, t.mon, t.date,       //Desired by the Pgsql timestamp data type
           t.hr, t.min, t.sec);
   currentHour = t.hr;            //Used to calculate the grow light time switch
   propaclock1 = buf ;              //Used to export a timestamp to the database
} //End of readTime DS1302 Reading the time and format results into variables --



void pumpWater(word seconds){ //Turn on relay 2 for (seconds) seconds **********
  digitalWrite(pinLedGreen, HIGH);                      //Green HIGH=on, LOW=off
  //digitalWrite(pinRelay2, LOW);                        //HIGH=off, LOW=on Relay2
  while (seconds > 0){                    //Test if we have counted down to zero
      seconds--;                                  //Equals seconds = seconds - 1
      delay(1000);                                        //This waits 1 seconds
      timer.Update();                    //Refresh interrupt call back mechanism
  }                                       //Try again if seconds is already zero
  //digitalWrite(pinRelay2, HIGH);                       //HIGH=off, LOW=on Relay2
  digitalWrite(pinLedGreen, LOW);                       //Green HIGH=on, LOW=off
} //Exit pumpWater -------------------------------------------------------------



void flashLedGreen(word waitms){ //Signal that program is running normally *****
  digitalWrite(pinLedGreen, HIGH);                      //Green HIGH=on, LOW=off
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinLedGreen, LOW);                       //Green HIGH=on, LOW=off
} //Exit flashLedGreen ---------------------------------------------------------



void flashLedBlue(word waitms){ //Signal that program is running waitstate *****
  digitalWrite(pinLedBlue, HIGH);                        //Blue HIGH=on, LOW=off
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinLedBlue, LOW);                         //Blue HIGH=on, LOW=off
} //Exit flashLedBlue ----------------------------------------------------------



void testLeds(word waitms){ //Turn on and off all colors of 3 color LED ********
  digitalWrite(pinLedRed, HIGH);                          //Red HIGH=on, LOW=off
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinLedRed, LOW);                           //Red HIGH=on, LOW=off
  digitalWrite(pinLedGreen, HIGH);                      //Green HIGH=on, LOW=off
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinLedGreen, LOW);                       //Green HIGH=on, LOW=off
  digitalWrite(pinLedBlue, HIGH);                        //Blue HIGH=on, LOW=off
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinLedBlue, LOW);                         //Blue HIGH=on, LOW=off
} //Exit testLeds --------------------------------------------------------------



void testRelais(word waitms){ //Turn on and off all relais 1 time **************
  digitalWrite(pinRelay1, LOW);                        //HIGH=off, LOW=on Relay1
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinRelay1, HIGH);                       //HIGH=off, LOW=on Relay1
  digitalWrite(pinRelay2, LOW);                        //HIGH=off, LOW=on Relay2
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinRelay2, HIGH);                       //HIGH=off, LOW=on Relay2
  digitalWrite(pinRelay3, LOW);                        //HIGH=off, LOW=on Relay3
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinRelay3, HIGH);                       //HIGH=off, LOW=on Relay3
  digitalWrite(pinRelay4, LOW);                        //HIGH=off, LOW=on Relay4
  delay(waitms);                                       //Test your patience here
  digitalWrite(pinRelay4, HIGH);                       //HIGH=off, LOW=on Relay4
} //Exit testRelais ------------------------------------------------------------