////////////////////////////////////////////////////////////////////////////////
// Name:       RS232uno-01                                                    //
// Platform:   Arduino UNO R3                                                 //
// Created by: HARB rboek2@gmail.com November 2019 GPL copyrights             //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=26             //
// This program is made to cummonicate via RS232.                             //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=43  //
////////////////////////////////////////////////////////////////////////////////



// SET PRECOMPILER OPTIONS *****************************************************
  //Initialse conditional compiling, uncomment to include, comment to exclude --
  // Do comment for runtime versions
  //#define RS232                 //Uncomment to include Serial Monitor sections


  //Define the needed header files for the precompiler, no charge if not used --
  #include <RTClib.h>        //Manipulates clock via I2C needs Wire.h lib DS1307
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=289
  #include <Wire.h>     //Needed ao by RTClib: Two Wire Interface lib TWI DS1307
              // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=31
  #include <OneWire.h>    //Library can be installed through Arduino IDE DS18B20
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=180
  #include <EEPROM.h>               //Needed to read or write settings in EEPROM
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=312
  #include <SPI.h>   //Serial Peripheral Interface requiered by software SD-CARD
              // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=28
  #include <SD.h>                      //Include SD library software for SD-CARD
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=116
                           
  //Define PINS ----------------------------------------------------------------
  #define heaterPin     2                  //Pin connects VERWARMINGSMAT RELAIS1
  #define ledBluPin     3        //3 Colour LED, which PWM pin connects BLUE LED
  #define lightPin      4                //Pin connects GROEIVERLICHTING RELAIS2
  #define ledGrePin     5       //3 Colour LED, which PWM pin connects GREEN LED
  #define ledRedPin     6         //3 Colour LED, which PWM pin connects RED LED


  OneWire term1(8);           //Connects to pin 8, but may be any DIO pin DS1820

  //Define DATABASE VARIABLES --------------------------------------------------
  float  tempDS18B20;                      //Soil temperature in Celsius DS18B20

  //Define DATABASE & EEPROM variables -----------------------------------------
  int    ledRedBril     =    10;                      //Max brillance of RED LED
  int    ledGreBril     =     5;                    //Max brillance of GREEN LED
  int    ledBluBril     =    10;                     //Max brillance of BLUE LED
  int    freqMeasSec    =    10;  //Every freqMeasSec we will take a MEASUREMENT

  //Define variables -----------------------------------------------------------
  int    ledOnBoardVal  =   LOW; //You choose HIGH-on or LOW-off for LED_BUILTIN
  int    ledRedStatus   =     0;            //0-255 current status for pc of LED
  int    ledGreStatus   =     0;            //0-255 current status for pc of LED
  int    ledBluStatus   =     0;            //0-255 current status for pc of LED
  int    brillance      =     0;   //Used in testleds to count down TEST ALL LED
  word   msWait         =    20;   //Used in testleds to count down TEST ALL LED
  bool   heaterStatus   = HIGH; //Status HIGH=off, LOW=on VERWARMINGSMAT RELAIS1
  bool   lightStatus    = HIGH;   //Status 1=off, 0=on  GROEIVERLICHTING RELAIS2

  int    byteReceived   =     0;    //Individual bytes received by program RS232
  String currentData    =    "";//Export through RS232 or show on SERIAL MONITOR
  word   readCounter    =     0;  //Read sensors if counted down to zero SENSORS
  word   readTimer      =   600;  //Fill readCounter after reaching zero SENSORS
  byte   present        =     0;//Used for oneWire, present = ds.reset() DS18B20
  byte   i;                   //Used for oneWire, loopcounter byte array DS18B20
  byte   data[12];            //Used for oneWire to store data read from DS18B20
  byte   type1_s        =     0;     //Type 0 = ok, except old DS1820=1, DS18B20
  byte   addr1[8];               //Array with first 8 bytes, inc/address DS18B20
  String klokTime       = "2019-06-11 23-23-34";               //DateTime DS1307
  char   buf[100];                //Needed to display the date/time stamp DS1307
  int    currenthour;         //Compare with start and finish for GROWLED DS1307
  String tmp2           = "";                      //Can be used anywhere DS1307
  int    tmp1;                                            //Can be used ANYWHERE

  //Initialize OBJECTS ---------------------------------------------------------
  DS1307 rtc;                         //Initialize Real Time Clock object DS1307
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  disable_jtag();       //Disable jtag to free port C, enabled by default SYSTEM
  Serial.begin(57600);        //Nothing more needed for the Serial Monitor RS232
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN

  pinMode(heaterPin, OUTPUT);              //Pin connects VERWARMINGSMAT RELAIS1
  digitalWrite(heaterPin, heaterStatus);               //Switches OFF the RELAY1
  pinMode(lightPin, OUTPUT);             //Pin connects GROEIVERLICHTING RELAIS2
  digitalWrite(lightPin, lightStatus);                 //Switches OFF the RELAY2

  //Start objects --------------------------------------------------------------
  DS1820_init();      //Determins the type of DS1820 and reads properties DS1820
  Wire.begin();                 //Start the Two Wire Interface object I2C DS1307
  rtc.begin();    //Initialize Wire.begin first. Start the object running DS1307
  //rtc.adjust(DateTime(__DATE__, __TIME__));      //Set to time compiled DS1307

  //Test hardware and software -------------------------------------------------
  //test_LEDs();     //PWM fade in and fade out for 3 colorLEDs on board ALL LED
  //test_RELAY();                          //Switches ON for 2 seconds all RELAY
  //Serial.println("Start loop");                       //Show on SERIAL MONITOR
} //End of setup ---------------------------------------------------------------




