////////////////////////////////////////////////////////////////////////////////
// Name:       RO-042                                                         //
//             SD card test                                                   //
// Platform:   Arduino Mega 2560                                              //
// Created by: HARB rboek2@gmail.com march 2017 GPL copyrights                //
// Thanks to:  Library created 2011 by Limor Frie, modified 2012 by Tom Igoe  //
//             https://github.com/PaulStoffregen/OneWire                      //
//             <DS1302.h> Copyright (c) 2009, Matt Sparks                     //
// http://robotigs.com/robotigs/includes/parts_header.php?idpart=116          //
////////////////////////////////////////////////////////////////////////////////


// SET PRECOMPILER OPTIONS *****************************************************

//Set system targets -----------------------------------------------------------
//Propagating Strawberry Hybrid:      propatemp=24, propahours=16, propahumid=90
//Propagating Strawberry Hybrid Phase2:         Temp=18, Hours=14, propahumid=90
//Propagating Strawberry Hybrid Phase2:         Temp=18, Hours=14, propahumid=90
float propswitmp1 = 20.0;        //Switching temperature in the propagator +-1dC
float propahours = 12.0; //Number of hours around noon to switch on LED lighting
word  propamoist = 320;         //Measurement under which to signal water needed

//Define the needed header files for the precompiler, no charge if not used ----
#include <stdio.h>                //http://quadpoint.org/projects/arduino-ds1302
#include <DS1302.h>                            //Copyright (c) 2009, Matt Sparks
#include <SPI.h>     //Serial Peripheral Interface requiered by SD card software
#include <SD.h>                                             //Include SD library
#include <OneWire.h> //Library can be installed through Arduino IDE menu DS18B20
OneWire ds(2);      //DS1820 is connected to pin 2, but may be any DIO input pin
#include <DHT.h>                     //DHT22/11 Temperature and humidity sensors

// Define precompiler variables ------------------------------------------------
File root;             //Set up variables using the SD utility library functions
#define DHTPIN 3                        //What digital pin is DHTXX connected to
//#define DHTPIN1 12                    //What digital pin is DHTXX connected to
//#define DHTPIN2 A0                    //What digital pin is DHTXX connected to
//#define DHTTYPE DHT11            //DHT 11 Uncomment whatever type you're using
//#define DHTTYPE DHT21                                        //DHT 21 (AM2301)
#define DHTTYPE DHT22                                 //DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);                               //Initialize DHT sensor1
//DHT dht1(DHTPIN1, DHTTYPE);                           //Initialize DHT sensor2
//DHT dht2(DHTPIN2, DHTTYPE);                           //Initialize DHT sensor3

//Declare variables ------------------------------------------------------------
bool ledBinVal   = LOW;             //Choose HIGH=on or LOW=off for on-board LED
byte addr[8];    //Array with the first 8 bytes of DS1820 ROM, including address
byte i;                                        //Used in DS18B20 initialisiation
byte type_s = 0; //0 always ok, except old DS1820 = 1, results in different calc
byte present = 0;               //Used for DS18B20 oneWire, present = ds.reset()
byte data[12];                              //Used during DS18B20 result reading
bool moistSwitch1 = HIGH;       //Needed for RO-043 moisture HIGH=L>R or LOW=R<L

String html;              //HTML Response preapaired and counted into bodylength
unsigned int bodyLength;                                    //HTML answer length

float  propairtmp1;   //Air temperature measured by DHT22 in degree Celsius 99.9
float  propairhum1;          //Air humidity measured by DHT22 in percentage 99.9
float  propboxtmp1;      //Switchbox soil temperature in Celsius by DS18B20 99.9
word   propboxldr1;                  //Switchbox read LDR light intensity 0-1023
word   propboxmoi1;      //Switchbox soil humidity measurement left pin 0 - 1023
word   propboxmoi2;     //Switchbox soil humidity measurement right pin 0 - 1023
String propaclock1;  //DS1302 without time-zone format PgSql yyyy-mm-dd hh:mm:ss

