////////////////////////////////////////////////////////////////////////////////
// Name:       mrBoldermin-02                                                 //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=16             //
//             This program will drive the mrBolderbmin robot.                //
//             01 - Motoren + drivers / IR afstandsbediening                  //
//             02 - SRF04 / Accu monitor                                      //
// Created by: HARB rboek2@gmail.com Feb 2020 GPL copyrights                  //
// Platform:   Arduino Mega 2560                                              //
//                                                                            //
// As outputs the following modules are mounted:                              //
// - Standard Arduino Onboard LED (PWM)                                       //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=185 //
// - 3 color LED (PWM)                                                        //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=293 //
// - Activ loudspeaker / buzzer                                               //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=240 //
// - Motordriver module L9110S                                                //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=222 //
//                                                                            //
// As inputs the following modules are mounted:                               //
// - Sonar Range Finder SRF04                                                 //
//             http://robotigs.nl/robotigs/includes/parts_header.php?idpart=6 //
// - Spanningsdeler                                                           //
//            http://robotigs.nl/robotigs/includes/parts_header.php?idpart=44 //
//                                                                            //
// For communications are mounted:                                            //
// - Standard Serial Monitor output                                           //
//            http://robotigs.nl/robotigs/includes/parts_header.php?idpart=43 //
// - IR ontvanger voor een tv afstandsbediening                               //
//            http://robotigs.nl/robotigs/includes/parts_header.php?idpart=10 //
// - IR afstandsbediening tv                                                  //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=198 //
////////////////////////////////////////////////////////////////////////////////




