COMUNICACIÓN MIDI
#include <MIDI.h>
const int sustain = 7;
int a = 0;
int b = 0;
const int analogInPin3 = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin3 = 11; // Analog output pin that the LED is attached to
const int analogInPin = A2; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
const int analogInPin2 = A1; // Analog input pin that the potentiometer is attached to
const int analogOutPin2 = 10; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0;
int sensorValue2 = 0; // value read from the pot
int outputValue2= 0;
int sensorValue3 = 0; // value read from the pot
int outputValue3= 0;
void setup() {
MIDI.begin();
Serial.begin(31250);
pinMode(sustain,INPUT);
}
void loop() {
////////////////////////////////////////////pitch bend//////////////
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue,0, 1023, -8190, 8190);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
delay(7);
//////////////////////////////////modulacion//////////////////////////////
sensorValue2 = analogRead(analogInPin2);
// map it to the range of the analog out:
outputValue2 = map(sensorValue2, 0, 1023, 0, 127);
// change the analog out value:
analogWrite(analogOutPin2, outputValue2);
delay(7);
////////////////////////////////////volumen///////////////////
sensorValue3 = analogRead(analogInPin3);
// map it to the range of the analog out:
outputValue3 = map(sensorValue3, 0, 1023, 0, 127);
// change the analog out value:
analogWrite(analogOutPin3, outputValue3);
delay(7);
////////////////////////////////////aquitermina//////////////////////////////
int b = digitalRead(sustain);
if(b==HIGH){ b = 127; }
controlChange(1, 7,outputValue3);
PitchWheelChange(1, outputValue);
controlChange(1, 1,outputValue2);
controlChange(1, 64,b);
delay(7);
}
// plays a MIDI note. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void noteOn(int cmd, int pitch, int velocity) {
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
void PitchWheelChange(int channel, int value)
{ /// value -8192,8191 (14 bits)
unsigned int change = 0x2000 + value;
unsigned char low = change & 0x7F ; //low 7 bits
unsigned char high = (change >> 7)& 0x7F; //high 7 bits
Serial.write(B11100000 + channel-1);
Serial.write(low);
Serial.write(high);
}
void controlChange(int channel, int control, int value)
{
Serial.write(0xB0 + channel-1);
Serial.write(control);
Serial.write(value);
}