word moist;
word water;
float hic0;
float dew0;
float airtemp1;
float humidity1;
float hic1;
float dew1;
float airtemp2;
float humidity2;
float hic2;
float dew2;

bool lightSwitch = HIGH;       //Choose HIGH=off or LOW=on for LED strip, Relay1
bool airSwitch   = HIGH;      //Choose HIGH=off or LOW=on for Air heater, Relay2
bool soilSwitch  = HIGH;  //Set HIGH=off or LOW=on for Propagator heater, Relay3
bool waterSwitch = HIGH;       //Choose HIGH=off or LOW=on for Waterpump, Relay4


int starthours = 12 - (propahours / 2);         //Used to switch clock switch ON
int finishhours = 12 + (propahours / 2);       //Used to switch clock switch OFF
int currenthour;      //As in timetxt to compare with starthours and finishhours
char buf[50];                                            //Needed to read DS1302
String record;                     //Needed to store all data to print and write
const int kSclkPin = 38;                      //DS1302  Serial Clock, Yellow CLK
const int kIoPin   = 40;                         //DS1302 Input/Output, Blue DAT
const int kCePin   = 42;                         //DS1302 Chip Enable, Green RST
DS1302 rtc(kCePin, kIoPin, kSclkPin);                   //Create a DS1302 object
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  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
  analogReference(DEFAULT);           //Option may be different for other boards
  
  pinMode(53, OUTPUT);          //SD card CS 53 is hardware default on mega 2560

  pinMode (44, OUTPUT);                                      //LED 3 colors: RED
  digitalWrite(44, LOW);                                  //Red HIGH=on, LOW=off
  pinMode (45, OUTPUT);                                     //LED 3 colors: BLUE
  digitalWrite(45, LOW);                                 //Blue HIGH=on, LOW=off
  pinMode (46, OUTPUT);                                    //LED 3 colors: GREEN
  digitalWrite(46, LOW);                                //Green HIGH=on, LOW=off
    
  pinMode(22, OUTPUT);            //LEDstrip lighting pin as output GREEN Relay1
  digitalWrite(22, HIGH);                      //LEDstrip HIGH=OFF low=on Relay1
  pinMode(24, OUTPUT);                            //1000W Air Heater BLUE Relay2
  digitalWrite(24, HIGH);                     //1000W Air Heater HIGH=off Relay2
  pinMode (26, OUTPUT);       //Define propagator heater as output ORANGE Relay3
  digitalWrite(26, HIGH);                  //35W propagator heater LOW=on Relay3
  pinMode (28, OUTPUT);              //Define  waterpump as output YELLOW Relay4
  digitalWrite(28, HIGH);                            //Waterpump HIGH=off Relay4
  
  //Serial.print("\nInitializing SD card... ");
  if (!SD.begin()) {     //Change to match the CS of your SD card if not default
    //Serial.println("initialization failed!");
  }else{
    //Serial.println("Done.");
    root = SD.open("/");
  }

  if (SD.exists("datalog.txt")) {
    //Serial.println("datalog.txt exists.");
  } else {
    //Serial.println("datalog.txt doesn't exist.");
    // open a new file and immediately close it:
    //Serial.println("Creating datalog.txt...");
    root = SD.open("datalog.txt", FILE_WRITE);
  }
  //printDirectory(root, 0);
  root.close();                                    //End of SD card initialising
  //Serial.println("SETUP SD CARD DONE");
  //Serial.println(" ");
  
  DS1820_init();                                  //Determins the type of DS1820
  //Serial.println("SETUP DS18B20 DONE");
  //Serial.println(" ");

  rtc.writeProtect(false);                                              //DS1302
  rtc.halt(false);                                                      //DS1302
  //Time t(2017, 08, 14, 13, 55, 00, Time::kMonday);     //UNCOMMENT TO SET TIME
  //rtc.time(t); //Set the time and date on the chip   UNCOMMENT TO SET THE TIME
  //Serial.println("SETUP DS1302 DONE");
  //Serial.println(" ");

  dht.begin();                                                //Start DHT sensor
  //dht1.begin();                                             //Start DHT sensor
  //dht2.begin();                                             //Start DHT sensor
  //Serial.println("SETUP DHT22 DONE");
  //Serial.println(" ");
  
  //Serial.println(" ");
  //Serial.println("SETUP DONE, go looping");
  //Serial.println(" ");
  //delay(5000);                //Test your patience here to protct PC AT relais
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  // 1) FIRST STEP IS TO READ ALL SENSORS --------------------------------------
  analogWrite(46, 5);                                   //Green HIGH=on, LOW=off
  propairtmp1 = dht.readTemperature();       //DHT22 read temperature as Celsius
  propairhum1 = dht.readHumidity();//DHT22 reading sensor takes 250 milliseconds
  DS1820_read();             //Switchbox read temperature from DS1820 in Celsius
  read_moisture();     //Switchbox read the moisture of the soil in 2 directions
  propboxldr1 = analogRead(A2);                 //Switchbox read LDR light brick
  readTime();        //DS1302 Reading the time and format results into variables
  digitalWrite(46, LOW);                                //Green HIGH=on, LOW=off

  if (waterSwitch == LOW){
     //analogWrite(44, 60);                               //Red HIGH=on, LOW=off
     delay(20);
  }else{
    //digitalWrite(44, LOW);                              //Red HIGH=on, LOW=off
  }
  
  // 2) SECOND STEP IS TO CALCULATE THE DESIRED SETTING OF THE SWITCHES --------
  calculate_lightswitch();  //Calculate lightSwitch ON or OFF according to clock
  calculate_soilswitch();           //Calculate builtin heater of the propagator
  calculate_waterswitch();               //Calculate waterpump of the propagator
    
  // 3) THIRD STEP IS TO SET THE SWITCHES AS DESIRED ---------------------------
  switch_light();   //Switch the LED lighting on or off according to lightSwitch
  switch_propagator();                     //Switch the heater of the propagator
  
  // 4) LAST STEP IS TO COMMUNICATE TO THE SERIAL PORT AND FINISH LOOP ---------
  create_data_record();         //Join all readings and settings into one string
  write_SD_card();           //Add a record with all current data to the SD card
  http_check();              //See if we received a http request and reply if so
  toggle_ledBin();                  //Toggles the default on-board LED on or off
  delay(1000);                  //Test your patience here to protct PC AT relais
  digitalWrite(44, LOW);                                  //Red HIGH=on, LOW=off
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER


