////////////////////////////////////////////////////////////////////////////////
// Name:       HarbitronUno-01                                                //
// Platform:   Arduino UNO Rev3                                               //
// Created by: HARB rboek2@gmail.com januari 2019 GPL copyrights              //
// Based on:   http://www.arduino.cc/en/Tutorial/Midi                         //
// Robot:      http://robotigs.com/robotigs/includes/bots_header.php?idbot=21 //
//             Midi sending device. Supposed to be a simple instrument.       //
//             In this case to extend the Korg Triton Extreme, but you can    //
//             send any Wifi code you like.                                   //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************
  // Define precompiler variables, runs faster & doesn`t use RAM ---------------
  // Define PINS ---------------------------------------------------------------
  #define key1            11                        //Red keyboard key HARBITRON
  #define key2            A1                      //White keyboard key HARBITRON
  #define key3            A0                     //Yellow keyboard key HARBITRON
  #define key4             8                       //Blue keyboard key HARBITRON
  #define key5             9                      //Green keyboard key HARBITRON
  #define key6            10                       //Grey keyboard key HARBITRON
  #define switch1         12                          //Dubbelpolig om HARBITRON
  #define pot1            A2             //Keyboard potmeter 50k brown HARBITRON
  
  // Include the needed header files for the precompiler, no charge if not used-
  //Define VARIABLES -----------------------------------------------------------
  bool ledOnBoardVal =  LOW;     //You choose HIGH-on or LOW-off for LED_BUILTIN
  bool key1val       = HIGH;         //HIGH-not-pressed or LOW-pressed HARBITRON
  bool key2val       = HIGH;         //HIGH-not-pressed or LOW-pressed HARBITRON
  bool key3val       = HIGH;         //HIGH-not-pressed or LOW-pressed HARBITRON
  bool key4val       = HIGH;         //HIGH-not-pressed or LOW-pressed HARBITRON
  bool key5val       = HIGH;         //HIGH-not-pressed or LOW-pressed HARBITRON
  bool key6val       = HIGH;         //HIGH-not-pressed or LOW-pressed HARBITRON
  bool switch1val    = HIGH;                         //HIGH=ON LOW=OFF HARBITRON
  word pot1val       = 0;                    //1-1023 program selector HARBITRON
  //Initialize OBJECTS ---------------------------------------------------------
//END OF PRECOMPILER OPTIONS ---------------------------------------------------

void setup() { //Setup runs once ***********************************************
  disable_jtag();         //Disable jtag to free port C, enabled by default JTAG
  Serial.begin(31250);                                 //Set MIDI baud rate MIDI
  //Serial.begin(9600);                                //Set MIDI baud rate MIDI
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard BUILTIN LED
  pinMode(key1, INPUT_PULLUP);                                  //Blue HARBITRON
  pinMode(key2, INPUT_PULLUP);                                 //Green HARBITRON
  pinMode(key3, INPUT_PULLUP);                                 //White HARBITRON
  pinMode(key4, INPUT_PULLUP);                                //Orange HARBITRON
  pinMode(key5, INPUT_PULLUP);                                //Purple HARBITRON
  pinMode(key6, INPUT_PULLUP);                                //Yellow HARBITRON
  pinMode(switch1, INPUT_PULLUP);                               //Rose HARBITRON
  //Start objects --------------------------------------------------------------
  Triton_init();                              //Plays settings to Triton by MIDI
  //Test hardware and software -------------------------------------------------
} //End of setup ---------------------------------------------------------------

void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  //speelLadder();      //Plays a ladder of keys music to any instrument by MIDI
  toggle_ledOnBoard();           //Toggles the LED_BUILTIN  ON or OFF onboardLED

  if (key1val != digitalRead(key1)) {
    key1val = !key1val;                                           //Toggle value
    if (key1val == LOW){                            //Meaning the key is pressed
      Midi9byte(0x90, 0x43, 0x45, 0x48, 0x45, 0x4C, 0x45, 0x30, 0x45);       //Plays 4 notes
    }else{
      Midi9byte(0x80, 0x43, 0x00, 0x48, 0x00, 0x4C, 0x00, 0x30, 0x00);    //Releases 4 notes
    }
  }
  if (key2val != digitalRead(key2)) {
    key2val = !key2val;                                           //Toggle value
    if (key2val == LOW){                            //Meaning the key is pressed
      Midi9byte(0x90, 0x44, 0x45, 0x47, 0x45, 0x4C, 0x45, 0x34, 0x45);       //Plays 4 notes
    }else{
      Midi9byte(0x80, 0x44, 0x00, 0x47, 0x00, 0x4C, 0x00, 0x34, 0x00);    //Releases 4 notes
    }
  }
  if (key3val != digitalRead(key3)) {
    key3val = !key3val;                                           //Toggle value
    if (key3val == LOW){                            //Meaning the key is pressed
      Midi9byte(0x90, 0x45, 0x45, 0x48, 0x45, 0x4C, 0x45, 0x2D, 0x45);       //Plays 4 notes
    }else{
      Midi9byte(0x80, 0x45, 0x00, 0x48, 0x00, 0x4C, 0x00, 0x2D, 0x00);    //Releases 4 notes
    }
  }
  if (key4val != digitalRead(key4)) {
    key4val = !key4val;                                           //Toggle value
    if (key4val == LOW){                            //Meaning the key is pressed
      Midi9byte(0x90, 0x45, 0x45, 0x48, 0x45, 0x4D, 0x45, 0x29, 0x45);       //Plays 4 notes
    }else{
      Midi9byte(0x80, 0x45, 0x00, 0x48, 0x00, 0x4D, 0x00, 0x29, 0x00);    //Releases 4 notes
    }
  }
  if (key5val != digitalRead(key5)) {
    key5val = !key5val;                                           //Toggle value
    if (key5val == LOW){                            //Meaning the key is pressed
      Midi9byte(0x90, 0x45, 0x45, 0x48, 0x45, 0x4C, 0x45, 0x2D, 0x45);       //Plays 4 notes
    }else{
      Midi9byte(0x80, 0x45, 0x00, 0x48, 0x00, 0x4C, 0x00, 0x2D, 0x00);    //Releases 4 notes
    }
  }
  if (key6val != digitalRead(key6)) {
    key6val = !key6val;                                           //Toggle value
    if (key6val == LOW){                            //Meaning the key is pressed
      Midi2byte(0xC0, 120);  
        //Midi9byte(0x90, 0x45, 0x45, 0x48, 0x45, 0x4C, 0x45, 0x2D, 0x45);       //Plays 4 notes
    }else{
      Midi2byte(0xC0, 3);  
        //Midi9byte(0x80, 0x45, 0x00, 0x48, 0x00, 0x4C, 0x00, 0x2D, 0x00);    //Releases 4 notes
    }
  }
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER


