////////////////////////////////////////////////////////////////////////////////
// Name:       mrBlueBumpersUno-01                                            //
// Platform:   Arduino UNO Rev3                                               //
// Created by: HARB, July 2018, GPL copyrights                                //
// 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
  //#ifdef RS232     //Only include these lines if the variable has been defined

  // Include the needed header files for the precompiler, no charge if not used-
  
  // Define precompiler variables, runs faster & doesn`t use RAM ---------------
  // Define PINS ---------------------------------------------------------------
  #define buzActPin  12         //Define which I/O pin connects the activ BUZZER
  #define ledGrePin   7      //3 Colour LED, to which pin connects DIO green LED
 
  #define Bumper1pin  8                //AS Achterkant Stuurboord blauw  BUMPER1
  #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 Bumper6pin  4                         //BA Bakboord Achter wit BUMPER6
  #define Bumper7pin A5                      //VB Voorkant bakboard geel BUMPER7
  #define Bumper8pin  2                   //VS Voorkant Stuurboord paars BUMPER8
  #define BumperXpin  3     //Define which DIO pin connects ring, red ALL BUMPER
  
  //Define VARIABLES -----------------------------------------------------------
  bool ledOnBoardVal = LOW;      //You choose HIGH-on or LOW-off for LED_BUILTIN
  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
  volatile byte stateBumpers = LOW;  //ISR LOW=not touched, HIGH=touched BUMPERS
  //word counter1      = 0;    //Used to count down time in stead of delay BUMPERS
  word counter2      = 0;    //Used to count down time in stead of delay BUMPERS
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  disable_jtag();         //Disable jtag to free port C, enabled by default JTAG
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  pinMode(ledGrePin, OUTPUT);         //Make the LED connection output GREEN LED
  pinMode(buzActPin, OUTPUT);          //Make the connection output ACTIV BUZZER

  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 BUMPERS

  //Start objects --------------------------------------------------------------
  noInterrupts();
  SwitchesLow();                         //Set all switches in low state BUMPERS
  waitWhile(500);  
  attachInterrupt(digitalPinToInterrupt(BumperXpin), ISRbumpers, FALLING); //INT
  //waitWhile(500);  
  //Test hardware and software -------------------------------------------------
  beep(1);                        //Create a test beep with KY-012 active BUZZER
  Serial.println("Setup completed");     //Show the user the setup is done RS232
  interrupts();
} //End of setup ---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  checkBumpers();              //Check if any interrupt has been induced BUMPERS
  toggle_ledOnBoard();          //Toggles the LED_BUILTIN  ON or OFF onboard LED
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER 


void checkBumpers(void){ ///Check if any interrupt has been induced BUMPERS ****
  if (stateBumpers == HIGH) {
    digitalWrite (ledGrePin, HIGH);
    waitWhile(60000);
    digitalWrite (ledGrePin, LOW);
    stateBumpers = LOW;              //ISR LOW=not touched, HIGH=touched BUMPERS
    //whichSwitch();    //1 by 1, Use in combination with Serial Monitor BUMPERS
    //anySwitch(); //All together Use in combination with Serial Monitor BUMPERS
  }
} //Exit checkBumpers ----------------------------------------------------------



void waitWhile(word counter1){ //Count to kill time without using an interrupt *
  while (counter1 > 0){
    counter2 = 60000;
    while (counter2 > 0){  
      counter2 = counter2 - 1;
      counter2 = counter2 * 1;
    }
    counter1 = counter1 - 1;  
  }
}


void ISRbumpers(void){ //INTERRUPT SERVICE ROUTINE BUMPERS *********************
  stateBumpers = HIGH;               //ISR LOW=not touched, HIGH=touched BUMPERS
  //noInterrupts();
  //detachInterrupt(INT0); //Not possible with pinnumbers Pin2=INT0, Pin3=INT1 INT
} //Exit ISRbumpers ------------------------------------------------------------



void anySwitch(void){ //Use in combination with Serial Monitor *****************
  SwitchesLow();                                 //Set all switches in low state
  delay(3);                                    //It takes some time to stabilize
  valBumper1 = digitalRead (BumperXpin);
  Serial.print ("All:");
  Serial.println (valBumper1);
} //Exit testSwitches ----------------------------------------------------------



