////////////////////////////////////////////////////////////////////////////////
// Name:       mrBlueChassissUno-01                                           //
// Platform:   Arduino UNO Rev3                                               //
// Created by: HARB rboek2@gmail.com september 2018 GPL copyrights            //
// Robot:   http://robotigs.com/robotigs/includes/bots_header.php?idbot=4     //
// IRreceiv:http://robotigs.com/robotigs/includes/parts_header.php?idpart=10  //
// Bumpers :http://robotigs.com/robotigs/includes/parts_header.php?idpart=91  //
////////////////////////////////////////////////////////////////////////////////


// 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 COMPILER
  //#ifdef RS232     //Lines only included if Serial Monitor is defined COMPILER
  //#endif                               //End of conditional compiling COMPILER

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

  // Define precompiler variables, runs faster & doesn`t use RAM ---------------
  // Define PINS ---------------------------------------------------------------
  #define RCDpin      0            //Rcd Receive serial connection yellow BRAINS
  #define TXDpin      1             //Txd Transmit serial connection blue BRAINS
  #define BumperXpin  2    //Define which INT1 pin connects ring, red ALL BUMPER
  #define ledGrePin   3      //3 Colour LED, to which pin connects PWM green LED
  #define Bumper8pin  4              //VS Voorkant Stuurboord DIO  paars BUMPER8
  #define BIA         5  //Define which pin connects to pin B-IA green PWM MOTOR
  #define BIB         6   //Define which pin connects to pin B-IB blue PWM MOTOR
  #define Bumper6pin  7                         //BA Bakboord Achter wit BUMPER6
  #define Bumper1pin  8                //AS Achterkant Stuurboord blauw  BUMPER1
  #define RECV_PIN    9        //Define which DIO pin connects white IR RECEIVER
  #define AIA        10 //Define which pin connects to pin A-IA orange PWM MOTOR
  #define AIB        11 //Define which pin connects to pin A-IB yellow PWM MOTOR
  #define buzActPin  12   //Define which I/O pin connects the activ white BUZZER
  #define ledRedPin  13           //3 Colour LED, which pin connects DIO red LED
  #define ledBluPin  99       //3 Colour LED connects DIO blue LED NOT CONNECTED
  #define powerInPin A0        //Define which ADC pin connects purple CHECKPOWER
  #define Bumper2pin A1                    //AB Achterkant Bakboord roze BUMPER2
  #define Bumper3pin A2                     //SA Stuurboard achter groen BUMPER3
  #define Bumper4pin A3                       //SV Stuurboord Voor grijs BUMPER4
  #define Bumper5pin A4                         //BV Bakboord Voor bruin BUMPER5
  #define Bumper7pin A5                      //VB Voorkant bakboard geel BUMPER7

  // Include 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

  //Define VARIABLES -----------------------------------------------------------
  bool brainsMounted = false;     //It should detect itself on RS232 port BRAINS
  int  brainsCommand = 0;      //0=Nothing, all others are drive commands BRAINS
  int  brainsright   = 0;     //0=Nothing, speed setting stuurboord motor BRAINS
  int  brainsleft    = 0;       //0=Nothing, speed setting bakboord motor BRAINS
  bool ledOnBoardVal = LOW;      //You choose HIGH-on or LOW-off for LED_BUILTIN
  bool ledGreenVal   = LOW;        //You choose HIGH-on or LOW-off for GREEN LED
  int  ledGreenDim   = 30;                         //Dim between 0-255 GREEN LED
  long IRlastKey     = ' ';         //Last key received by Infra Red IR RECEIVER
  byte userSpeed     = 255;  //Speed for all driving  actions by the user MOTORS
  word blinkTimer    = 10;             //Cycles after which a toggle is made LED
  word blinkCntr     = 1;                //Used to count down the blinkTimer LED
  word powerReading  = 0;   //Contains last measurement of the powerpack VOLTAGE
  word powerVoltage  = 0;   //Contains last measurement of the powerpack VOLTAGE
  int  switchActiv   = 0; //0=Nothing, all others are activ switch number BUMPER
  word msBumpAction  = 1000;   //Milliseconds of reaction after detection BUMPER
  bool valBumperx    = 0;            //All bumpers to be received at once BUMPER
  bool valBumper1    = 0;              //AS Achterkant Stuurboord blauw  BUMPER1
  bool valBumper2    = 0;                  //AB Achterkant Bakboord roze BUMPER2
  bool valBumper3    = 0;                   //SA Stuurboard achter groen BUMPER3
  bool valBumper4    = 0;                     //SV Stuurboord Voor grijs BUMPER4
  bool valBumper5    = 0;                       //BV Bakboord Voor bruin BUMPER5
  bool valBumper6    = 0;                       //BA Bakboord Achter wit BUMPER6
  bool valBumper7    = 0;                    //VB Voorkant bakboard geel BUMPER7
  bool valBumper8    = 0;                 //VS Voorkant Stuurboord paars BUMPER8

  #ifdef Keyes                       //Define BUTTONS remote control IR RECEIVER
    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                   //Define BUTTONS remote control IR RECEIVER
    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;           //Clear buffer of startup errors IR RECEIVER