// SET PRECOMPILER OPTIONS *****************************************************
  // Initialse conditional compiling, uncomment to include, comment to exclude -
  // Do comment the next line for runtime versions -----------------------------
  //#ifdef RS232     //Lines only included if Serial Monitor is defined COMPILER
  //#endif                               //End of conditional compiling COMPILER
  #define RS232          //Uncomment to include Serial Monitor sections COMPILER


  // Define the needed header files for the precompiler, no charge if not used -
  #include <IRremote.h>   //Never use the IDE default but replace it IR RECEIVER
              // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=10

  // Define precompiler variables, runs faster & doesn`t use RAM ---------------
  // Define PINS ---------------------------------------------------------------
  #define RECV_PIN    2        //Define which DIO pin connects white IR RECEIVER
  #define ledRedPin  44                    //PWM pin for this colour 3 color LED
  #define ledGrePin  46                    //PWM pin for this colour 3 color LED
  #define ledBluPin  45                    //PWM pin for this colour 3 color LED
  #define buzActPin  53   //Define which I/O pin connects the activ white BUZZER
  #define echoPinR   14                   //Echo oranje reads right sensor SRF04
  #define trigPinR    5                 //Trigger geel starts right sensor SRF04
  #define trigPinL   15                  //Trigger geel starts left sensor SRF04
  #define echoPinL   16                    //Echo oranje reads left sensor SRF04
  #define accuPin    A0               //H=1/2 orange ADC voltage divider BATTERY
  #define AIA         7   //Pin connected to right motor forward PWM MOTORDRIVER
  #define AIB        11  //Pin connected to right motor backward PWM MOTORDRIVER
  #define BIA         8    //Pin connected to left motor forward PWM MOTORDRIVER
  #define BIB        12   //Pin connected to left motor backward PWM MOTORDRIVER

  //Set appropriate IR remote control to be used -------------------------------
  //#define Keyes                //Set Remote Control by uncommenting IR RECEIVE
  #define Provision              //Set Remote Control by uncommenting IR RECEIVE

  //Set all the possible engine actions and their location ---------------------
  #define honk        1                                     //Tuut tuut Action 1
  #define forward     2                      //Rijd rechtuit naar voren Action 2
  #define backward    3                   //Rijd rechtuit naar achteren Action 3
  #define stilstaan   4                    //Alle motoren etc uitzetten Action 4
  #define turnRight   5        //Draai naar rechts met rechterwiel stil Action 5
  #define turnLeft    6          //Draai naar links met linkerwiel stil Action 6
  #define spinRight   7  //Draai naar rechts met 2 motoren tegengesteld Action 7
  #define spinLeft    8   //Draai naar links met 2 motoren tegengesteld Action 8
  
  //Define EEPROM variables ----------------------------------------------------
  byte   engAction      =   0;        //Which action should be performed ENGINES
  byte   irDesire       =   0;       //Tv afstandsbediening na press IR RECEIVER
  byte   ledRedBright   = 200;  //Brightness of set by PWM 0=min,255=max RED LED
  byte   ledGreBright   = 200;//Brightness of set by PWM 0=min,255=max GREEN LED
  byte   ledBluBright   = 200; //Brightness of set by PWM 0=min,255=max BLUE LED
  byte   speedEngines   = 200;               //Speed of both engines 0-255 MOTOR
  long   accuVdcCharge  = 660;          //Treshold to search accu loader BATTERY
  long   accuVdcAlarm   = 600; //Treshold to stop moving and start alarm BATTERY
  long   accuVdcClose   = 590;  //Treshold to power down until recharged BATTERY
  
  //Define DATABASE VARIABLES --------------------------------------------------

  //Define VARIABLES -----------------------------------------------------------  
  bool   ledOnBoardVal  = LOW;   //You choose HIGH=on or LOW=off for LED_BUILTIN
  byte   msWait         = 1;            //Test your patience during the test LED
  word   readTimer      = 1999;   //Fill readCounter after reaching zero SENSORS
  unsigned long timeSet;                //To check if IRremote has been released
  long   durationL, cmL;                                            //Left SRF04
  long   durationR, cmR;                                           //Right SRF04
  word   readCounter    = 0;      //Read sensors if counted down to zero COUNTER
  byte   brillance      = 0;        //Brightness counter to test PWM LED COUNTER
  byte   accuStatus     = 4;                //Assuming batteries charged BATTERY
  long   accuVdc        = 0;         //Current voltage * 100 of the pack BATTERY
  long   accuReading    = 0;              //Current ADC reading the pack BATTERY
  //Define user settings -------------------------------------------------------



  #ifdef Keyes                                         //IR Remote Control Keyes
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=198
    const long butRP = 4294967295UL; //Any button is continously pressed, REPEAT
    const long butUp =   16736925UL;             //Blue up arrow button, FORWARD
    const long butDn =   16754775UL;              //Blue down arrow button, BACK
    const long butLf =   16720605UL;              //Blue left arrow button, LEFT
    const long butRt =   16761405UL;            //Blue right arrow button, RIGHT
    const long butOk =   16712445UL;                  //Red OK button, DISENGAGE
    const long butMp =   16728765UL;        //Red * (multiply) button, DISENGAGE
    const long butNr =   16732845UL;          //Red # (number) button, DISENGAGE
    const long but0  =   16730805UL;             //White 0 button, run program 0
    const long but1  =   16738455UL;             //White 1 button, run program 1
    const long but2  =   16750695UL;             //White 2 button, run program 2
    const long but3  =   16756815UL;             //White 3 button, run program 3
    const long but4  =   16724175UL;             //White 4 button, run program 4
    const long but5  =   16718055UL;             //White 5 button, run program 5
    const long but6  =   16743045UL;             //White 6 button, run program 6
    const long but7  =   16716015UL;             //White 7 button, run program 7
    const long but8  =   16726215UL;             //White 8 button, run program 8
    const long but9  =   16734885UL;             //White 9 button, run program 9
  #endif //End of #ifdef Keyes                           IR Remote Control Keyes

  #ifdef Provision                                 //IR Remote Control Provision
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=198
    const long butRP = 4294967295UL; //Any button is continously pressed, REPEAT
    const long butUp = 2645382921UL;             //Blue up arrow button, FORWARD
    const long butDn = 1510218204UL;              //Blue down arrow button, BACK
    const long butLf = 1044084000UL;              //Blue left arrow button, LEFT
    const long butRt =  341443957UL;            //Blue right arrow button, RIGHT
    const long butOk = 2090285130UL;                 //Blue OK button, DISENGAGE
    const long butMp = 4198925182UL;     //Black -/-- (double) button, DISENGAGE
    const long butNr = 3255896299UL;                //Black AV button, DISENGAGE
    const long but0  = 4251643239UL;             //Black 0 button, run program 0
    const long but1  = 2005671744UL;             //Black 1 button, run program 1
    const long but2  = 3075690975UL;             //Black 2 button, run program 2
    const long but3  = 1079910150UL;             //Black 3 button, run program 3
    const long but4  =  771752011UL;             //Black 4 button, run program 4
    const long but5  =  613775946UL;             //Black 5 button, run program 5
    const long but6  =  305207297UL;             //Black 6 button, run program 6
    const long but7  = 3916687738UL;             //Black 7 button, run program 7
    const long but8  = 2229941719UL;             //Black 8 button, run program 8
    const long but9  = 2280455024UL;             //Black 9 button, run program 9
  #endif //End of #ifdef Provision                   IR Remote Control Provision

  // Initialize OBJECTS --------------------------------------------------------
  IRrecv irrecv(RECV_PIN);                //Initialise interrupt pin IR RECEIVER
  decode_results results;   //If something IR received, translate into keynumber
