WEMOS Lolin32 phantom potentiometer input

Hey folks! I am working on a MIDI controller using BLE on a WEMOS Lolin32 and am running into a strange issue. Here's a snippet of the larger program that isolates the problem I'm having:

#include <ResponsiveAnalogRead.h>
#include <BLEMIDI_Transport.h>
#include <hardware/BLEMIDI_ESP32.h>
#include <MIDI.h>

BLEMIDI_CREATE_INSTANCE("MIDI_TEST_PROJ", MIDI)

const int POT_COUNT = 1;
const int POT_PIN[POT_COUNT] = {4};  

int potCurr[POT_COUNT] = { 0 };
int potPrev[POT_COUNT] = { 0 };
int potVar = 0;

int midiCurr[POT_COUNT] = { 0 };
int midiPrev[POT_COUNT] = { 0 };

const int varThreshold = 80;          // Threshold for the potentiometer signal variation

int reading = 0;
float snapMultiplier = 0.01;                      // (0.0 - 1.0) - Increase for faster, but less smooth reading
ResponsiveAnalogRead responsivePot[POT_COUNT] = {};  // Creates an array for the responsive pots that gets filled in the setup.

//*********************************************************************************************************************************************************************

void setup() {

  Serial.begin(115200);

  for (int i = 0; i < POT_COUNT; i++) {
    responsivePot[i] = ResponsiveAnalogRead(0, true, snapMultiplier);
    responsivePot[i].setAnalogResolution(4096);  
  }

  BLEMIDI.setHandleConnected(onConnected);
  BLEMIDI.setHandleDisconnected(onDisconnected);

  MIDI.begin();
}

//*********************************************************************************************************************************************************************

void loop() {
  potentiometers();
}

//*********************************************************************************************************************************************************************

void potentiometers() {
  for (int i = 0; i < POT_COUNT; i++){
    
    potCurr[i] = analogRead(POT_PIN[i]);

    midiCurr[i] = map(potCurr[i], 0, 4095, 0, 127);  // Maps the potentiometer read range to the range of a MIDI signal

    potVar = abs(potCurr[i] - potPrev[i]);

    //Serial.println(midiCurr[i]);

    if (potVar > varThreshold) {
        Serial.println(midiCurr[i]);
        //MIDI.sendControlChange(1 + i, midiCurr[i], 6);
        potPrev[i] = potCurr[i];
    }
  }
}

void onConnected() {
  digitalWrite(LED_BUILTIN, HIGH);
}
void onDisconnected() {
  digitalWrite(LED_BUILTIN, LOW);
}




In the potentiometers function, when running the "if" statement on line 60, it prints out continuously alternating numbers of what the potentiometer is reading and another value. So for example 60, 127, 60, 127, 60, 127, 60, 127 where 60 is the expected value, then I turn it and it'll go to 86, 127, 86, 127 etc. The second number is usually the max, but when it's closer to the low end of the range it's sometimes a random number instead.

When I print the same value outside of that conditional starting on line 60 (specifically at the commented out Serial.println on line 58) it works as expected and the values are correct and consistent.

When I comment out the MIDI.begin on line 38 it works inside the conditional, but that of course defeats the purpose since it no longer works as a MIDI device. Pretty sure that's the culprit but haven't been able to figure out how to fix it.

Anyone else run into a similar issue or have any troubleshooting advice? I've been stumped on this for several days now and am not sure what to try next.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.