Using a potentiometer without constant reading... maybe some padding

Hi. I am using a bunch of buttons that will work fine over wifi using the code below on a esp32. Actually I deleted the button code to make this easier. I am sure I am just not thinking of something simple. Buttons, like the potentiometers, are asked to constantly listen for changes. But buttons aren't constantly in a state of slight deviation every listen... so they send tons of messages and slow everything down... the other buttons and my Ableton Live set. Is there any way to somehow write into the code "if variation in value is within +/-3~ values, don't send a message?" I am using these to control numbers 1-127 of their controls (knobs in guitar rig, levers in LIVE, etc.). There's got to be a better way to code these that make the pots calm TF down. Lol.

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>

#define SerialMon Serial
#include <AppleMIDI_Debug.h>
#include <AppleMIDI.h>

char ssid[] = "ATT4K"; //  your network SSID (name)
char pass[] = "passpasspass";    // your network password (use for WPA, or use as key for WEP)

// set pin numbers
const int pot0_Pin = 34;       // the number on arduino of potentiomter 0
const int pot1_Pin = 39;       // the number on arduino of potentiomter 1

const int DEBOUNCE_DELAY = 50;   // the debounce time; increase if the output flickers

// -----------------------------------------------------------------------------
// potentiometer 0 values Control
// -----------------------------------------------------------------------------

//int pot0Value;
//int lastPot0value;
//int outputPot0Value = 0;

// -----------------------------------------------------------------------------
// pot 1 control
// -----------------------------------------------------------------------------

int pot1Value;
int lastPot1value;
int outputPot1Value = 0;

// -----------------------------------------------------------------------------
// more variables
// -----------------------------------------------------------------------------
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long t0 = millis();
int8_t isConnected = 0;


APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE();

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------


void setup() {
  Serial.begin(115200);  

  
  DBG_SETUP(115200);
  DBG("Booting");
  
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    DBG("Establishing connection to WiFi..");
  }
  DBG("Connected to network");

  DBG(F("OK, now make sure you an rtpMIDI session that is Enabled"));
  DBG(F("Add device named Arduino with Host"), WiFi.localIP(), "Port", AppleMIDI.getPort(), "(Name", AppleMIDI.getName(), ")");
  DBG(F("Select and then press the Connect button"));
  DBG(F("Then open a MIDI listener and monitor incoming notes"));
  DBG(F("Listen to incoming MIDI commands"));

  MIDI.begin();

  AppleMIDI.setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char* name) {
    isConnected++;
    DBG(F("Connected to session"), ssrc, name);
  });
  AppleMIDI.setHandleDisconnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) {
    isConnected--;
    DBG(F("Disconnected"), ssrc);
  });
}

void loop() {

  // Listen to incoming notes
  MIDI.read();

// -----------------------------------------------------------------------------
// Pot 0 Analog Value Control
// -----------------------------------------------------------------------------   
    if (pot0Value != outputPot0Value) {
      lastDebounceTime = millis();
      // save the the last flickerable state
      outputPot0Value = pot0Value;
    }
    
    lastPot0value = pot0Value;
    pot0Value = analogRead(pot0_Pin);
    Serial.println(pot0Value);
    outputPot0Value = map(pot0Value, 10, 3950, 0, 127);
  
    if(lastPot0value == pot0Value) {
      MIDI.sendControlChange(110, outputPot0Value, 13);
      outputPot0Value = pot0Value;
      // wait 2 milliseconds before the next loop for the analog-to-digital
      // converter to settle after the last reading:
      delay(4); 
    }
    
//// -----------------------------------------------------------------------------
//// Pot 1 Analog Value Control
//// -----------------------------------------------------------------------------
    if (pot1Value !=  outputPot1Value) {
      lastDebounceTime = millis();
      // save the the last flickerable state
      outputPot1Value = pot1Value;
    }
    
    lastPot1value = pot1Value;
    pot1Value = analogRead(pot1_Pin);
    Serial.println(pot1Value);
    ////try 80 then 60 then 50!!!
    outputPot1Value = map(pot1Value, 10, 4000, 0, 127);

    if(lastPot1value == pot1Value) {
      MIDI.sendControlChange(111, outputPot1Value, 13);
      outputPot1Value = pot1Value;
      // wait 2 milliseconds before the next loop for the analog-to-digital
      // converter to settle after the last reading:
      delay(4);
    }

}
if ((value > lastpotvalue + tolerance) || (value < lastpotvalue - tolerance))
  // value has change by at least tolerance 

Or if you like

if ( abs(value - lastpotvalue) >= tolerance) )

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