//END OF PRECOMPILER OPTIONS ---------------------------------------------------



void setup() { //Setup runs once ***********************************************
  disable_jtag();         //Disable jtag to free port C, enabled by default JTAG
  #ifdef RS232       //Lines only included if Serial Monitor is defined COMPILER
    Serial.begin(9600);     //Nothing more needed to function for SERIAL MONITOR
  #endif                                 //End of conditional compiling COMPILER
  pinMode(buzActPin, OUTPUT);          //Make the connection output ACTIV BUZZER

  pinMode(ledRedPin, OUTPUT);           //Make the LED connection output RED LED
  pinMode(ledGrePin, OUTPUT);         //Make the LED connection output GREEN LED
  pinMode(ledBluPin, OUTPUT);          //Make the LED connection output BLUE LED
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard BUILTIN LED

  pinMode(AIA, OUTPUT);             //Motordriver pins are all output pins MOTOR
  pinMode(AIB, OUTPUT);             //Motordriver pins are all output pins MOTOR
  pinMode(BIA, OUTPUT);             //Motordriver pins are all output pins MOTOR
  pinMode(BIB, OUTPUT);             //Motordriver pins are all output pins MOTOR

  pinMode(Bumper1pin, OUTPUT);         //AS Achterkant Stuurboord blauw  BUMPER1
  pinMode(Bumper2pin, OUTPUT);             //AB Achterkant Bakboord roze BUMPER2
  pinMode(Bumper3pin, OUTPUT);              //SA Stuurboard achter groen BUMPER3
  pinMode(Bumper4pin, OUTPUT);                //SV Stuurboord Voor grijs BUMPER4
  pinMode(Bumper5pin, OUTPUT);                  //BV Bakboord Voor bruin BUMPER5
  pinMode(Bumper6pin, OUTPUT);                  //BA Bakboord Achter wit BUMPER6
  pinMode(Bumper7pin, OUTPUT);               //VB Voorkant bakboard geel BUMPER7
  pinMode(Bumper8pin, OUTPUT);            //VS Voorkant Stuurboord paars BUMPER8
  pinMode(BumperXpin, INPUT_PULLUP);    //Matches all bumperswitches rood BUMPER

  //Start objects --------------------------------------------------------------
  SwitchesLow();                          //Set all switches in LOW state BUMPER
  irrecv.enableIRIn();                                   //Start the IR RECEIVER

  //Test hardware and software -------------------------------------------------
  test_LEDs_dio(200);          //ON-OFF (ms) for each 3 color common cathode LED
  #ifdef RS232       //Lines only included if Serial Monitor is defined COMPILER
    Serial.println("Setup completed");   //Show the user the setup is done RS232
  #endif                                 //End of conditional compiling COMPILER
  beep(1);                 //Create a test beep (x5ms) with KY-012 active BUZZER
} //End of setup ---------------------------------------------------------------