void create_data_record(void){  //Join all readings and settings into one string
  html = "<!DOCTYPE HTML>";        //Always start our HTML answer with this line
  html += "<html><head><title>Propagator</title></head><body>";    //Normal page
  html += "propairtmp1=";
  html += String(propairtmp1, 1);
  html += "&propairhum1=";
  html += String(propairhum1, 1);
  html += "&propboxtmp1=";
  html += String(propboxtmp1, 1);
  html += "&propboxmoi1=";
  html += propboxmoi1;
  html += "&propboxmoi2=";
  html += propboxmoi2;
  html += "&propboxldr1=";
  html += propboxldr1;
  html += "&propaclock1=";
  html += propaclock1;
  html += "</body></html>";                              //Terminating HTML page
} //Exit create_data_record ----------------------------------------------------

void read_moisture(void) { //Reads the moisture of the soil ********************
  if (moistSwitch1) {
    pinMode(A0, INPUT);
    pinMode(A1, OUTPUT);
    digitalWrite(A1, HIGH);
    delay(2);
    propboxmoi1 = analogRead(A0);                                      //Moisture
    digitalWrite(A1, LOW);
  }else{
    pinMode(A1, INPUT);
    pinMode(A0, OUTPUT);
    digitalWrite(A0, HIGH);
    delay(2);
    propboxmoi2 = analogRead(A1);                                      //Moisture
    digitalWrite(A0, LOW);
  }
  moistSwitch1 = !moistSwitch1;                      //Toggle value moistSwitch1
} //Exit read_moisture ---------------------------------------------------------


