////////////////////////////////////////////////////////////////////////////////
// Name:       SR-004 Test Sonar Range Finder                                 //
// Created by: HARB rboek2@gmail.com december 2019 GPL copyrights             //
// Platform:   Arduino UNO Rev3                                               //
// As outputs the following modules are mounted:                              //
// - Standard Arduino Onboard LED (PWM)                                       //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=185 //
// As inputs the following modules are mounted:                               //
// - SRF04 Sonar Range Finders                                                //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=6   //
// For communications are mounted:                                            //
// - Standard Serial Monitor output                                           //
//            http://robotigs.nl/robotigs/includes/parts_header.php?idpart=43 //
////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1   Never use this memory location to be AVR compatible    //
////////////////////////////////////////////////////////////////////////////////




// SET PRECOMPILER OPTIONS *****************************************************
  // Initialse conditional compiling, uncomment to include, comment to exclude -
  // Do comment the next line for runtime versions -----------------------------
  #define RS232                   //Uncomment to include Serial Monitor sections
  //#ifdef RS232     //Only include these lines if the variable has been defined

  // Define the needed header files for the precompiler, no charge if not used -
  // Define precompiler variables, runs faster & doesn`t use RAM ---------------
  // Define PINS ---------------------------------------------------------------
  #define trigPin1      4                        //Trigger starts sensor 1 SRF04
  #define echoPin1      3                            //Echo reads sensor 1 SRF04
  #define trigPin2      5                        //Trigger starts sensor 2 SRF04
  #define echoPin2      7                            //Echo reads sensor 2 SRF04
  //                 0, 1                    Serial monitor aansluiting TERMINAL


  //Define EEPROM variables ----------------------------------------------------

  //Define DATABASE VARIABLES --------------------------------------------------

  //Define VARIABLES -----------------------------------------------------------
  bool    ledOnBoardVal  = LOW;  //You choose HIGH=on or LOW=off for LED_BUILTIN
  long duration1, cm1, inches1;
  long duration2, cm2, inches2;

  //Initialise objects ---------------------------------------------------------
 
//END OF PRECOMPILER OPTIONS ---------------------------------------------------



void setup() { //Setup runs once ***********************************************
  Serial.begin(57600);     //Nothing more needed for the Serial Monitor TERMINAL
  Serial.println("Setup ports");         //Show the user the setup is done RS232
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard BUILTIN LED
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  disable_jtag();         //Disable jtag to free port C, enabled by default JTAG


  //Start objects --------------------------------------------------------------
  //Test hardware and software -------------------------------------------------
  Serial.println("Setup completed");     //Show the user the setup is done RS232
} //End of setup ---------------------------------------------------------------



void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin1, INPUT);
  duration1 = pulseIn(echoPin1, HIGH);
 
  // convert the time into a distance
  cm1 = (duration1/2) / 29.1;
  inches1 = (duration1/2) / 74; 
  Serial.print(inches1);
  Serial.print("in, ");
  Serial.print(cm1);
  Serial.print("cm");
  Serial.print("  ");

  digitalWrite(trigPin2, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin2, INPUT);
  duration2 = pulseIn(echoPin2, HIGH);
 
  // convert the time into a distance
  cm2 = (duration2/2) / 29.1;
  inches2 = (duration2/2) / 74; 
  Serial.print(inches2);
  Serial.print("in, ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
  
  toggle_ledOnBoard();          //Toggles the LED_BUILTIN on-board LED on or off
  delay(1000);                                      //Just to test your patience
} //Start loop() again ----------------------- KEEP ON RUNNING THIS LOOP FOREVER




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_ledBin ---------------------------------------------------------




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  - DS1307 Clock             TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  - DS1307 Clock             TWI //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - LAN SPI SCK  LED_BUILTIN/SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - LAN SPI MISO             SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - LAN SPI MOSI             PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - LSN SPI SS               PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       - Relay2                   PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  - LAN CLK                  DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      - Relay1                   DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 - ledBluPin                PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   - ledGrePin                PWM //
//  4    -   6 - PD4 -   4 - PCINT20/XCK/T0    - buzActPin                DIO //
// ~3    -   5 - PD3 -   4 - PCINT19/XCK/T0    - ledRedPin                PWM //
//  2    -   4 - PD2 -   2 - PCINT18/INT0      -                          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       - DS18B20                  ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       - ldrPin                   ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      - Relay4                   ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      - Relay3                   ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  - DS1307 Clock             TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  - DS1307 Clock             TWI //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1   Never use this memory location to be AVR compatible    //
// 0001  0001      1   tuinLDRtreshold *10 to switch DAY=0/NIGHT=1023     LDR //
// 0002  0002      1   Number of minutes tuinWaterDelay after dark        ALL //
// 0003  0003      1 WATER1 tuinWater1prog program 1=off 2=on 3=auto   RELAY1 //
// 0004  0004      1   Number of minutes tuinWater1spray water on      RELAY1 //
// 0005  0005      1 WATER2 tuinWater2prog program 1=off 2=on 3=auto   RELAY2 //
// 0006  0006      1   Number of minutes tuinWater2spray water on      RELAY2 //
// 0007  0007      1 WATER3 tuinWater3prog program 1=off 2=on 3=auto   RELAY3 //
// 0008  0008      1   Number of minutes tuinWater3spray water on      RELAY3 //
// 0009  0009      1 WATER4 tuinWater4Pprog program 1=off 2=on 3=auto  RELAY4 //
// 0010  0010      1   Number of minutes tuinWater4spray water on      RELAY4 //
////////////////////////////////////////////////////////////////////////////////
//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=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  //
////////////////////////////////////////////////////////////////////////////////