void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  readClock();               //Read the current time into the object from DS1307
  readSensors();          //Read several sensors at timed intervals only SENSORS
  checkRS232();                //Check if anything has been passed through RS232
}//End of void loop() ------------------------ KEEP ON RUNNING THIS LOOP FOREVER



void readClock() { //Read the current time into the object from DS1307 *********
  DateTime now = rtc.now();  //Read the current time into the object from DS1307
  strncpy(buf,"YYYY-MM-DD hh:mm:ss\0",100);  //Format string for the time DS1307
  klokTime = now.format(buf);       //Format the timestap into a variable DS1307
  strncpy(buf,"hh\0",100);                  //Format string for the time GROWLED
  tmp2 = now.format(buf);          //Format the timestap into a variable GROWLED
  currenthour = tmp2.toInt();           //Convert to value for switching GROWLED
} //Exit readClock -------------------------------------------------------------



void checkRS232() {            //Check if anything has been passed through RS232
  // RESET READ SENSOR COUNTER AFTER EVERY REQUEST
  if (Serial.available() > 0) { //Check bytes received, -1 is empty buffer RS232
    byteReceived = Serial.read();    //First byte received is always a menu item
    switch (byteReceived) {          //Go to the according procedure / menu item

      case 49: //************************************* Menu item 1 => LED RED ON
      ledRedStatus = ledRedBril;                          //Refresh the LED DATA
      analogWrite(ledRedPin,ledRedBril);                         //Switch LED ON
      Serial.println("LED ON");                         //Show on SERIAL MONITOR
      break; //case 49:                                Menu item 1 => LED RED ON

      case 50: //************************************ Menu item 2 => LED RED OFF
      ledRedStatus = 0;                                   //Refresh the LED DATA
      analogWrite(ledRedPin,0);                                 //Switch LED OFF
      Serial.println("LED OFF");                        //Show on SERIAL MONITOR
      break; //case 48:                               Menu item 2 => LED RED OFF

      case 51: //*********************************** Menu item 3 => LED GREEN ON
      ledGreStatus = ledGreBril;                          //Refresh the LED DATA
      analogWrite(ledGrePin,ledGreBril);                         //Switch LED ON
      Serial.println("LED ON");                         //Show on SERIAL MONITOR
      break; //case 51:                              Menu item 3 => LED GREEN ON

      case 52: //********************************** Menu item 4 => LED GREEN OFF
      ledGreStatus = 0;                                   //Refresh the LED DATA
      analogWrite(ledGrePin,0);                                 //Switch LED OFF
      Serial.println("LED OFF");                        //Show on SERIAL MONITOR
      break; //case 52:                             Menu item 4 => LED GREEN OFF

      case 53: //************************************ Menu item 5 => LED BLUE ON
      ledBluStatus = ledBluBril;                          //Refresh the LED DATA
      analogWrite(ledBluPin,ledBluBril);                         //Switch LED ON
      Serial.println("LED ON");                         //Show on SERIAL MONITOR
      break; //case 53:                               Menu item 5 => LED BLUE ON

      case 54: //*********************************** Menu item 6 => LED BLUE OFF
      ledBluStatus = 0;                                   //Refresh the LED DATA
      analogWrite(ledBluPin,0);                                 //Switch LED OFF
      Serial.println("LED OFF");                        //Show on SERIAL MONITOR
      break; //case 54:                              Menu item 6 => LED BLUE OFF

      case 55: //*********************** Menu item 7 => SHOW TEMPERATURE DS18B20
      spawnData();       //Export the data of this program to a PC through RS232
      break; //case 55:                  Menu item 7 => SHOW TEMPERATURE DS18B20
      
    }//End of switch (byteReceived)
  }//End of serial available
} //Exit checkRS232 ------------------------------------------------------------



