On a different note...
...I've been reading through the documentation and source code for the libraries you referenced in your code, which I think are these ones:
These already implement the MIDI message parsing using callbacks, e.g.:
That example stresses the importance of swift non-blocking code. This means that updateFader() should also be non-blocking. You have the option to write your own custom state machine parser or use the libraries' parsers.
Here is how your code could be re-written to use the MIDI library's parser with non-blocking callbacks and non-blocking updateFader(). You may want to double-check which pins you are using for midiSerial because pins 0 and 1 are used for the default Serial monitor or you could use MIDI.sendNoteOn() and MIDI.sendNoteOff().
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
unsigned int faderMax = 0;
unsigned int faderMin = 0;
unsigned int faderTargetPosition = 0;
void doNoteOn(byte channel, byte note, byte velocity)
{
midiSerial.write(channel);
midiSerial.write(note);
midiSerial.write(velocity);
faderTargetPosition = velocity * INCOMING_VELOCITY_SCALE;
// Clip if out of bounds.
if (faderTargetPosition > faderMax)
{
faderTargetPosition = faderMax;
}
else if (faderTargetPosition < faderMin)
{
faderTargetPosition = faderMin;
}
}
void doNoteOff(byte channel, byte note, byte velocity)
{
midiSerial.write(channel);
midiSerial.write(note);
midiSerial.write(velocity);
}
void setup()
{
MIDI.setHandleNoteOn(doNoteOn);
MIDI.setHandleNoteOff(doNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
. . .
}
void loop()
{
// Parse the MIDI messages and call the callbacks.
MIDI.read();
if ((millis() - lastDebounceTime) > debounceDelay)
{
int totalCp = touch.capacitiveSensor(30);
int faderPos = analogRead(FADER_PIN);
int faderHiResMIDI = faderPos * 16.0146627566 - 8191.5;
if (totalCp <= minimumCp)
{
// Not touching fader.
//if (incomingCommand == ch1Vol)
//{
updateFader(faderTargetPosition);
//}
}
else
{
if (faderPos == lastfaderValue)
{
// Touching fader.
MIDI.sendNoteOn(Db_1, fullVel, ch_1);
digitalWrite(MOTOR_DOWN_PIN, LOW);
digitalWrite(MOTOR_UP_PIN, LOW);
}
else if ((faderPos > lastfaderValue + 1) or (faderPos < lastfaderValue - 1))
{
// Moving fader.
MIDI.sendPitchBend(faderHiResMIDI, ch_1);
digitalWrite(MOTOR_DOWN_PIN, LOW);
digitalWrite(MOTOR_UP_PIN, LOW);
}
}
lastfaderValue = faderPos;
lastDebounceTime += debounceDelay; // Constant interval.
}
}
void updateFader(const unsigned int& faderTargetPosition)
{
const unsigned int currentPosition = analogRead(FADER_PIN);
if (faderTargetPosition < currentPosition - tolerance) // Below the tolerance band.
{
digitalWrite(MOTOR_UP_PIN, LOW); // Switch the up control off before switching the down control on.
digitalWrite(MOTOR_DOWN_PIN, HIGH);
}
else if (faderTargetPosition > currentPosition + tolerance) // Above the tolerance band.
{
digitalWrite(MOTOR_DOWN_PIN, LOW); // Switch the down control off before switching the up control on.
digitalWrite(MOTOR_UP_PIN, HIGH);
}
else // Within the tolerance band.
{
digitalWrite(MOTOR_DOWN_PIN, LOW);
digitalWrite(MOTOR_UP_PIN, LOW);
}
}