void http_check(void) { //See if we received a http request and reply if so ****
  String Request = "";                                    //Reset receive string
  while (Serial.available() > 0) {   //Check if any request is made by a browser
    Request = String(Request + Serial.readString());   //Read incoming character
  } //End of if (Serial.available() > 0)              Entire block has been read
  if (Request != "") {                         //Did I really receive a request?
    //Serial.println(Request);                                //Show the request
    bodyLength = html.length();  //Calculate the number of characters to be sent
    bodyLength = bodyLength + 1;                            //Add the basic text
    Serial.println("HTTP/1.1 200 OK");                   //Answer to the request
    Serial.println("Connection: close");          //Close after html is finished
    Serial.print("Content-Length: ");        //Finish html after amount of chars
    Serial.println (bodyLength);
    Serial.println("Content-Type: text/html");         //Needed to be compatible
    Serial.println(" /n \n");                //Needed to end the headers somehow
    Serial.println(html);                                         //Entire tekst
    //READ THE SETTINGS FROM THE REQUEST 
    //tempStr = Request.substring(11,12);//ANALYZE PRG1 0=OFF 1=ON 2=AUTO 3=Unknwn
    //if (tempStr == "3") {  //Change nothing since 3 means unknown program status
    //}else{
    //  prgVal1 = tempStr;                                //Set to desired program
    //}
  } //End of if (inStri <> "")
} //Exit http_check, end of See if we received a http request and reply if so---


void write_SD_card(void){ //Add a record with all current data to the SD card **
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {                        //If the file is available, write to it
    dataFile.println(html);
    dataFile.close();
  }else{                               //If the file isn't open, pop up an error
    Serial.println("Error opening datalog.txt");
  }
} //Exit write_SD_card ---------------------------------------------------------


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


void calculate_lightswitch() { //Calculate lightSwitch according to clock ******
  if (currenthour < starthours){
      lightSwitch = HIGH;            //Switch the grow LEDs off, HIGH=off LOW=on
   }
   if (currenthour > finishhours){
      lightSwitch = HIGH;            //Switch the grow LEDs off, HIGH=off LOW=on
   }
   if (currenthour > (starthours-1) && currenthour <(finishhours)){
      lightSwitch = LOW;             //Switch the  grow LEDs on, HIGH=off LOW=on
   }
} //Exit calculate_lightswitch -------------------------------------------------


void calculate_soilswitch() { //Calculate builtin heater of the propagator *****
  if (propboxtmp1 < propswitmp1 - 1){          //If tmp comes 1 below switchtemp
      soilSwitch = LOW;          //Switch the builtin heater on, HIGH=off LOW=on
  }
  if (propboxtmp1 > propswitmp1 + 1){        //If temp reaches 1 plus switch OFF
      soilSwitch = HIGH;        //Switch the builtin heater off, HIGH=off LOW=on
   }
} //Exit calculate_soilswitch --------------------------------------------------


void calculate_waterswitch() { //Calculate water pump of the propagator ********
  if (propboxmoi1 < propamoist){          //If moisture reaches below switch value
      waterSwitch = LOW;             //Switch the water pump on, HIGH=off LOW=on
  }
  if (propboxmoi1 > propamoist + 10){  //If moist reaches +10 switch the alarm OFF
      waterSwitch = HIGH;           //Switch the water pump off, HIGH=off LOW=on
   }
} //Exit calculate_waterswitch -------------------------------------------------




