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);
}
}
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);
}
}
- 699
- 6
- 15
void calibrateFader()
{
const byte num_samplesNUM_SAMPLES = 10;
digitalWrite(motorUp, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorUp, LOW);
unsigned int faderTotalfaderMax = 0;
for AnalogueAverage(byte i =wiper, 0;NUM_SAMPLES);
i < num_samples; i++Serial.println(faderMax);
{
digitalWrite(motorDown, HIGH);
analogWrite(motorPWM, motorSpeed);
faderTotal += analogReaddelay(wiper300);
}digitalWrite(motorDown, LOW);
faderMin = faderTotal /AnalogueAverage(wiper, num_samples;NUM_SAMPLES);
Serial.println(faderMin);
}
unsigned int digitalWriteAnalogueAverage(motorDown, HIGH);
const byte pin, const analogWrite(motorPWM,byte motorSpeednum_samples);
{
delay(300);
const byte ROUND_OFF = digitalWrite(motorDown,num_samples LOW);/ 2;
faderTotalunsigned long total = 0;
for (byte i = 0; i < num_samples; i++)
{
faderTotaltotal += analogRead(wiperpin);
}
faderMaxreturn =(total faderTotal+ ROUND_OFF) / num_samples;
Serial.println(faderMax);
}
void calibrateFader()
{
const byte num_samples = 10;
digitalWrite(motorUp, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorUp, LOW);
unsigned int faderTotal = 0;
for (byte i = 0; i < num_samples; i++)
{
faderTotal += analogRead(wiper);
}
faderMin = faderTotal / num_samples;
Serial.println(faderMin);
digitalWrite(motorDown, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorDown, LOW);
faderTotal = 0;
for (byte i = 0; i < num_samples; i++)
{
faderTotal += analogRead(wiper);
}
faderMax = faderTotal / num_samples;
Serial.println(faderMax);
}
void calibrateFader()
{
const byte NUM_SAMPLES = 10;
digitalWrite(motorUp, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorUp, LOW);
faderMax = AnalogueAverage(wiper, NUM_SAMPLES);
Serial.println(faderMax);
digitalWrite(motorDown, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorDown, LOW);
faderMin = AnalogueAverage(wiper, NUM_SAMPLES);
Serial.println(faderMin);
}
unsigned int AnalogueAverage(const byte pin, const byte num_samples)
{
const byte ROUND_OFF = num_samples / 2;
unsigned long total = 0;
for (byte i = 0; i < num_samples; i++)
{
total += analogRead(pin);
}
return (total + ROUND_OFF) / num_samples;
}
Fourthly,faderMin
and faderMax
should be just that – the minimum and maximum extent of fader travel. But by adding/substracting tolerance
from min/max it is unnecessarily reducing the range. The tolerance is effectively being doubled by adjusting for it in bothsetup()
andadjustFader()
. The extent of fader travel can be obtained by taking an average of several values in calibrateFader()
. Then the tolerance can be adjusted for only once in adjustFader()
as described above.
void calibrateFader()
{
const byte num_samples = 10;
digitalWrite(motorUp, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorUp, LOW);
unsigned int faderTotal = 0;
for (byte i = 0; i < num_samples; i++)
{
faderTotal += analogRead(wiper);
}
faderMin = faderTotal / num_samples;
Serial.println(faderMin);
digitalWrite(motorDown, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorDown, LOW);
faderTotal = 0;
for (byte i = 0; i < num_samples; i++)
{
faderTotal += analogRead(wiper);
}
faderMax = faderTotal / num_samples;
Serial.println(faderMax);
}
Fourthly,faderMin
and faderMax
should be just that – the minimum and maximum extent of fader travel. But by adding/substracting tolerance
from min/max it is unnecessarily reducing the range. The tolerance is effectively being doubled by adjusting for it in bothsetup()
andadjustFader()
. The extent of fader travel can be obtained by taking an average of several values in calibrateFader()
. Then the tolerance can be adjusted for only once in adjustFader()
as described above.
void calibrateFader()
{
const byte num_samples = 10;
digitalWrite(motorUp, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorUp, LOW);
unsigned int faderTotal = 0;
for (byte i = 0; i < num_samples; i++)
{
faderTotal += analogRead(wiper);
}
faderMin = faderTotal / num_samples;
Serial.println(faderMin);
digitalWrite(motorDown, HIGH);
analogWrite(motorPWM, motorSpeed);
delay(300);
digitalWrite(motorDown, LOW);
faderTotal = 0;
for (byte i = 0; i < num_samples; i++)
{
faderTotal += analogRead(wiper);
}
faderMax = faderTotal / num_samples;
Serial.println(faderMax);
}