"MIDI over USB",is MIDI.h library? please help me check my code. thank you very much.
#include <Bounce.h>
#include <ResponsiveAnalogRead.h>
#include <Metro.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
Metro serialMetro = Metro(250);
Bounce button1 = Bounce(2, 5); // 5 = 5 ms debounce time
Bounce button2 = Bounce(3, 5); // which is appropriate for good
Bounce button3 = Bounce(4, 5); // quality mechanical pushbuttons
bool ispushed = false;
const int ButtonCC[] = {114,115,116,117,118,119}; //studio one using controller response on button
const int ButtonNote[] = {122,123,124,125,126,127}; // reaper using noteOn response on button
bool CCUPDATED = false;
const int Leds = 4;
const int ledPins[Leds] = {8,5,6,15};
const int A_PINS = 3;
const int ANALOG_PINS[A_PINS] = {A5,A6,A7};
int CCID[A_PINS] = {11, 3, 1};
float data[A_PINS] ={0};
float dataLag[A_PINS] = {0};
bool CCIDUPDATED = false;
// ititialize the ReponsiveAnalogRead objects
ResponsiveAnalogRead analog[]{
///////////////////////////////////////////////////////////////////////////
{ANALOG_PINS[0],true},
{ANALOG_PINS[1],true},
{ANALOG_PINS[2],true},
///////////////////////////////////////////////////////////////////////////
};
//////////////////////////// +Leds Controller ////////////////////////////////
void ledsOff(int endingIndex)
{
for (int i = 0; i < endingIndex; i++)
{
if (digitalRead(ledPins[i]) == HIGH){digitalWrite(ledPins[i], LOW);}
}
}
//-----------------------------------------------------------------------------
// leds controller
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
void ledsControl(int index, bool alloff, bool ishigh, bool isblink = false) //isblink = false is option parameters
{
if (alloff) {ledsOff(3);}
if (ishigh) {digitalWrite(ledPins[index], HIGH);}
if (!ishigh) {digitalWrite(ledPins[index], LOW);}
if (isblink)
{
previousMillis = millis();
if (currentMillis - previousMillis >=150)
{
digitalWrite(ledPins[index], LOW);
previousMillis = currentMillis;
}
}
}
////////////////////////////////////////////////////////////////////////////
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
for (int i = 0; i < Leds; i++)
{
pinMode(ledPins[i], OUTPUT);
}
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial.begin(9600);
while (!Serial) { }
}
///////////////////////////////////////////////////////////////////////////
void loop() {
establishContact();
if(CCIDUPDATED){ledsControl(3,false,true);}
if(!CCIDUPDATED){ledsControl(3,false,false);}
Potentiometer();
ButtonPushed();
}
/////////////////////////// +Potentiometer /////////////////////////////////////////////////
void Potentiometer(){
// update the ResponsiveAnalogRead object every loop
for (int i = 0;i < A_PINS;i++){
analog[i].update();
// if the repsonsive value has change, print out 'changed'
if(analog[i].hasChanged()) {
data[i] = analog[i].getValue()>>3;
if (data[i] != dataLag[i]){
dataLag[i] = data[i];
MIDI.sendControlChange(CCID[i], data[i], 1);
}
}
}
}
//////////////////////////// +Buttons ////////////////////////////////////////////////
void ButtonPushed()
{
button1.update();
button2.update();
button3.update();
// Note On messages when each button is pressed
if (button1.fallingEdge()){MIDIActions("RECORD");ispushed = true;}
if (button2.fallingEdge()){MIDIActions("PLAY");ispushed = true;}
if (button3.fallingEdge()){MIDIActions("STOP");ispushed = true;}
if (ispushed && serialMetro.check() == 1){sendNoteControlOff();} //if release the button, controller back to 0, and also will sendNoteOff
}
//-----------------------------------------------------------------------
void MIDIActions(String action)
{
if (action == "RECORD")
{
MIDI.sendControlChange(ButtonCC[0], 127, 1);
MIDI.sendNoteOff(ButtonNote[0],127,1);
ledsControl(0,true,false);
}
if (action == "PLAY")
{
MIDI.sendControlChange(ButtonCC[1],127,1);
MIDI.sendNoteOn(ButtonNote[1],127,1);
ledsControl(1,true,false);
}
if (action == "STOP")
{
MIDI.sendControlChange(ButtonCC[2],127,1);
MIDI.sendNoteOn(ButtonNote[2],127,1);
ledsControl(2,true,false);
}
}
//-------------------------------------------------
void sendNoteControlOff()
{
for (int i = 0; i < 3; i++)
{
MIDI.sendControlChange(ButtonCC[i], 0, 1);
MIDI.sendNoteOff(ButtonNote[i],127,1);
}
}
//////////////////////////////+Serial/////////////////////////////////////////////
void establishContact() {
if (!CCIDUPDATED && Serial.available() <= 0) {
Serial.println("Bens"); // send a capital A
delay(300);
}
}
//----------------------------------------------- using USB_DUAL_SERIAL usb type, and using SerialUSB1 comunicate with pc
String inputString = "";
void serialEvent() {
while (Serial.available())
{
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n' && inputString.indexOf(',') > 0)
{
CCUPDATED = false;
updateController(inputString,',');
}
}
}
//---------------------------------------------
int temp[3];
int fromIndex = 0;
int n = 0;
void updateController(String text, char splitSymbol)
{
for (int i = 0; i < strings.length() - 1; i++)
{
// split data based on point (.), Can also be replaced by comma (,)
if (strings[i] == splitSymbols)
{
temp[n] = strings.substring(startIndex,i);
startIndex = i + 1;
n++;
if (temp[n] != NULL && n < 3)
{
CCID[n] = temp[i].toInt();
CCIDUPDATED = true;
Serial.println("New CC Number: " +temp[n] + ',');
}
}
}
}