void SwitchesLow(void){ //Set all switches in LOW state ************************
  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 **********************
  digitalWrite(Bumper1pin, HIGH);                    //Achter Stuurboord BUMPER1
  digitalWrite(Bumper2pin, HIGH);                      //Achter Bakboord BUMPER2
  digitalWrite(Bumper3pin, HIGH);//Pull stuurboord rechts LOW BumperXpin BUMPER3
  digitalWrite(Bumper4pin, HIGH);//Pull stuurboord rechts LOW BumperXpin BUMPER4
  digitalWrite(Bumper5pin, HIGH);//Pull stuurboord rechts LOW BumperXpin BUMPER5
  digitalWrite(Bumper6pin, HIGH);//Pull stuurboord rechts LOW BumperXpin BUMPER6
  digitalWrite(Bumper7pin, HIGH);//Pull stuurboord rechts LOW BumperXpin BUMPER7          
  digitalWrite(Bumper8pin, HIGH);//Pull stuurboord rechts LOW BumperXpin BUMPER8
} //Exit SwitchesHigh ----------------------------------------------------------


void whichSwitch(void){ //Use in combination with Serial Monitor ***************
  SwitchesHigh();                               //Set all switches in high state
  digitalWrite(Bumper1pin, LOW);          //AS = Achter Stuurboord blauw BUMPER1
  delay(3);                                    //It takes some time to stabilize
  valBumper1 = digitalRead (BumperXpin);
  digitalWrite(Bumper1pin, HIGH);         //AS = Achter Stuurboord blauw BUMPER1
  Serial.print ("1:");
  Serial.print (valBumper1);

  digitalWrite(Bumper2pin, LOW);             //AB = Achter Bakboord roze BUMPER2
  delay(3);                                    //It takes some time to stabilize
  valBumper2 = digitalRead (BumperXpin);
  digitalWrite(Bumper2pin, HIGH);            //AB = Achter Bakboord roze BUMPER2
  Serial.print (" 2:");
  Serial.print (valBumper2);

  digitalWrite(Bumper3pin, LOW);          //SA = Stuurboord Achter groen BUMPER3
  delay(3);                                    //It takes some time to stabilize
  valBumper3 = digitalRead (BumperXpin);
  digitalWrite(Bumper3pin, HIGH);         //SA = Stuurboord Achter groen BUMPER3
  Serial.print (" 3:");
  Serial.print (valBumper3);

  digitalWrite(Bumper4pin, LOW);            //SV = Stuurboord Voor grijs BUMPER4
  delay(3);                                    //It takes some time to stabilize
  valBumper4 = digitalRead (BumperXpin);
  digitalWrite(Bumper4pin, HIGH);           //SV = Stuurboord Voor grijs BUMPER4
  Serial.print (" 4:");
  Serial.print (valBumper4);

  digitalWrite(Bumper5pin, LOW);              //BV = Bakboord Voor bruin BUMPER5
  delay(3);                                    //It takes some time to stabilize
  valBumper5 = digitalRead (BumperXpin);
  digitalWrite(Bumper5pin, HIGH);             //BV = Bakboord Voor bruin BUMPER5
  Serial.print (" 5:");
  Serial.print (valBumper5);

  digitalWrite(Bumper6pin, LOW);              //BA = Bakboord Achter wit BUMPER6
  delay(3);                                    //It takes some time to stabilize
  valBumper6 = digitalRead (BumperXpin);
  digitalWrite(Bumper6pin, HIGH);             //BA = Bakboord Achter wit BUMPER6
  Serial.print (" 6:");
  Serial.print (valBumper6);

  digitalWrite(Bumper7pin, LOW);             //VB = Voor Bakboord yellow BUMPER7
  delay(3);                                    //It takes some time to stabilize
  valBumper7 = digitalRead (BumperXpin);
  digitalWrite(Bumper7pin, HIGH);            //VB = Voor Bakboord yellow BUMPER7
  Serial.print (" 7:");
  Serial.print (valBumper7);

  digitalWrite(Bumper8pin, LOW);           //VS = Voor Stuurboord purple BUMPER8
  delay(3);                                    //It takes some time to stabilize
  valBumper8 = digitalRead (BumperXpin);
  digitalWrite(Bumper8pin, HIGH);          //VS = Voor Stuurboord purple BUMPER8
  Serial.print (" 8:");
  Serial.println (valBumper8);
} //Exit testSwitches ----------------------------------------------------------



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 beep(uint8_t ms) { //Create a beep with KY-012 active buzzer **************
  digitalWrite(buzActPin,HIGH);                                 //Turn buzzer on
  while (ms > 0){                            //Timer of the duration of the beep
    delay(5);                                                //Wait milliseconds
    ms--;                                     //Countdown untill we reached zero
  }            //Timer of the duration of the beep has been counted down to zero
  digitalWrite(buzActPin,LOW);     //Turn annoying buzzer off as fast as you can
} //Exit beep ------------------------------------------------------------------



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



////////////////////////////////////////////////////////////////////////////////
// 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 Arduino      SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       -                          SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - Motordriver A-IB green   PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - Motordriver A-IA purple  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   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       -                          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       -                          ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      -                          ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      -                          ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  -                          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