void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  if (brainsMounted == false){     //Meaning no extra hardware is mounted BRAINS
    checkPower();                               //Check the battery pack VOLTAGE
    readIRreceiver();  //Read IR remote control as often as possible IR RECEIVER
    readBumper();    //If any switch pressed, halt and return switchActiv BUMPER
    driveAction();              //Set the engines according to the logics MOTORS
    toggleLED();            //Toggles the LED_BUILTIN at time intervals only LED 
  }else{                              //Meaning extra hardware is mounted BRAINS
    toggleLED();            //Toggles the LED_BUILTIN at time intervals only LED 
    //readBrains();                     //Ask for driving instructions on BRAINS
  } //End of if (brainsMounted == true)          //Meaning extra hardware BRAINS
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER



void checkPower(void) { //Check the battery pack VOLTAGE ***********************
  powerReading = analogRead (powerInPin);                //Set at H=1/2 read ADC
  powerVoltage = map(powerReading, 0, 1023, 0, 1000);   //Map into a voltage ADC
  #ifdef RS232             //Only included if Serial Monitor is defined COMPILER
    Serial.print ("Voltage 18650 pack:");          //Show text on SERIAL MONITOR
    Serial.println (powerVoltage);                 //Show text on SERIAL MONITOR
  #endif                                 //End of conditional compiling COMPILER
  if (powerVoltage<640){             //Safety to not to drain the Litium BATTERY
    beep(1);               //Create a test beep (x5ms) with KY-012 active BUZZER
  }
} //Exit checkPower ------------------------------------------------------------


void readBrains(void) { //Ask for driving instructions on BRAINS ***************
} //Exit readBrains ------------------------------------------------------------




void readIRreceiver(void) { //Read as often as possible IR RECEIVER ************
  if (irrecv.decode(&results)) {    //If we received anything then lets check it
    IRlastKey = results.value;         //Read the value of the last received key
    irrecv.resume();             //Prepair to receive the next value IR RECEIVER
    delay(150);                         //Pause (ms) needed to reset IR RECEIVER
        //#ifdef RS232     //Only included if Serial Monitor is defined COMPILER
        //  Serial.print ("IRlastKey:");           //Show text on SERIAL MONITOR
        //  Serial.println (IRlastKey);            //Show text on SERIAL MONITOR
        //#endif                         //End of conditional compiling COMPILER
  } else {                   //Meaning no key at all is pressed at the TV remote
    IRlastKey = 0;        //Reset the value of the last received key IR RECEIVER
    halt();                                    //Stop all engine movements MOTOR
    digitalWrite(buzActPin,LOW);                        //Always turn off BUZZER
  } //End of if (irrecv.decode(&results)     receive anything then lets check it
} //Exit of readIRreceiver              Receives data from the IR remote control