void switch_light() { //Switch the LED lighting according to lightSwitch *******
    if (lightSwitch == LOW){          //SWITCH THE LED STRIP GROW LIGHT, RELAY 1
      digitalWrite(22, LOW);  //So turn the lighting on, Relay1 HIGH=off, LOW=on
   }else{
      digitalWrite(22, HIGH);      //So switch the lighting off, Relay1 HIGH=off
   } //End of (lightSwitch == LOW){              SWITCH THE LED STRIP GROW LIGHT
} //Exit switch_light ----------------------------------------------------------


void switch_propagator() { //Switch the heater of the propagator ***************
    if (soilSwitch == LOW){           //SWITCH THE LED STRIP GROW LIGHT, RELAY 2
      digitalWrite(24, LOW);    //So turn the heater on, Relay1 HIGH=off, LOW=on
   }else{
      digitalWrite(24, HIGH);        //So switch the heater off, Relay1 HIGH=off
   } //End of (soilSwitch == LOW){           SWITCH THE HEATER OF THE PROPAGATOR
} //Exit switch_propagator -----------------------------------------------------

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 to the database
} //End of DS1302 Reading the time and format results into variables -----------


void DS1820_read(void) {          //Reads the temperature from DS1820 in Celcius
  ds.reset();
  ds.select(addr);
  ds.write(0x44);         // start conversion, with parasite power on at the end    
  delay(800);                                // maybe 750ms is enough, maybe not
  present = ds.reset(); 
  ds.select(addr);    
  ds.write(0xBE);                                             // Read Scratchpad
  for ( i = 0; i < 9; i++) {                                  // we need 9 bytes
    data[i] = ds.read();
  }
  int16_t raw = (data[1] << 8) | data[0];
  propboxtmp1 = (float)raw / 16.0;
}


double dewPoint(double celsius, double humidity) { //Calculate dewpoint ********
//John Main added dewpoint code from: http://playground.arduino.cc/main/DHT11Lib
//Also added DegC output for Heat Index.
//dewPoint function NOAA
//ref1: http://wahiduddin.net/calc/density_algorithms.htm
//ref2: http://www.colorado.edu/geography/weather_station/Geog_site/about.htm

  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.246);
  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;
  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078);                                      //Temp var
  return (241.88 * T) / (17.558 - T);
} //End of double dewPoint(double celsius, double humidity) --------------------