void spawnData() { //Export the data of this program to a PC through RS232 *****
  currentData = klokTime;      //Reset the datstring and add temperature DS18B20
  currentData += " ";                             //Add a space to separate DATA
  currentData += tempDS18B20;  //Reset the datstring and add temperature DS18B20
  currentData += " ";                             //Add a space to separate DATA
  currentData += ledRedStatus;                          //Show on SERIAL MONITOR
  currentData += " ";                             //Add a space to separate DATA
  currentData += ledGreStatus;                          //Show on SERIAL MONITOR
  currentData += " ";                             //Add a space to separate DATA
  currentData += ledBluStatus;                          //Show on SERIAL MONITOR
  currentData += " ";                             //Add a space to separate DATA
  currentData += ledRedBril;                            //Show on SERIAL MONITOR
  currentData += " ";                             //Add a space to separate DATA
  currentData += ledGreBril;                            //Show on SERIAL MONITOR
  currentData += " ";                             //Add a space to separate DATA
  currentData += ledBluBril;                            //Show on SERIAL MONITOR
  currentData += " ";                             //Add a space to separate DATA
  currentData += freqMeasSec;                           //Show on SERIAL MONITOR
  Serial.println(currentData);  //Export through RS232 or show on SERIAL MONITOR
} //Exit spawnData -------------------------------------------------------------




void readSensors() { //Read several sensors at timed intervals only ************
  if (readCounter == 0){       //Only perform measurements if counted down TIMER
    analogWrite(ledGrePin, ledGreBril);     //Green HIGH=on, LOW=off activityLED
    DS1820_read();               //Reads the temperature in Celsius from DS18B20
    readCounter = readTimer * freqMeasSec;             //RESET the counter TIMER
    //setActuators();                            //Calculate and set all OUTPUTS
    digitalWrite(ledGrePin, LOW);            //Blue HIGH=on, LOW=off activityLED
  }else{                                //Meaning counter was not yet zero TIMER
    readCounter--;                        //Decrement of the timer counter TIMER
  } //End of if (moistureCnt1 == 0)Perform measurements if counted down    TIMER
} //Exit readSensors -----------------------------------------------------------



void DS1820_read(void) { //Reads the temperature from DS1820 in Celsius ********
  term1.reset();                              //Reset whatever still was running
  term1.select(addr1);                      //Set the parameters for the library
  term1.write(0x44);       //Start conversion, with parasite power on at the end
  delay(800);     //Maybe 750ms is enough, maybe not, takes a lot of time though
  present = term1.reset();              //We assume that the conversion is ready
  term1.select(addr1);                      //Set the parameters for the library
  term1.write(0xBE);                                          // Read Scratchpad
  for ( i = 0; i < 9; i++) {                                   //We need 9 bytes
    data[i] = term1.read();                                  //Read byte by byte
  }                                                       //End of reading bytes
  int16_t raw = (data[1] << 8) | data[0];                      //Rotate the data
  tempDS18B20 = (float)raw / 16.0;     //Untill they are in the correct position
} //Exit DS1820_read -----------------------------------------------------------