void driveAction(void){ //Set the engines according to the logics MOTORS *******
  if (switchActiv > 0){            //Meaning some switch has been pressed BUMPER
      switch (switchActiv){           //So what switchvalue did I receive BUMPER

        case 1:                        //AS Achterkant Stuurboord blauw  BUMPER1
          forward(userSpeed, userSpeed);    //Both engines (0-255) DRIVE FORWARD
          delay(msBumpAction);    //Pause needed to move to safe position MOTORS
          //#ifdef RS232   //Only included if Serial Monitor is defined COMPILER
          //  Serial.print ("Switch:");            //Show text on SERIAL MONITOR
          //  Serial.println (switchActiv);        //Show text on SERIAL MONITOR
          //#endif                       //End of conditional compiling COMPILER
        break;                            //Jump to the end of this switch BREAK
        
        case 2:                            //AB Achterkant Bakboord roze BUMPER2
          forward(userSpeed, userSpeed);    //Both engines (0-255) DRIVE FORWARD
          delay(msBumpAction);    //Pause needed to move to safe position MOTORS
          //#ifdef RS232   //Only included if Serial Monitor is defined COMPILER
          //  Serial.print ("Switch:");            //Show text on SERIAL MONITOR
          //  Serial.println (switchActiv);        //Show text on SERIAL MONITOR
          //#endif                       //End of conditional compiling COMPILER
        break;                            //Jump to the end of this switch BREAK

        case 3:                             //SA Stuurboard achter groen BUMPER3
          forward(userSpeed, userSpeed);    //Both engines (0-255) DRIVE FORWARD
          delay(msBumpAction);    //Pause needed to move to safe position MOTORS
          //#ifdef RS232   //Only included if Serial Monitor is defined COMPILER
          //  Serial.print ("Switch:");            //Show text on SERIAL MONITOR
          //  Serial.println (switchActiv);        //Show text on SERIAL MONITOR
          //#endif                       //End of conditional compiling COMPILER
        break;                            //Jump to the end of this switch BREAK

        case 4:                               //SV Stuurboord Voor grijs BUMPER4
          backward(userSpeed, userSpeed);                 //Drive (0-255) MOTORS
          delay(msBumpAction);    //Pause needed to move to safe position MOTORS
          //#ifdef RS232   //Only included if Serial Monitor is defined COMPILER
          //  Serial.print ("Switch:");            //Show text on SERIAL MONITOR
          //  Serial.println (switchActiv);        //Show text on SERIAL MONITOR
          //#endif                       //End of conditional compiling COMPILER
        break;                            //Jump to the end of this switch BREAK

        case 5:                                 //BV Bakboord Voor bruin BUMPER5
          backward(userSpeed, userSpeed);                 //Drive (0-255) MOTORS
          delay(msBumpAction);    //Pause needed to move to safe position MOTORS
          //#ifdef RS232   //Only included if Serial Monitor is defined COMPILER
          //  Serial.print ("Switch:");            //Show text on SERIAL MONITOR
          //  Serial.println (switchActiv);        //Show text on SERIAL MONITOR
          //#endif                       //End of conditional compiling COMPILER
        break;                            //Jump to the end of this switch BREAK

        case 6:                                 //BA Bakboord Achter wit BUMPER6
          forward(userSpeed, userSpeed);    //Both engines (0-255) DRIVE FORWARD
          delay(msBumpAction);    //Pause needed to move to safe position MOTORS
          //#ifdef RS232   //Only included if Serial Monitor is defined COMPILER
          //  Serial.print ("Switch:");            //Show text on SERIAL MONITOR
          //  Serial.println (switchActiv);        //Show text on SERIAL MONITOR
          //#endif                       //End of conditional compiling COMPILER
        break;                            //Jump to the end of this switch BREAK

        case 7:                              //VB Voorkant bakboard geel BUMPER7
          backward(userSpeed, userSpeed);                 //Drive (0-255) MOTORS
          delay(msBumpAction);    //Pause needed to move to safe position MOTORS
          //#ifdef RS232   //Only included if Serial Monitor is defined COMPILER
          //  Serial.print ("Switch:");            //Show text on SERIAL MONITOR
          //  Serial.println (switchActiv);        //Show text on SERIAL MONITOR
          //#endif                       //End of conditional compiling COMPILER
        break;                            //Jump to the end of this switch BREAK

        case 8:                           //VS Voorkant Stuurboord paars BUMPER8
          backward(userSpeed, userSpeed);                 //Drive (0-255) MOTORS
          delay(msBumpAction);    //Pause needed to move to safe position MOTORS
          //#ifdef RS232   //Only included if Serial Monitor is defined COMPILER
          //  Serial.print ("Switch:");            //Show text on SERIAL MONITOR
          //  Serial.println (switchActiv);        //Show text on SERIAL MONITOR
          //#endif                       //End of conditional compiling COMPILER
        break;                            //Jump to the end of this switch BREAK

        default:     //If NO button case matched, then perform this case DEFAULT
          break;                          //Jump to the end of this switch BREAK
    } //End of switch (switchActiv)                                       BUMPER
  }else{                                  //Meaning no switch was pressed BUMPER
    if (IRlastKey != 0){         //Meaning some key has been pressed IR RECEIVER
      switch (IRlastKey){         //So what value did I receive from IR RECEIVER

        case -1:      //Some button is being kept on pressed by user IR RECEIVER
          break;         //Do nothing, just jump to the end of this switch BREAK

        case butUp:                 //User has pressed the button up IR RECEIVER
          forward(userSpeed, userSpeed);    //Both engines (0-255) DRIVE FORWARD
          break;                          //Jump to the end of this switch BREAK

        case butRt:              //User has pressed the button right IR RECEIVER
          right(userSpeed, userSpeed);                //Drive (0-255) TURN RIGHT
          break;                          //Jump to the end of this switch BREAK

        case butDn:               //User has pressed the button down IR RECEIVER
          backward(userSpeed, userSpeed);  //Drive (0-255) both engines BACKWARD
          break;                          //Jump to the end of this switch BREAK

        case butLf:               //User has pressed the button left IR RECEIVER
          left(userSpeed, userSpeed);                  //Drive (0-255) TURN LEFT
          break;                          //Jump to the end of this switch BREAK

        case butOk:                 //User has pressed the OK button IR RECEIVER
          digitalWrite(buzActPin,HIGH);                         //Turn on BUZZER
          break;                          //Jump to the end of this switch BREAK

        default:     //If NO button case matched, then perform this case DEAFULT
          break;                          //Jump to the end of this switch BREAK
      } //End of switch (results.value)  What value did I receive from TV remote
    }else{        //Meaning we finally can perform a received instruction BRAINS



      
    } //End of if (IRlastKey != 0){Meaning some key has been pressed IR RECEIVER
  } //End of Meaning no bumperswitch has been pressed
} //Exit driveAction -----------------------------------------------------------




