Need some help with a MIDI controller and Motorized Fader

Hi there,

I'm trying to hook up a motorized fader to my arduino, and I'm having a hard time getting the code together.

What I want is for the motorized fader to lock into a position specified by a fader on my other midi keyboard. That way I can control the position of the motorized fader by moving the keyboard's fader manually.

Here's the issue: I'm sending MIDI data from my keyboard into the Serial Monitor in one part of my code just fine, but when I use the same line in the next part of my code, the numbers in the Serial Monitor seem very off, and very jumpy. Note: The line that is commented out of the code is where I'm getting good readings. The next time I use Serial Monitor however, the numbers aren't quite right.

Here's the code (tried to reduce it as much as I could)

#include <AltSoftSerial.h>
#include <CapacitiveSensor.h>
AltSoftSerial midiSerial;

#define RXPIN 8
#define TXPIN 1
const int dir1PinA = 5;
const int dir2PinA = 6;
int faderValue = analogRead(A0);
int velocityByte;

CapacitiveSensor cs_7_2 = CapacitiveSensor(7, 2);

void setup()
{
  cs_7_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
  Serial.begin(250000);
  midiSerial.begin(31250);
  pinMode(dir1PinA, OUTPUT);
  pinMode(dir2PinA, OUTPUT);
  analogRead(faderValue);
  analogWrite(3, 255);
}

void loop()
{
  int touchSensorLow = digitalRead(7);
  int touchSensorHigh = digitalRead(2);
  
  {
    long start = millis();
    long total =  cs_7_2.capacitiveSensor(30);

    if (total <= 1) {
      motorControl();
    }

    else {
      stopMotor();
    }
  }
  
while ( midiSerial.available() > 2 )
  {
    int commandByte = midiSerial.read();
    int noteByte = midiSerial.read();
    velocityByte = midiSerial.read();
    midiSerial.write( commandByte);
    midiSerial.write( noteByte);
    midiSerial.write( velocityByte);
   //Serial.println(velocityByte);
  }
}

void motorControl() {
  int sensorValue = analogRead(A0);
  int number=500;
  
  if (sensorValue <= number-5) {
    moveMotorUp();
  }
  if (sensorValue >= number+5) {
    moveMotorDown();
  }
   if (sensorValue == number) {
    stopMotor();
  }
  Serial.println(velocityByte);
}

void moveMotorUp() {
  digitalWrite(dir1PinA, LOW);
  digitalWrite(dir2PinA, HIGH);
  delay(1);

  digitalWrite(dir1PinA, LOW);
  digitalWrite(dir2PinA, LOW);
  delay(1);
}

void moveMotorDown() {
  digitalWrite(dir1PinA, HIGH);
  digitalWrite(dir2PinA, LOW);
  delay(1);

  digitalWrite(dir1PinA, LOW);
  digitalWrite(dir2PinA, LOW);
  delay(1);
}

void stopMotor() {
  digitalWrite(dir1PinA, LOW);
  digitalWrite(dir2PinA, LOW);
}

It has something to do with using "if" to control the position of the motor. When I take that out and simply send the code to the "motorControl" function, it works fine. Unfortunately, in that case I no longer have control over the DC motor. Perhaps a new method is necessary

I found that I just had my capacitive touch set too low. I moved it up to 18 and it works now.

here's the fix:

if (total <= 18) {

At this point though, the fader is still a bit unresponsive compared to my controller. If anyone can lend some advice on that that would be awesome.

Thanks