//END OF PRECOMPILER OPTIONS ---------------------------------------------------



void setup() { //Setup runs once ***********************************************
  disable_jtag();              //Disable to free port C, enabled by default JTAG
  #ifdef RS232         //Only include this part if the variable has been defined
    Serial.begin(57600);           //Nothing else needed for the Serial Monitor
  #endif //End of        Serial not compiled if precompiler variable was not set

  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  pinMode(buzActPin, OUTPUT);                                //KY-006 buzzer pin
  pinMode(ledRedPin, OUTPUT);             //Make the LED connections output pins
  pinMode(ledGrePin, OUTPUT);             //Make the LED connections output pins
  pinMode(ledBluPin, OUTPUT);             //Make the LED connections output pins
  digitalWrite(AIA, 0);             //Right motor forward do nothing MOTORDRIVER
  digitalWrite(AIB, 0);            //Right motor backward do nothing MOTORDRIVER
  digitalWrite(BIA, 0);              //Left motor forward do nothing MOTORDRIVER
  digitalWrite(BIB, 0);             //Left motor backward do nothing MOTORDRIVER
  pinMode(AIA, OUTPUT);          //Set right motor forward as output MOTORDRIVER
  pinMode(AIB, OUTPUT);         //Set right motor backward as output MOTORDRIVER
  pinMode(BIA, OUTPUT);           //Set left motor forward as output MOTORDRIVER
  pinMode(BIB, OUTPUT);          //Set left motor backward as output MOTORDRIVER
  pinMode(trigPinL, OUTPUT);                                        //Left SRF04
  pinMode(echoPinL, INPUT);                                         //Left SRF04
  pinMode(trigPinR, OUTPUT);                                       //Right SRF04
  pinMode(echoPinR, INPUT);                                        //Right SRF04

  //Start objects --------------------------------------------------------------
  irrecv.enableIRIn();                                  // Start the IR RECEIVER
  
  //Test hardware and software -------------------------------------------------
  //test_LEDs();          //PWM fade in and fade out for all 4 LEDs on board LED
  beep(1);                      //Create a beep (x5ms) with KY-012 active BUZZER
} //End of setup ---------------------------------------------------------------






void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  readSensors();          //Read several sensors at timed intervals only SENSORS
  readIRreceiver();       //Receives data from the IR remote control IR RECEIVER
  engAction = irDesire;     //Meest simpele omzetting van wens naar actie ACTION
  performAction();       //Actually drive the engines and other actuators ACTION
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER







void readSensors() { //Read several sensors at timed intervals only ************
  if (readCounter == 0){       //Only perform measurements if counted down TIMER
    analogWrite(ledGrePin, ledGreBright);  //Green HIGH=on, LOW=off activity LED
    readSRF04L();                           //Read Sonar Range Finder left SRF04
    readCounter = readTimer;                           //RESET the counter TIMER
    readAccupack();                //Set accuStatus according to voltage BATTERY
    readSRF04R();                          //Read Sonar Range Finder right SRF04    
    //show_testdata();               //Data for programmer at the Serial MONITOR
    digitalWrite(ledGrePin, LOW);           //Blue HIGH=on, LOW=off activity LED
  }else{ //Meaning counter was not yet zero
    readCounter--;                        //Decrement of the timer counter TIMER
  } //End of if (readCounter == 0)
} //Exit readSensors -----------------------------------------------------------