void DS1820_init(void) {                          //Determins the type of DS1820
  if ( !ds.search(addr)) {
    //Serial.println("No more addresses.");
    //Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
  //Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    //Serial.write(' ');
    //Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      //Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 
  switch (addr[0]) {   // the first ROM byte indicates which chip
    case 0x10:
      //Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      //Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      //Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      //Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  //Serial.print("  Data = ");
  //Serial.print(present, HEX);
  //Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    //Serial.print(data[i], HEX);
    //Serial.print(" ");
  }
  //Serial.print(" CRC=");
  //Serial.print(OneWire::crc8(data, 8), HEX);
  //Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  propboxtmp1 = (float)raw / 16.0;
  //fahrenheit = celsius * 1.8 + 32.0;
  //Serial.print("  Temperature = ");
  //Serial.print(celsius);
  //Serial.print(" Celsius, ");
  //Serial.print(fahrenheit);
  //Serial.println(" Fahrenheit");
}

void printDirectory(File dir, int numTabs) {
   while(true) {
     File entry =  dir.openNextFile();
     if (! entry) {   //No more files, return to the first file in the directory
       dir.rewindDirectory();
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
   }
}
////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO MEGA 2560                                    //
// Board  -Atmel- PIN - IDE - Function          - External Connection    FUNC //
//                                                                            //
// 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   -  31 - REF -     - AREF              -                         REF //
// GND    -  32 - GND -     - GND               -                         GND //
// 13 PWM -  26 - PB7 -  13 - OC0A/OC1C/PCINT17 - LED Arduino LED_BUILTIN PWM //
// 12 PWM -  18 - PB6 -  12 - OC1B/PCINT16      -                         PWM //
// 11 PWM -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  -                         PWM //
// 10 PWM -  16 - PB2 -  10 - SS/OC1B/PCINT2    -                         PWM //
//  9 PWM -  15 - PB1 -   9 - OC1A/PCINT1       -                         PWM //
//  8 PWM -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  -                         DIO //
//                                                                            //
// CONNECTIONS RAILS TOP MIDDLE: DIGITAL PWM<~> ***************************** //
//  7 PWM -  13 - PD7 -   7 - PCINT23/AIN1      -                         PWM //
//  6 PWM -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                         PWM //
//  5 PWM -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                         PWM //
//  4 PWM -   6 - PD4 -   4 - PCINT20/XCK/T0    -                         PWM //
//  3 PWM -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                         PWM //
//  2 PWM -   4 - PD2 -   2 - PCINT18/INT0      -                         INT //
//  1 TX0 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor          TX0 //
//  0 RX0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor          RC0 //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 14 TX3 -  13 - PD7 -   7 - PCINT23/AIN1      -                         DIO //
// 15 RX3 -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                         PWM //
// 16 TX2 -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                         TX2 //
// 17 RX2 -   6 - PD4 -   4 - PCINT20/XCK/T0    -                         RX2 //
// 18 TX1 -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                         INT //
// 19 RX1 -   4 - PD2 -   2 - PCINT18/INT0      -                         INT //
// 20 SDA -   3 - PD1 -   1 - PCINT17/TXD       -                         TWI //
// 21 SCL -   2 - PD0 -   0 - PCINT16/RCD       -                         TWI //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V     -   1 - RES -     - PCINT14/RESET     -                         RES //
// 3.3V   -     -     -     -                   - SD Card power supply        //
// 5V     -     -     -     -                   -                             //
// GND    -     -     -     -                   -                             //
// GND    -     -     -     -                   -                             //
// Vin    -     -     -     -                   -                             //
//                                                                            //
// CONNECTIONS RAILS BOTTOM CENTER: ANALOG IN ******************************* //
// A0     -  23 - PC0 -A0/14- ADC0/PCINT8       - Moisture sensor 1 left  ADC //
// A1     -  24 - PC1 -A1/15- ADC1/PCINT9       - Moisture sensor 1 right 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 //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A11    -  89 - PK0 -     - ADC1 4/PCINT?     -                         ADC //
// A12    -  88 - PK1 -     - ADC15/PCINT?      -                         ADC //
// A13    -  87 - PK2 -     - ADC14/PCINT?      -                         ADC //
// A15    -  86 - PK3 -     - ADC15/PCINT?      -                         ADC //
// A12    -  85 - PK4 -     - ADC14/PCINT?      -                         ADC //
// A13    -  84 - PK5 -     - ADC15/PCINT?      -                         ADC //
// A14    -  83 - PK6 -     - ADC14/PCINT22     -                         ADC //
// A15    -  82 - PK7 -     - ADC15/PCINT23     -                         ADC //
//                                                                            //
// CONNECTIONS RAILS QUER RIGHT ********************************************* //

// 23     -     -     -     -                   - DHT22 Sensor            DIO //
// 25     -     -     -     -                   -                         DIO //
// 27     -     -     -     -                   -                         DIO //
// 27     -     -     -     -                   -                         DIO //

// 44     -  40 - PL5 -     - OC5C              - 3 Color led Red         PWM //
// 45     -  39 - PL4 -     - OC5B              - 3 Color led Blue        PWM //
// 46     -  38 - PL3 -     - OC5A              - 3 Color led Green       PWM //
// 50     -  22 - PB3 -     - MISO/PCINT3       - SD Card MISO            SPI //
// 51     -  21 - PB2 -     - MOSI/PCINT2       - SD Card MOSI            SPI //
// 52     -  20 - PB1 -     - SCK/PCINT1        - SD Card SCK             SPI //
// 53     -  19 - PB1 -     - SS/PCINT0         - SD Card SS              SPI //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP in 8 bits bytes:                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898
////////////////////////////////////////////////////////////////////////////////
// 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  //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////