void testDrive(byte speed){ //Spotdrive to test engines (0-255) MOTORS *********
    //testDrive(userSpeed);            /Spotdrive to test engines (0-255) MOTORS
  forward(speed, speed);                    //Both engines (0-255) DRIVE FORWARD
  delay(1000);                                       //Do so for 1 second MOTORS
  right(speed, speed);                                    //Drive (0-255) MOTORS
  delay(1000);                                       //Do so for 1 second MOTORS
  backward(speed, speed);                                 //Drive (0-255) MOTORS
  delay(1000);                                       //Do so for 1 second MOTORS
  left(speed, speed);                                     //Drive (0-255) MOTORS
  delay(1000);                                       //Do so for 1 second MOTORS
  halt();                                     //Stop all engine movements MOTORS
} //Exit testDrive -------------------------------------------------------------


void forward(byte speeds, byte speedb) { //Both engines (0-255) DRIVE FORWARD **
  analogWrite(AIB, 0);  //AIB Right Backward, NEVER turn on AIA and AIB together
  analogWrite(AIA, speeds);       //AIA Stuurboord Forward is driving full speed
  analogWrite(BIB, 0);    //BIB Bakboord Backward, NEVER on BIA and BIB together
  analogWrite(BIA, speedb);        //BIA Backboord Forward is driving full speed
} //End of forward(byte speed) Drive (0-255) both engines forward MOTOR --------


void left(byte speeds, byte speedb) { //Drive (0-255) engines TURN LEFT ********
  analogWrite(AIA, 0);   //AIA Right Forward, NEVER turn on AIA and AIB together
  analogWrite(AIB, speeds);           //AIB Right Backward is driving full speed
  analogWrite(BIB, 0);   //BIB Left Backward, NEVER turn on BIA and BIB together
  analogWrite(BIA, speedb);             //BIA Left Forward is driving full speed
} //End of left(byte speed) Drive (0-255) TURN LEFT --------------------------


void backward(byte speeds, byte speedb) {  //Both engines (0-255) DRIVE BACKWARD
  analogWrite(AIA, 0);   //AIA Right Forward, NEVER turn on AIA and AIB together
  analogWrite(AIB, speeds);           //AIB Right Backward is driving full speed
  analogWrite(BIA, 0);    //BIA Left Forward, NEVER turn on BIA and BIB together
  analogWrite(BIB, speedb);            //BIB Left Backward is driving full speed
} //End of backward(byte speed) Drive (0-255) DRIVE BACKWARD -------------------


void right(byte speeds, byte speedb) { //Drive (0-255) engines TURN RIGHT ****** 
  analogWrite(AIB, 0);  //AIB Right Backward, NEVER turn on AIA and AIB together
  analogWrite(AIA, speeds);            //AIA Right Forward is driving full speed
  analogWrite(BIA, 0);    //BIA Left Forward, NEVER turn on BIA and BIB together
  analogWrite(BIB, speedb);            //BIB Left Backward is driving full speed
} //End of right(byte speed) Drive (0-255) TURN RIGHT --------------------------