void readSRF04L() { //Read Sonar Range Finder left SRF04 ***********************
  digitalWrite(trigPinL, LOW);                 //Read the signal from the sensor
  delayMicroseconds(5);                //A HIGH pulse whose duration is the time
  digitalWrite(trigPinL, HIGH);      //(in microseconds) from the sending of the
  delayMicroseconds(10);    //ping to the reception of its echo off of an object
  digitalWrite(trigPinL, LOW);                      //Start with sending a pulse
  delayMicroseconds(10);    //ping to the reception of its echo off of an object
  durationL = pulseIn(echoPinL, HIGH);    //Measure the time until echo received
  cmL = (durationL/2) / 29.1;                 //Convert the time into a distance
} //Exit readSRF04L ------------------------------------------------------------





void readSRF04R() { //Read Sonar Range Finder right SRF04 **********************
  digitalWrite(trigPinR, LOW);                 //Read the signal from the sensor
  delayMicroseconds(5);                //A HIGH pulse whose duration is the time
  digitalWrite(trigPinR, HIGH);      //(in microseconds) from the sending of the
  delayMicroseconds(10);    //ping to the reception of its echo off of an object
  digitalWrite(trigPinR, LOW);                      //Start with sending a pulse
  delayMicroseconds(10);    //ping to the reception of its echo off of an object
  durationR = pulseIn(echoPinR, HIGH);    //Measure the time until echo received
  cmR = (durationR/2) / 29.1;                 //Convert the time into a distance
} //Exit readSRF04R ------------------------------------------------------------




void readIRreceiver(void) {           //Receives data from the IR remote control
  if (irrecv.decode(&results)) {  //If I did receive anything then lets check it
    switch (results.value) {             //Perform value received from TV remote

      case butRP:                       //User keeps pressing that button REPEAT
      //Vanaf nu ontvang de IRonvager enkel nog repeats of anders helemaal niets
      //engAction verandert niet hierdoor
      break;                                                //Dus verander niets

      case butOk:           //User has pressed the button Ok = SOUND BUZZER HORN
        irDesire = honk;
        break;

      case butUp:               //User has pressed the button up = DRIVE FORWARD
        irDesire = forward;
        break;

      case butRt:               //User has pressed the button right = TURN RIGHT
        irDesire = spinRight;
        break;

      case butLf:                 //User has pressed the button left = TURN LEFT
        irDesire = spinLeft;
        break;

      case butDn:            //User has pressed the button down = DRIVE BACKWARD
        irDesire = backward;
        break;

      default:           //Iedere andere toets ingedrukt op de afstandsbediening
        irDesire = stilstaan;
        break;

    } //End of switch (results.value)      Perform value received from TV remote
    irrecv.resume();                         //Prepair to receive the next value

    //We are sure a key was pressed. So now set a timer we can count down until
    // we declare the last key invalid. In other words: check if no key is
    // pressed at the IR remote control. So we do set a time here.
    timeSet = millis();                        //Sets time since program started
  } //End of if (irrecv.decode(&results)

  if ((millis()-timeSet) > 150) {     //Als er na 150ms nog niets is waargenomen
    irDesire = stilstaan;       //We moeten er vanuit gaan dat alles is released
  } //End of if ((millis()-timeSet) > 150)

} //Exit of readIRreceiver ------------ Receives data from the IR remote control




void show_testdata () { //Data for programmer at the Serial Monitor ************
  Serial.print("Since=");
  Serial.print(millis() - timeSet);               //Prints time last key pressed
  Serial.print("  timeSet=");
  Serial.print(timeSet);                          //Prints time last key pressed
  Serial.print(" accuReading=");
  Serial.print(accuReading);
  Serial.print(" accuVdc=");
  Serial.print(accuVdc);
  Serial.print(" accuStatus=");
  Serial.print(accuStatus);
  Serial.print(" cmL=");
  Serial.print(cmL);
  Serial.print(" cmR=");
  Serial.print(cmR);
  Serial.println(" ");
} //Exit show_testdata Data for programmer at the Serial Monitor DONE ----------