void Triton_init(void){ //Plays settings to Triton by MIDI *********************
  Midi7byte(0xF0, 0x42, 0x30, 0x50, 0x4E, 0x00, 0xF7);           //Sys ex Triton - Change Mode to Combi

  Midi3byte(0xB0, 0x00, 0x00);          //Plays an entire event message to MIDI - selecteer combi bank D, LUKT NIET!!!!!!!!!!!!!!!!!!!!!!!!!
  Midi3byte(0xB0, 0x20, 0x03);          //Plays an entire event message to MIDI - selecteer combi bank D, LUKT NIET !!!!!!!!!!!!!!!!!!!!!!!!
  Midi2byte(0xC0, 110);                                //Verander de combi naar 3 - Selecteer Combination (0-127)
  
  Midi3byte(0xB0, 80, 00);                           //Set SW1: 0=OFF 80=ON MIDI
  Midi3byte(0xB0, 81, 80);                           //Set SW2: 0=OFF 80=ON MIDI
} //Exit Triton_init -----------------------------------------------------------





void speelLadder(void){ //Plays settings to Triton by MIDI ********************
  for (int note = 0x1E; note < 0x5A; note ++) {  // play notes from F#-0 (0x1E) to F#-5 (0x5A):
    Midi3byte(0x90, note, 0x45); //Note on channel 1 (0x80), some note value (note), silent velocity (0x00):
    delay(100);
    Midi3byte(0x80, note, 0x00);
    delay(100);
  }
} //Exit speelLadder -----------------------------------------------------------




void sendEvent (unsigned int hexBytes[]){ //Plays an event message to MIDI *****
  unsigned int counter = 0;      //Used to select the bytes from the string MIDI
  //Serial.print(sizeof(hexBytes[counter]); 
  while (hexBytes[counter] > 0){     //As long as I find correct values for MIDI
    //Serial.print(hexBytes[counter],HEX); 
    //Serial.print(", "); 
    Serial.write(hexBytes[counter]);           //Transmit the selected byte MIDI
    counter ++;               //Finally jump to the next byte in this event MIDI
  } //while (hexBytes[counter] > 0)     meaning no more bytes to be sent by MIDI
} //Exit sendEvent -------------------------------------------------------------





void Midi9byte (int cmd, int pitch, int velocity, int byte4, int byte5, int byte6, int byte7, int byte8, int byte9) { //Plays a note MIDI ********
  Serial.write(cmd);        //Does not check to see that cmd is greater than 127
  Serial.write(pitch);                   //Or that data values are less than 127
  Serial.write(velocity);                            //Just push the serial data
  Serial.write(byte4);                               //Just push the serial data
  Serial.write(byte5);                               //Just push the serial data
  Serial.write(byte6);                               //Just push the serial data
  Serial.write(byte7);                               //Just push the serial data
  Serial.write(byte8);                               //Just push the serial data
  Serial.write(byte9);                               //Just push the serial data
} //Exit Midi7byte -------------------------------------------------------------




void Midi7byte (int cmd, int pitch, int velocity, int byte4, int byte5, int byte6, int byte7) { //Plays a note MIDI ********
  Serial.write(cmd);        //Does not check to see that cmd is greater than 127
  Serial.write(pitch);                   //Or that data values are less than 127
  Serial.write(velocity);                            //Just push the serial data
  Serial.write(byte4);                               //Just push the serial data
  Serial.write(byte5);                               //Just push the serial data
  Serial.write(byte6);                               //Just push the serial data
  Serial.write(byte7);                               //Just push the serial data
} //Exit Midi7byte -------------------------------------------------------------




void Midi3byte (int cmd, int pitch, int velocity) { //Plays a note MIDI ********
  Serial.write(cmd);        //Does not check to see that cmd is greater than 127
  Serial.write(pitch);                   //Or that data values are less than 127
  Serial.write(velocity);                            //Just push the serial data
} //Exit Midi3byte -------------------------------------------------------------




void Midi2byte (int cmd, int pitch){ //Transmit 1 command & 1 data byte MIDI ***
  Serial.write(cmd);        //Does not check to see that cmd is greater than 127
  Serial.write(pitch);                   //Or that data values are less than 127
} //Exit Midi2byte -------------------------------------------------------------




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




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



////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO                                          //
// Board -Atmel- PIN - IDE - Function          - Connection               ALT //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  -                          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              SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - DIO Switch 1             SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - DIO key 5                PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - DIO key 4                PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       - DIO key 2                PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  - DIO key 1                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 + MIDI    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       - DIO key 6         yellow ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       - DIO key 5         purple ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      - ADC Potmeter 50k   brown 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