#define note1

#define noteOn1 144 //note-on 144-159#define noteOn2 144
#define noteOn2 145
//#define noteOn3 146
//#define noteOn4 147
//#define noteOn5 148

#define pitchBendCommand1 224 //224-239 are pitch bend outputs
#define pitchBendCommand2 225
//#define pitchBendCommand3 226
//#define pitchBendCommand4 227
//#define pitchBendCommand5 228

#define sensor1 60 // This is still 'middle C' in the MIDI standard but we use it to encode 'sensor number'
#define sensor2 69
//#define sensor3 60
//#define sensor4 60
//#define sensor5 60

#define middleVolume 64
#define offVolume 0

//FSRS:
int analogPin1 = 1; // Digital input pin for a switch
int analogPin2 = 2;
//int analogPin3 = 0;
//int analogPin4 = 0;
//int analogPin5 = 0;

int ledPin13 = 13; // Digital output pin for on-board LED
int ledPin12 = 12;
//int ledPin11= 0;
//int ledPin10 = 0;
//int ledPin9 = 0;

int analogValue1 = 0; // value from the analog input, Analog to Digital Converter (ADC)
int analogValue2 = 0;
//int analogValue3 = 0;
//int analogValue4 = 0;
//int analogValue5 = 0;

int analogValMSB1 = 0; //poner aca analogValMSB0,1,2????
int analogValMSB2 = 0;
//int analogValMSB3 = 0;
//int analogValMSB4 = 0;
//int analogValMSB5 = 0;

int analogValLSB1 = 0;
int analogValLSB2 = 0;
//int analogValLSB3 = 0;
//int analogValLSB4 = 0;
//int analogValLSB5 = 0;

int scaledValue1 = 0; // scaled value for MIDI transmission
int scaledValue2 = 0;
//int scaledValue3 = 0;
//int scaledValue4 = 0;
//int scaledValue5 = 0;

void setup() {
// Here we set MIDI baud rate,
Serial.begin(31250); // Note that midi is just regular serial with a specified
blink(2); // protocol (transfer speed and message order)
}

/*For simple Midi, there are three BYTE values sent in a row, makind up a MIDI MESSAGE:
BYTE1 - a STATUS or COMMAND header, explaining what kind of data to expect next
BYTE2 - the data we are expecting, based on BYTE1
BYTE3 - (optionally) more data we're expecting, based on BYTE1
i.e.:
[BYTE1, BYTE2, BYTE3] --> [COMMAND, DATA, DATA]

*/

void loop() {

// This code really only with xBendin - as this gives the full "14 bit resolution" (actually 10 bit in this case)

/*
The LSB and MSB for Pitch Bend are 7 bit numbers in the MIDI spec, i.e.: 0-127
We really want to take the bottom 7 bits of the LSB
And shift the MSB of the analog result down by 7

10 bit ADC result: 000000M1 1111111L
Pitch Bend LSB required: XXXXXXXXX111111L (this will be between 0-127)
So we AND with 0000000001111111 to get the LSB

10 bit ADC result: 000000M1 1111111L
Pirch Bend MSB required: 0000000000000M11 (this will be between 0-127)
So we shift down by 7 to get the MSB
*/

digitalWrite(ledPin13, HIGH); //turn light on
digitalWrite(ledPin12, HIGH);
//digitalWrite(ledPin11, HIGH);
//digitalWrite(ledPin10, HIGH);
//digitalWrite(ledPin9, HIGH);

analogValue1 = analogRead(analogPin1); //this is a 16bit number with 10bits of useful information in it
analogValue2 = analogRead(analogPin2); //i.e.: 000000XX XXXXXXXX - where the 'X's are our ADC result bits
//analogValue3 = analogRead(analogPin3);
//analogValue4 = analogRead(analogPin4);
// analogValue5 = analogRead(analogPin5);

analogValLSB1 = analogValue1 & 127; //Keep lower 7 bits bits, 0000000011111111 = 127
analogValLSB2 = analogValue2 & 127;
//analogValLSB3 = analogValue3 & 127;
//analogValLSB4 = analogValue4 & 127;
//analogValLSB5 = analogValue5 & 127;


analogValMSB1 = (analogValue1 >> 7) ; //Need to shift the MSB into to the lower 7 bits
analogValMSB2 = (analogValue2 >> 7) ;
//analogValMSB3 = (analogValue3 >> 7) ;
//analogValMSB4 = (analogValue4 >> 7) ;
//analogValMSB5 = (analogValue5 >> 7) ;

note(pitchBendCommand1, analogValLSB1, analogValMSB1); //224 LSB MSB = pitch bend message, LSB, MSB
//delay(20); //give poor MIDI a chance!
note1(pitchBendCommand2, analogValLSB2, analogValMSB2);
delay(20);
//note(pitchBendCommand3, analogValLSB3, analogValMSB3);
//delay(20);
//note(pitchBendCommand4, analogValLSB4, analogValMSB4);
//delay(20);
//note(pitchBendCommand5, analogValLSB5, analogValMSB5);
//delay(20);



digitalWrite(ledPin13, LOW); //turn off light
digitalWrite(ledPin12, LOW);
//digitalWrite(ledPin11, LOW);
//digitalWrite(ledPin10, LOW);
//digitalWrite(ledPin9, LOW);
}

// This is a function we use to create our MIDI Message
// It packages up one MIDI message based on what we pass to it in the loop()
// part of the program.

void note(char cmd, char data1, char data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}

// Blinks an LED 'X' times
void blink(int howManyTimes) {
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(ledPin13, HIGH);
digitalWrite(ledPin12, HIGH);
//digitalWrite(ledPin11, HIGH);
// digitalWrite(ledPin10, HIGH);
// digitalWrite(ledPin9, HIGH);

delay(100);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin12, LOW);
// digitalWrite(ledPin11, LOW);
//digitalWrite(ledPin10, LOW);
// digitalWrite(ledPin9, LOW);

delay(100);
}
}