void performAction() { //Actually drive the engines and other actuators ********
  switch (engAction) {      //Perform the value received from the program logics

      case honk:            //User has pressed the button Ok = SOUND BUZZER HORN
        digitalWrite(buzActPin,HIGH);                           //Turn on BUZZER
      break;                                                        //Ready exit

      case forward:             //User has pressed the button up = DRIVE FORWARD
        digitalWrite(AIA, 0);                     //Right motor backward nothing
        analogWrite(AIB, speedEngines);              //Right motor forward drive
        digitalWrite(BIA, 0);                      //Left motor backward nothing
        analogWrite(BIB, speedEngines);               //Left motor forward drive
      break;                                                        //Ready exit

      case spinRight:           //User has pressed the button right = TURN RIGHT
        analogWrite(AIA, speedEngines);             //Right motor backward drive
        digitalWrite(AIB, 0);                      //Right motor forward nothing
        digitalWrite(BIA, 0);                      //Left motor backward nothing
        analogWrite(BIB, speedEngines);               //Left motor forward drive
      break;                                                        //Ready exit

      case spinLeft:              //User has pressed the button left = TURN LEFT
        digitalWrite(AIA, 0);                     //Right motor backward nothing
        analogWrite(AIB, speedEngines);              //Right motor forward drive
        analogWrite(BIA, speedEngines);              //Left motor backward drive
        digitalWrite(BIB, 0);                       //Left motor forward nothing
      break;                                                        //Ready exit

      case backward:         //User has pressed the button down = DRIVE BACKWARD
        analogWrite(AIA, speedEngines);             //Right motor backward drive
        digitalWrite(AIB, 0);                      //Right motor forward nothing
        analogWrite(BIA, speedEngines);              //Left motor backward drive
        digitalWrite(BIB, 0);                       //Left motor forward nothing
      break;                                                        //Ready exit

      case stilstaan:                    //User has pressed the button stilstaan
        digitalWrite(AIA, 0);                         //Right motor forward STOP
        digitalWrite(AIB, 0);                        //Right motor backward STOP
        digitalWrite(BIA, 0);                          //Left motor forward STOP
        digitalWrite(BIB, 0);                         //Left motor backward STOP
        digitalWrite(buzActPin,LOW);            //Turn annoying sound off BUZZER
      break;                                                        //Ready exit

      default:                             //Iedere andere code zet de boel stil
        digitalWrite(AIA, 0);                         //Right motor forward STOP
        digitalWrite(AIB, 0);                        //Right motor backward STOP
        digitalWrite(BIA, 0);                          //Left motor forward STOP
        digitalWrite(BIB, 0);                         //Left motor backward STOP
        digitalWrite(buzActPin,LOW);            //Turn annoying sound off BUZZER
      break;                                                        //Ready exit
    } //End of switch (results.value)      Perform value received from TV remote
} //Exit performAction ---------------------------------------------------------





