 /**
-   <h1>Pianoduino</h1>
-   61 key MIDI Keyboard with Arduino Uno Code, 24 shiftout outputs, 24 shiftin inputs
-   1 resistive touchscreen and 16 analog I/O.
-   @author Malcolm Davis
-   @version 0.1
-   @since 2017-02-03
-*/
-
-//Arduino Pins
+ * <h1>Pianoduino</h1>
+ * 61 key MIDI Keyboard with Arduino Uno Code, 24 shiftout outputs, 24 shiftin inputs
+ * 1 resistive touchscreen and 16 analog I/O.
+ * @author Malcolm Davis
+ * @version 0.1
+ * @since 2017-02-03
+ */
+
+//*********************Arduino Pins*************************//
 static const byte muxAPin = 2;
 static const byte muxBPin = 3;
 static const byte muxCPin = 4;

}

/**
    Method to send a note off message.
    @param pChannel the channel to send the noteoff.
    @param pPitch the note to turn off.
 */
void noteOff(byte pChannel, byte pPitch){
  pChannel += noteOFF-1;

static const byte outClkPin = 12;
static const byte latchPin = 10;
//If a Analog mux is added, just connect the control pins in parallel and add the En pin to the list.
static const byte muxEn [] = {
  5};
//**********************************************************//

//**************************Constants*********************//
byte mask[] = { 
  B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };//To read the columns values
static const byte  keyMidiCons[144] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  B00101000, B00101000, B00101001, B00101001, B00101010, B00101010, B00101011, B00101011, B00101100, B00101100, B00100111, B00100111,
  B00101110, B00101110, B00101111, B00101111, B00110000, B00110000, B00101011, B00101011, B00101100, B00101100, B00101101, B00101101,
  B00111010, B00111010, B00111011, B00111011, 0, 0, B00110111, B00110111, B00111000, B00111000, B00111001, B00111001,
  B00110100, B00110100, B00110101, B00110101, B00110110, B00110110, B00110001, B00110001, B00110010, B00110010, B00110011, B00110011,
  B01010010, B01010010, B01010011, B01010011, B01010100, B01010100, B01001111, B01001111, B01010000, B01010000, B01010001, B01010001,
  B01001100, B01001100, B01001101, B01001101, B01001110, B01001110, B01001001, B01001001, B01001010, B01001010, B01001011, B01001011,
  B01000110, B01000110, B01000111, B01000111, B01001000, B01001000, B01000011, B01000011, B01000100, B01000100, B01000101, B01000101,
  B01000000, B01000000, B01000001, B01000001, B01000010, B01000010, B00111101, B00111101, B00111110, B00111110, B00111111, B00111111,
  0, 0, 0, 0, B00111100, B00111100, 0, 0, 0, 0, 0, 0
};
static const byte drumNotes []= {
  B00100110
}; //Drums note, snaredrum,...
static const byte blank = B00000000;//To send blank to the register.
static const byte noteON = B10010000;
static const byte noteOFF = B10000000;
static const byte control = B10110000;
static const byte pitchBend = B11100000;
static const byte  padMidi = B00000000; //Pad first note.
static const byte  buttonMidi = B00010000; //Controll general first code.
static const byte analogMidi =B00110000; //Controll general first code.
// MIDI codes taken from http://nickfever.com/music/midi-cc-list
static const byte MIDI_CC_MODULATION = B00000001;
static const byte MIDI_CC_VOLUME = B00000111;
static const byte MIDI_CC_BALANCE = B00001000;
static const byte MIDI_CC_SUSTAIN = B00101000;
static const byte MIDI_CC_NOTES_OFF = B01111011;



//Shift in movements(3 shift registers and 8 pins per shift register)
//The sum of all the rows have to be smaller or equal to the shift in registers.
static const byte shiftIns=24; 
//*******************Configuration constants**********************//
//Don't change this values unless you change the hardware.
static const byte keyColumns = 12;
static const byte keyRows = 12; //
static const byte buttonColumns = 3;
static const byte buttonRows = 4; 
static const byte padColumns = 4;
static const byte padRows = 4;
//This values can be changed.
static const short sensitivity = 1000;
static const byte keyboardChannel = 1;
static const byte buttonPadChannel = 2;
static const byte controlChannel = 3;
static const byte drumsChannel = 4;
static const byte drumsThreshold [] = { 
40 
};//Drums threshold, snaredrum,...
static const byte analogThreshold = 1;
//#define debug //Uncomment to debug
//***************************************************************//

//*******************************Variables**********************//
static bool sValue[] = {
  0, 0, 0}; // S0, S1, S2 values
long noteTime [sizeof(keyMidiCons) / sizeof(byte) / 2]; //Time that the note have been pressed.
bool keyState[sizeof(keyMidiCons) / sizeof(byte)]; //State of the key.
bool buttonState[buttonColumns*buttonRows]; //State of the buttons.
bool padState[padRows*padColumns]; //State of the pad buttons.
bool rowsRead [keyRows+buttonRows]; // Rows shift in values.
byte trans = 0; //Level to transpose the octaves.
static byte  keyMidi[sizeof(keyMidiCons) / sizeof(byte)]; 
byte analogValue[(sizeof(muxEn) / sizeof(byte)) * 8]; 
//**************************************************************//


/**
 * Method to setup the pins and stuffs of the pianoduino.
 * @param none
 * @return void
 */
void setup() {
  byte i;
  Serial.begin(115200);
#ifdef debug 
  Serial.println("Setup Start");
#endif
  pinMode(muxAPin, OUTPUT);
  pinMode(muxBPin, OUTPUT);
  pinMode(muxCPin, OUTPUT);