void DS1820_init(void) { //Determins the type of DS1820 thermometer1 ***********
  if (!term1.search(addr1)) {            //Term1 is an objest created by ONEWIRE
    term1.reset_search();                  //So if the variables are still empty
    delay(250);          //The variables must be filled and that costs some time
    return;                               //Are you sure any DS1820 is connected
  }                                           //End of if (!term1.search(addr1))
  if (OneWire::crc8(addr1, 7) != addr1[7]) {
      return;
  }
  switch (addr1[0]) {         //The first ROM byte indicates which  tupe of chip
    case 0x10:
      type1_s = 1;
      break;
    case 0x28:
      type1_s = 0;
      break;
    case 0x22:
      type1_s = 0;
      break;
    default:
      return;
  } 
  term1.reset();
  term1.select(addr1);
  term1.write(0x44, 1);    //Start conversion, with parasite power on at the end
  delay(800);     //Maybe 750ms is enough, maybe not, takes a lot of time though
  present = term1.reset();
  term1.select(addr1);    
  term1.write(0xBE);                                           //Read Scratchpad
  for ( i = 0; i < 9; i++) {                                   //We need 9 bytes
    data[i] = term1.read();
  }

  int16_t raw = (data[1] << 8) | data[0];
  if (type1_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 {     //// default is 12 bit resolution, 750 ms conversion time
    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
  }
  tempDS18B20 = (float)raw / 16.0;
} //Exit DS1820_init -----------------------------------------------------------



void test_LEDs(void){ //PWM fade in and fade out for all 4 LEDs on board *******
  brillance = 0;                 //Brightness of any color, just to test PWM LED

  while (brillance<ledRedBril){
    analogWrite(ledRedPin, brillance);        //Set LED to desired PWM value RED
    brillance++;
    delay (msWait);
  }
  while (brillance>0){
    analogWrite(ledRedPin, brillance);        //Set LED to desired PWM value RED
    brillance--;
    delay (msWait);
  }
  analogWrite(ledRedPin, 0);            //Set LED to desired PWM value = off RED

  while (brillance<ledGreBril){
    analogWrite(ledGrePin, brillance);      //Set LED to desired PWM value GREEN
    brillance++;
    delay (msWait);
  }
  while (brillance>0){
    analogWrite(ledGrePin, brillance);      //Set LED to desired PWM value GREEN
    brillance--;
    delay (msWait);
  }
  analogWrite(ledGrePin, 0);          //Set LED to desired PWM value = off GREEN

  while (brillance<ledBluBril){
    analogWrite(ledBluPin, brillance);       //Set LED to desired PWM value BLUE
    brillance++;
    delay (msWait);
  }
  while (brillance>0){
    analogWrite(ledBluPin, brillance);       //Set LED to desired PWM value BLUE
    brillance--;
    delay (msWait);
  }
  analogWrite(ledBluPin, 0);           //Set LED to desired PWM value = off BLUE
} //Exit test_LEDs -------------------------------------------------------------



void toggle_ledOnBoard(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 toggle_ledOnBoard -----------------------------------------------------



void test_RELAY(){ //Switches ON for 2 seconds all RELAY ***********************
  digitalWrite(heaterPin, LOW);                         //Switches ON the RELAY1
  delay (2000);                                             //Wait for 2 seconds
  digitalWrite(heaterPin, HIGH);                       //Switches OFF the RELAY1
  digitalWrite(lightPin, LOW);                          //Switches ON the RELAY2
  delay (2000);                                             //Wait for 2 seconds
  digitalWrite(lightPin, HIGH);                        //Switches OFF the RELAY2
} //End of test_Relay(){ Switches ON for 2 seconds the RELAY -------------------



void disable_jtag(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 jtag_disable ----------------------------------------------------------





////////////////////////////////////////////////////////////////////////////////
// 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  -                           NC //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  -                           NC //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - SCK SD-card LED_BUILT_IN SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - MISO SD-card             SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - MOSI SD-card         PWM SPI //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    -                      PWM SPI //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       -                          PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  - DS1820 Temperature       DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      -                          DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 - LED red                  PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   - LED green                PWM //
// 4     -   6 - PD4 -   4 - PCINT20/XCK/T0    - Relais 1                 INT //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 - LED blue                 PWM //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      - Relais 2                 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               -                          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  - Clock DS1307             TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  - Clock DS1307             TWI //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000    1        Never use this memory location to be AVR compatible //
// 0001  0001    1   WERKVERLICHTING hobbyLightProg: 1=off 2=on 3=auto RELAY1 //
// 0002  0002    1     If LDR reaches this kasLightON*10 switch        RELAY1 //
// 0003  0003    1     kasLightSecs*10 werkverlichting on              RELAY1 //
// 0004  0004    1   VERWARMING hobbyHeatProg: 1=off 2=on 3=auto       RELAY2 //
// 0005  0005    1     Celsius hobbyHeatON/10 verwarming switch on     RELAY2 //
// 0006  0006    1     Celsius hobbyHeatOFF/10 verwarming switch off   RELAY2 //
// 0007  0007    1   GROEILED hobbyLedProg: 1=off 2=on 3=auto          RELAY3 //
// 0008  0008    1     Hours kasLedHours around noon to switch on      RELAY3 //
// 0009  0009    1   WATER hobbyWaterProg: 1=off 2=on 3=auto           RELAY4 //
// 0010  0010    1     hobbyWaterSecs*10 to keep watering              RELAY4 //
// 0011  0011    1   AUDIO hobbyAudioProg: 1=off 2=on 3=auto           RELAY5 //
// 0012  0012    1     hobbyAudioMins*10 to keep audio playing         RELAY5 //
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
// 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=0)         //
// 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  //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898