void halt() { //Stop all engine movements MOTORS *******************************
  analogWrite(AIA, 0);               //Never turn on AIA an AIB at the same time
  analogWrite(AIB, 0);               //Never turn on AIA an AIB at the same time
  analogWrite(BIA, 0);               //Never turn on BIA an BIB at the same time
  analogWrite(BIB, 0);               //Never turn on BIA an BIB at the same time
} //End of halt() Stop all engine movements MOTOR ------------------------------






void readBumper(void){ //If switch pressed, halt&return switchActiv BUMPER *****
  valBumperx = digitalRead (BumperXpin);    //Check if any switch pressed BUMPER
  //#ifdef RS232     //Lines only included if Serial Monitor is defined COMPILER
  //    Serial.print ("All:");                     //Show text on SERIAL MONITOR
  //    Serial.println (valBumperx);               //Show text on SERIAL MONITOR
  //#endif                               //End of conditional compiling COMPILER
  if (valBumperx == HIGH) {                 //Meaning no switches pressed BUMPER
    switchActiv = 0;      //0=Nothing, all others are activ switch number BUMPER
  }else{             //Meaning at least on switch has been pressed at the BUMPER
    halt();                                    //Stop all engine movements MOTOR
    whichSwitch();                   //Test which switch, set switchActiv BUMPER
  } //End of if (valBumperx==HIGH){         //Meaning no switches pressed BUMPER
} //Exit testAllSwitches -------------------------------------------------------