void readAccupack() { //Set accuStatus according to voltage BATTERY ************
  accuReading = analogRead(accuPin);   //Read value H=1/2 divider = 8,4 Vdc pack
  accuVdc =  accuReading * 0.93446;        //Recalculate including * 10 in a Vdc
  if (accuVdc > 30) {                           //Meaning batteries are inserted
    accuStatus = 5;                                //5=Accu is loaded to the max
    if (accuVdc < 700){    //This step is required for the icon on the dashboard
      accuStatus = 4;                                //4=Accu is normally loaded
    } //End of if (accuVdc < 700)
    if (accuVdc < accuVdcCharge) {     //If Vaccu drops below the charging level
      accuStatus = 3;      //Set that we need to recharge and that we will do so
      beep(10);                 //Create a beep (x5ms) with KY-012 active BUZZER
    } //End of if (accuVdc < accuVdcCharge)
  }else{ //Else of if (accuVdc > 30)
    accuStatus = 0;                                      //0=No accu is inserted
  } //End of if (accuVdc > 30)
} //Exit readAccupack ----------------------------------------------------------





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<255){
    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<255){
    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<255){
    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

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



void beep(uint8_t ms) {      //Create a beep (x5ms) with KY-012 active BUZZER **
  digitalWrite(buzActPin,HIGH);                                 //Turn on BUZZER
  while (ms > 0){                     //Timer of the duration of the beep BUZZER
    delay(5);                                         //Wait milliseconds BUZZER
    ms--;                              //Countdown untill we reached zero BUZZER
  }                 //Timer of the duration has been counted down to zero BUZZER
  digitalWrite(buzActPin,LOW);                  //Turn annoying sound off BUZZER
} //Exit beep ------------------------------------------------------------------




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




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





////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO MEGA 2560                                    //
// Board  -Atmel- PIN - Function          - External Connection          FUNC //
//                                                                            //
// CONNECTIONS RAILS RIGHT TOP: DIGITAL PWM<~> ****************************** //
// SCL    -  43 - PD0 - SCL/INT0          -                               TWI //
// SDA    -  44 - PD1 - SDA/INT1          -                               TWI //
// AREF   -  98 - REF - AREF              -                               REF //
// 13 PWM -  26 - PB7 - OC0A/OC1C/PCINT17 - LED Arduino LED_BUILTIN       PWM //
// 12 PWM -  25 - PB6 - OC1B/PCINT16      -                               PWM //
// 11 PWM -  24 - PB5 - OC1A/PCINT5       - AIA right motor forward       PWM //
// 10 PWM -  23 - PB4 - OC2A/PCINT4       - AIB right motor backward      PWM //
//  9 PWM -  18 - PH6 - OC2B              - BIA left motor forward        PWM //
//  8 PWM -  17 - PH5 - OC4C              - BIB left motor backward       PWM //
//                                                                            //
// CONNECTIONS RAILS RIGHT MIDDLE: DIGITAL PWM<~> *************************** //
//  7 PWM -  16 - PH4 - OC4B              -                               PWM //
//  6 PWM -  15 - PH3 - OC4A              - Buzzer activ                  PWM //
//  5 PWM -   5 - PE3 - OC3A/AIN1         -                               PWM //
//  4 PWM -   1 - PG5 - OC0B              -                               PWM //
//  3 PWM -   7 - PE5 - OC3C/INT5         -                               INT //
//  2 PWM -   6 - PE4 - OC3B/INT4         - IR TV remote receiver         INT //
//  1 TX0 -   3 - PE1 - TXD0              - Serial monitor PC             TX0 //
//  0 RX0 -   2 - PE0 - RXD0/PCINT8       - Serial monitor PC             RX0 //
//                                                                            //
// CONNECTIONS RAILS RIGHT BOTTOM: DIGITAL PWM<~> *************************** //
// 14 TX3 -  64 - PJ1 - TXD3/PCINT10      -                               TX3 //
// 15 RX3 -  63 - PJ0 - RXD3/PCINT9       -                               RX3 //
// 16 TX2 -  13 - PH1 - TXD2              -                               TX2 //
// 17 RX2 -  12 - PH0 - RXD2              -                               RX2 //
// 18 TX1 -  46 - PD3 - TXD1/INT3         -                               INT //
// 19 RX1 -  45 - PD2 - RXD1/INT2         -                               INT //
// 20 SDA -  44 - PD1 - SDA/INT1          -                               TWI //
// 21 SCL -  43 - PD0 - SCL/INT0          -                               TWI //
//                                                                            //
// CONNECTIONS RAILS LEFT TOP: POWER **************************************** //
// NC     -     -     -                   - Not Connected                     //
// IOREF  -     -     - 3.3/5Vdc          - Outputs controller voltage        //
// 5V     -   7 - VCC - VCC               -                               VCC //
// RES    -   1 - RES - PCINT14/RESET     -                               RES //
// 3.3V   -     -     -                   -                                   //
// 5V     -     -     -                   -                                   //
// GND    -     -     -                   -                                   //
// GND    -     -     -                   -                                   //
// Vin    -     -     - 7/9Vdc power in   -                                   //
//                                                                            //
// CONNECTIONS RAILS LEFT MIDDLE : ANALOG IN ******************************** //
// A0     -  97 - PF0 - ADC0              -                               ADC //
// A1     -  96 - PF1 - ADC1              - Battery test voltage divider  ADC //
// A2     -  95 - PF2 - ADC2              -                               ADC //
// A3     -  94 - PF3 - ADC3              -                               ADC //
// A4     -  93 - PF4 - ADC4/TCK          -                               ADC //
// A5     -  92 - PF5 - ADC5/TMS          -                               ADC //
// A6     -  91 - PF6 - ADC6/TDO          -                               ADC //
// A7     -  90 - PF7 - ADC7/TDI          -                               ADC //
//                                                                            //
// CONNECTIONS RAILS LEFT BOTTOM: ANALOG IN ********************************* //
// A8     -  89 - PK0 - ADC8/PCINT16      -                               ADC //
// A9     -  88 - PK1 - ADC9/PCINT17      -                               ADC //
// A10    -  87 - PK2 - ADC10/PCINT18     -                               ADC //
// A11    -  86 - PK3 - ADC11/PCINT19     -                               ADC //
// A12    -  85 - PK4 - ADC12/PCINT20     -                               ADC //
// A13    -  84 - PK5 - ADC13/PCINT21     -                               ADC //
// A14    -  83 - PK6 - ADC14/PCINT22     -                               ADC //
// A15    -  82 - PK7 - ADC15/PCINT23     -                               ADC //
//                                                                            //
// CONNECTIONS DOUBLE RAILS BOTTOM ****************************************** //
// Board  -Atmel- PIN - Function          - External Connection          FUNC //
// 5V     -     - 5Vdc- 5Vdc              -                               VCC //
// 5V     -     - 5Vdc- 5Vdc              -                               VCC //
// 22     -  78 - PA0 - AD0               -                               DIO //
// 23     -  77 - PA1 - AD1               -                               DIO //
// 24     -  76 - PA2 - AD2               -                               DIO //
// 25     -  75 - PA3 - AD3               -                               DIO //
// 26     -  74 - PA4 - AD4               -                               DIO //
// 27     -  73 - PA5 - AD5               -                               DIO //
// 28     -  72 - PA6 - AD6               -                               DIO //
// 29     -  71 - PA7 - AD7               -                               DIO //
// 30     -  60 - PC7 - A14               -                               DIO //
// 31     -  59 - PC6 - A15               -                               DIO //
// 32     -  58 - PC5 - A13               -                               DIO //
// 33     -  57 - PC4 - A12               -                               DIO //
// 34     -  56 - PC3 - A11               -                               DIO //
// 35     -  55 - PC2 - A10               -                               DIO //
// 36     -  54 - PC1 - A9                -                               DIO //
// 37     -  53 - PC0 - A8                -                               DIO //
// 38     -  50 - PD7 - T0                -                               DIO //
// 39     -  70 - PG2 - ALE               -                               DIO //
// 40     -  52 - PG1 - RD                -                               DIO //
// 41     -  51 - PG0 - WR                -                               DIO //
// 42     -  42 - PL7 -                   -                               DIO //
// 43     -  41 - PL6 -                   -                               DIO //
// 44     -  40 - PL5 - OC5C              - 3 Color led Red               PWM //
// 45     -  39 - PL4 - OC5B              - 3 Color led Green             PWM //
// 46     -  38 - PL3 - OC5A              - 3 Color led Blue              PWM //
// 47     -  37 - PL2 - T5                -                               DIO //
// 48     -  36 - PL1 - ICP5              -                               DIO //
// 49     -  35 - PL0 - ICP4              -                               DIO //
// 50     -  22 - PB3 - MISO/PCINT3       -                               SPI //
// 51     -  21 - PB2 - MOSI/PCINT2       -                               SPI //
// 52     -  20 - PB1 - SCK/PCINT1        -                               SPI //
// 53     -  19 - PB1 - SS/PCINT0         -                               SPI //
// GND    -     - GND - GND               -                               GND //
// GND    -     - GND - GND               -                               GND //
////////////////////////////////////////////////////////////////////////////////


//345678911234567892123456789312345678941234567895123456789612345678971234567898
// EEPROM MEMORY MAP:                                                         //
// 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  //
////////////////////////////////////////////////////////////////////////////////