////////////////////////////////////////////////////////////////////////////////
// Name:       RO-047                                                         //
//             Battery voltage monitor                                        //
// Platform:   Arduino Uno R3                                                 //
// Created by: HARB, May 2016, GPL copyrights                                 //
// http://robotigs.com/robotigs/includes/parts_header.php?idpart=44           //
// Connect 3 voltage dividers and read the results at the serial monitor      //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************
// Define precompiler settings and variables------------------------------------
bool ledBinVal = LOW;            //LED_BUILTIN, you can chose HIGH=on or LOW=off

#define anaVd1Pin     0 //Define to which I/O pin the H=1/4 divider is connected
int     anaVd1Val =   0;                    //Contains last measurement (0-1023)

#define anaVd2Pin     1 //Define to which I/O pin the H=1/3 divider is connected
int     anaVd2Val =   0;                    //Contains last measurement (0-1023)

#define anaVd3Pin     2 //Define to which I/O pin the H=1/2 divider is connected
int     anaVd3Val =   0;                    //Contains last measurement (0-1023)
///Define the needed header files for the precompiler, no charge if not used ---
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  analogReference(DEFAULT);           //Option may be different for other boards
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  Serial.println("Measure voltages with dividers");                //Hello world
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  delay(900);                                  //Wait milliseconds, 900s = 15min
  toggle_ledBin();                                //Show I am awake, Hello World
  anaVd1Val = analogRead(anaVd1Pin);                //Read voltage on pin anaPn1
  delay(2);                 //Wait milliseconds, really needed?, check datasheet
  anaVd2Val = analogRead(anaVd2Pin);                //Read voltage on pin anaPn1
  delay(2);                   //Wait milliseconds, really needed?, check datashe
  anaVd3Val = analogRead(anaVd3Pin);                //Read voltage on pin anaPn1
  delay(2);                //Wait milliseconds, really needed?, check datasheeet

  Serial.print("Divider1:");
  Serial.print(anaVd1Val);
  Serial.print("=");
  float Vd1Scr = (float)anaVd1Val * 20.0 / 1023.0;
  Serial.print(Vd1Scr);
  Serial.print(" Vdc   Divider2:");
  Serial.print(anaVd2Val);
  Serial.print("=");
  float Vd2Scr = (float)anaVd2Val * 15.0 / 1023.0;
  Serial.print(Vd2Scr);
  Serial.print(" Vdc   Divider3:");
  Serial.print(anaVd3Val);
  Serial.print("=");
  float Vd3Scr = (float)anaVd3Val * 10.0 / 1023.0;
  Serial.print(Vd3Scr);
  Serial.println(" Vdc");
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER

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


////////////////////////////////////////////////////////////////////////////////
// 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        - Arduino LED_BUILTIN LED  SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       -                          SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  -                          PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    -                          PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       -                          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    -                          PWM //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                          INT //
// ~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   -     -     -     -                   - Output to divider H=1/4      //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       - Input divider H=1/4      ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       - Divider H=1/3            ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      - Divider H=1/2            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 //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898