void whichSwitch(void){ //Test which switch, set switchActiv BUMPER ************
  SwitchesHigh();                               //Set all switches in high state
  switchActiv = 0;                           //Set answer to switchnumber BUMPER
      
  digitalWrite(Bumper1pin, LOW); //Make activ AS=Achter Stuurboord blauw BUMPER1
  valBumper1 = digitalRead (BumperXpin);       //LOW if pressed else HIGH BUMPER
  digitalWrite(Bumper1pin, HIGH);               //RESET switch to inactiv BUMPER
  if (valBumper1 == LOW){                     //If this switch is pressed BUMPER
    switchActiv = 1;                         //Set answer to switchnumber BUMPER
  } //End of if (valBumper1==LOW)               If this switch is pressed BUMPER

  digitalWrite(Bumper2pin, LOW);    //Make activ AB=Achter Bakboord roze BUMPER2
  valBumper2 = digitalRead (BumperXpin);       //LOW if pressed else HIGH BUMPER
  digitalWrite(Bumper2pin, HIGH);               //RESET switch to inactiv BUMPER
  if (valBumper2 == LOW){                     //If this switch is pressed BUMPER
    switchActiv = 2;                         //Set answer to switchnumber BUMPER
  } //End of if (valBumper2==LOW)               If this switch is pressed BUMPER

  digitalWrite(Bumper3pin, LOW); //Make activ SA=Stuurboord Achter groen BUMPER3
  valBumper3 = digitalRead (BumperXpin);       //LOW if pressed else HIGH BUMPER
  digitalWrite(Bumper3pin, HIGH);               //RESET switch to inactiv BUMPER
  if (valBumper3 == LOW){                     //If this switch is pressed BUMPER
    switchActiv = 3;                         //Set answer to switchnumber BUMPER
  } //End of if (valBumper3==LOW)               If this switch is pressed BUMPER

  digitalWrite(Bumper4pin, LOW);   //Make activ SV=Stuurboord Voor grijs BUMPER4
  valBumper4 = digitalRead (BumperXpin);       //LOW if pressed else HIGH BUMPER
  digitalWrite(Bumper4pin, HIGH);               //RESET switch to inactiv BUMPER
  if (valBumper4 == LOW){                     //If this switch is pressed BUMPER
    switchActiv = 4;                         //Set answer to switchnumber BUMPER
  } //End of if (valBumper4==LOW)               If this switch is pressed BUMPER

  digitalWrite(Bumper5pin, LOW);     //Make activ BV=Bakboord Voor bruin BUMPER5
  valBumper5 = digitalRead (BumperXpin);       //LOW if pressed else HIGH BUMPER
  digitalWrite(Bumper5pin, HIGH);               //RESET switch to inactiv BUMPER
  if (valBumper5 == LOW){                     //If this switch is pressed BUMPER
    switchActiv = 5;                         //Set answer to switchnumber BUMPER
  } //End of if (valBumper5==LOW)               If this switch is pressed BUMPER

  digitalWrite(Bumper6pin, LOW);     //Make activ BA=Bakboord Achter wit BUMPER6
  valBumper6 = digitalRead (BumperXpin);       //LOW if pressed else HIGH BUMPER
  digitalWrite(Bumper6pin, HIGH);               //RESET switch to inactiv BUMPER
  if (valBumper6 == LOW){                     //If this switch is pressed BUMPER
    switchActiv = 6;                         //Set answer to switchnumber BUMPER
  } //End of if (valBumper6==LOW)               If this switch is pressed BUMPER

  digitalWrite(Bumper7pin, LOW);    //Make activ VB=Voor Bakboord yellow BUMPER7
  valBumper7 = digitalRead (BumperXpin);       //LOW if pressed else HIGH BUMPER
  digitalWrite(Bumper7pin, HIGH);               //RESET switch to inactiv BUMPER
  if (valBumper7 == LOW){                     //If this switch is pressed BUMPER
    switchActiv = 7;                         //Set answer to switchnumber BUMPER
  } //End of if (valBumper7==LOW)               If this switch is pressed BUMPER

  digitalWrite(Bumper8pin, LOW);  //Make activ VS=Voor Stuurboord purple BUMPER8
  valBumper8 = digitalRead (BumperXpin);       //LOW if pressed else HIGH BUMPER
  digitalWrite(Bumper8pin, HIGH);               //RESET switch to inactiv BUMPER
  if (valBumper8 == LOW){                     //If this switch is pressed BUMPER
    switchActiv = 8;                         //Set answer to switchnumber BUMPER
  } //End of if (valBumper8==LOW)               If this switch is pressed BUMPER

  SwitchesLow();                        //RESET all switches in LOW state BUMPER
  
 // #ifdef RS232     //Lines only included if Serial Monitor is defined COMPILER
 //   Serial.print ("1:");                         //Show text on SERIAL MONITOR
 //   Serial.print (valBumper1);                   //Show text on SERIAL MONITOR
 //   Serial.print (" 2:");                        //Show text on SERIAL MONITOR
 //   Serial.print (valBumper2);                   //Show text on SERIAL MONITOR
 //   Serial.print (" 3:");                        //Show text on SERIAL MONITOR
 //   Serial.print (valBumper3);                   //Show text on SERIAL MONITOR
 //   Serial.print (" 4:");                        //Show text on SERIAL MONITOR
 //   Serial.print (valBumper4);                   //Show text on SERIAL MONITOR
 //   Serial.print (" 5:");                        //Show text on SERIAL MONITOR
 //   Serial.print (valBumper5);                   //Show text on SERIAL MONITOR
 //   Serial.print (" 6:");                        //Show text on SERIAL MONITOR
 //   Serial.print (valBumper6);                   //Show text on SERIAL MONITOR
 //   Serial.print (" 7:");                        //Show text on SERIAL MONITOR
 //   Serial.print (valBumper7);                   //Show text on SERIAL MONITOR
 //   Serial.print (" 8:");                        //Show text on SERIAL MONITOR
 //   Serial.print (valBumper8);                   //Show text on SERIAL MONITOR
 //   Serial.print ("    TOUCHE:");                //Show text on SERIAL MONITOR
 //   Serial.println (switchActiv);                //Show text on SERIAL MONITOR
 // #endif                               //End of conditional compiling COMPILER
} //Exit whichSwitch -----------------------------------------------------------


