Control Surface library affecting Fader Behavior

This code works great, moving an adafruit motorized fader to a target point and stops when it gets there.

But when I try to use this code with the control surface library, and running control surface in setup and loop, the fader moves to a totally incorrect position and jitters until I stop it.

Is there a way for this code to play nicely with Control Surface?

(The following code is working code, so the Control Surface.begin command is commented out. Comment back in for the buggy behavior.)

#include <Control_Surface.h>

USBMIDI_Interface midi;

const int targetValue = 400;     // Set your target value here
const int motorControlPinA = 18;  // Motor control pin A (PWM capable)
const int motorControlPinB = 19;  // Motor control pin B (PWM capable)
const int potentiometerPin = 24;  // Potentiometer pin
const int buffer = 15;

void setup() {
  pinMode(motorControlPinA, OUTPUT);
  pinMode(motorControlPinB, OUTPUT);
  pinMode(potentiometerPin, INPUT);
  Serial.begin(9600);
  //Control_Surface.begin();
}

void loop() {
  int potValue = readPotentiometerValue();
  int difference = abs(potValue - targetValue);

  if (difference > buffer) {
    controlMotor(potValue);
  } else {
    stopMotor();
  }
  Control_Surface.loop();
}

int readPotentiometerValue() {
  return analogRead(potentiometerPin);
}

void controlMotor(int potValue) {
  if (potValue < targetValue) {
    analogWrite(motorControlPinA, 255);  // Adjust PWM value for speed control
    analogWrite(motorControlPinB, 0);
    delay(3);
  } else {
    analogWrite(motorControlPinA, 0);
    analogWrite(motorControlPinB, 255);
    delay(3);
  }
}

void stopMotor() {
  analogWrite(motorControlPinA, 0);
  analogWrite(motorControlPinB, 0);
}

Is this the full code? There seems to be just one Control Surface element, the MIDI interface.
Try using only midi.begin() and midi.update() instead of Control_Surface.begin()/loop().

Which board are you using? If your board doesn't have native USB support, initializing the MIDI interface will call Serial.begin(31250);.

Another thing that could be happening is that Control Surface configures the analog read resolution to a higher resolution than 10 bits (if you use analog input elements on boards that support it). You can turn off this behavior, see Control Surface: AH Namespace Reference

Note that the jittering might be caused only by the slight difference in timing from adding extra code: this may be unavoidable for a primitive bang-bang controller.

See also:

Thanks for your help Peter!

Using Teensy 4.1. The increased resolution on the analog pin read sounds like a great idea to look at. I’ll report back on that aspect.

I probably need to keep the control surface begin and loop because in my project I’m using a ton of control surface library. (Here I’ve just provided the code relevant to the bug/question, but back in the shop using lots of control surface library stuff!)

For this simple test I have the Adafruit motorized fader wired to the Teensy in a breadboard in accordance to the diagram provided on their site.