void SwitchesLow(void){ //Set all switches in LOW state BUMPER *****************
  digitalWrite(Bumper1pin, LOW);       //AS Achterkant Stuurboord blauw  BUMPER1
  digitalWrite(Bumper2pin, LOW);           //AB Achterkant Bakboord roze BUMPER2
  digitalWrite(Bumper3pin, LOW);            //SA Stuurboard achter groen BUMPER3
  digitalWrite(Bumper4pin, LOW);              //SV Stuurboord Voor grijs BUMPER4
  digitalWrite(Bumper5pin, LOW);                //BV Bakboord Voor bruin BUMPER5
  digitalWrite(Bumper6pin, LOW);                //BA Bakboord Achter wit BUMPER6
  digitalWrite(Bumper7pin, LOW);             //VB Voorkant bakboard geel BUMPER7
  digitalWrite(Bumper8pin, LOW);          //VS Voorkant Stuurboord paars BUMPER8
} //Exit SwitchesLow -----------------------------------------------------------


void SwitchesHigh(void){ //Set all switches in HIGH state BUMPER ***************
  digitalWrite(Bumper1pin, HIGH);      //AS Achterkant Stuurboord blauw  BUMPER1
  digitalWrite(Bumper2pin, HIGH);          //AB Achterkant Bakboord roze BUMPER2
  digitalWrite(Bumper3pin, HIGH);           //SA Stuurboard achter groen BUMPER3
  digitalWrite(Bumper4pin, HIGH);             //SV Stuurboord Voor grijs BUMPER4
  digitalWrite(Bumper5pin, HIGH);               //BV Bakboord Voor bruin BUMPER5
  digitalWrite(Bumper6pin, HIGH);               //BA Bakboord Achter wit BUMPER6
  digitalWrite(Bumper7pin, HIGH);            //VB Voorkant bakboard geel BUMPER7
  digitalWrite(Bumper8pin, HIGH);         //VS Voorkant Stuurboord paars BUMPER8
} //Exit SwitchesHigh ----------------------------------------------------------





int toggleLED() { //Toggles the LED_BUILTIN at time intervals only LED *********
  if (blinkCntr == 0){           //Only perform if counter is counted down TIMER
    toggle_greenLED();                             //Toggles ON or OFF GREEN LED
    blinkCntr  =  blinkTimer;                          //RESET the counter TIMER 
  }else{                                //Meaning counter was not yet zero TIMER
    blinkCntr--;                                //Decrement of the counter TIMER
  } //End of if (counter==0)      Toggles the LED_BUILTIN at time interval TIMER
} //Exit toggleLED -------------------------------------------------------------




void test_LEDs_dio(word msWait){ //ON-OFF (ms) for each 3 color LED ************
  digitalWrite(ledRedPin, HIGH);                             //Switch ON RED LED
  delay (msWait);        //Blue LED is not connected in this robot, no pins left
  digitalWrite(ledRedPin, LOW);                              //Switch OF RED LED
  digitalWrite(ledGrePin, HIGH);                           //Switch ON GREEN LED
  delay (msWait);        //Blue LED is not connected in this robot, no pins left
  digitalWrite(ledGrePin, LOW);                            //Switch OF GREEN LED
} //Exit test_LEDs_dio ---------------------------------------------------------




void beep(uint8_t ms) { //Create a test 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_greenLED(void){ //Toggles ON or OFF GREEN LED **********************
  ledGreenVal = !ledGreenVal;                           //Toggle value GREEN LED
  digitalWrite(ledGrePin, ledGreenVal);                          //Set GREEN LED
} //Exit toggle_greenLED -------------------------------------------------------


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 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        - LED_BUILTIN & LED    red SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - LED Blue           white SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - Motordriver AIB   yellow PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - Motordriver AIA   orange PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       - irReceivPin       yellow PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  - Bumper1pin          blue DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      - Bumper6pin         white DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 - Motordriver BIB     blue PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   - Motordriver BIA    green PWM //
// ~4    -   6 - PD4 -   4 - PCINT20/XCK/T0    - Bumper8pin        yellow DIO //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 - LedGrePin          green PWM //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      - BumperXpin         green INT //
// TX->1 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor     Blauw TXD //
// RX<-0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor      Geel 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       - powerInPin        purple ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       - Bumper2pin          rose ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      - Bumper3pin         green ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      - Bumper4pin          grey ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  - Bumper5pin         brown TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  - Bumper7pin        yellow TWI //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898