Issues with potentiometers effecting each other

Hi all,

I've been searching and banging my head against this issue with no progress, so I'm finally just going to reach out and ask the question to people who probably know more than me.

I've built this, and have struggling with it. It seems no matter what I do the potentiometers all, at the very least, effect each other. For example, if I move one pot the others move somewhat. In the worst case, they all mirror each other and move the same amount. In the searching I've done, I've seen people suggest doing a second analogRead and throwing out the first, but this has not helped (see the code below). I've double checked the pots and all the electrical connections. I've used multiple proMicros and I'm getting the same result. Any ideas?

#include <Joystick.h>

//Joystick Setup
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK,
  1, 0,                  // Button Count, Hat Switch Count
  true, true, true,     // X, Y, & Z Axis
  false, false, false,     // Rx, Ry, or No Rz
  false, false,          // Rudder but no throttle
  false, false, false);  // No accelerator, brake, or steering;
  
int leftPedal = 0;
int rightPedal = 0;
int rudderAxis_ = 0;  

int lastButtonState[1];
int btnCount = 1;
int buttonPin[1] = {2};

void setup(){
  // Initialize Button Pins
  for (int i = 0; i < btnCount; i++)
  {
    pinMode(buttonPin[i], INPUT_PULLUP);
  }

  Joystick.begin(); //Starts joystick
}
  
void loop(){
  checkButtons();
  
  leftPedal = analogRead(A3);
  leftPedal = analogRead(A3);
  leftPedal = map(leftPedal,0,1023,0,255);
  Joystick.setXAxis(leftPedal);
  
  rightPedal = analogRead(A2);
  rightPedal = analogRead(A2);
  rightPedal = map(rightPedal,0,1023,0,255);
  Joystick.setYAxis(rightPedal);

  rudderAxis_ = analogRead(A1);
  rudderAxis_ = analogRead(A1);
  rudderAxis_ = map(rudderAxis_,0,1023,0,255);
  Joystick.setZAxis(rudderAxis_);

  delay(5);
}

void checkButtons(void) {
    for (int i=0; i<btnCount; i++){
      int currentButtonState = !digitalRead(buttonPin[i]);
      // Change Joystick buttons only if the state has stateChanged
      if (currentButtonState != lastButtonState[i]){
        Joystick.setButton(i, currentButtonState);
        lastButtonState[i] = currentButtonState;
      }
    }
}

PS. I had to add a button for SDL in Linux to see it as a joystick, so that's why that code is there.

What is the value of the pots?
Check the actual voltages in the pins with a multimeter.

And better put here the schematic instead of making the people having to search for it in your pages.

I've tried it with both 10K ohm and 20K ohm (just to rule out that the value wasn't the problem).

The voltage at the pot pins is 4.84, the voltage at the board matches.

Here is the wiring diagram.

I think that the Arduino has only one ADC, and it switchs from one pin to another with a multiplexer. So, could be that if you read the pins too fast the ADC has no time to recharge the capacitor or whatever needed for the new sample.

Use the 10K pot, or better 5K, to have less impedance and charge the adc faster. And add some delay between the analogRead in one pin to the next pin, maybe 1 or 5 millis, to test. I don't know which speed is acceptable for your application.

You should also check if the values that you see with the multimeter in the MCU pins are consistent with the readings in the console. To find out if the problem is outside or inside.
And that the 5V at VCC are stable.

Disregard my last. Experiencing same problem.

It is possible that your wiring is incorrect and that the pots are overloading the power supply.

Please post a link to the actual pots you are using, and YOUR hand-drawn, pencil and paper diagram showing exactly how they are wired. Also post a photo of the setup.

Think I got it this time. Note the use of internal pullup resistors on the pots in void setup()
Tested with an analog sound sensor, an LDR and a potentiometer (what I had on hand) with my trusty Uno R3 (with the slightly loose analog pin headers - it's ancient now).
Verifies with Micro board selected and seems to work ok - hopefully responsive enough.

Hope this helps you out. Recommend you calibrate your sensors to determine their min/max and import those values to your final sketch where applicable.

#include <Joystick.h>

//Joystick Setup
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK,
                   1, 0,                  // Button Count, Hat Switch Count
                   true, true, true,     // X, Y, & Z Axis
                   false, false, false,     // Rx, Ry, or No Rz
                   false, false,          // Rudder but no throttle
                   false, false, false);  // No accelerator, brake, or steering;

int lastButtonState[1];
int btnCount = 1;
int buttonPin[1] = {2};

const int Xpin = A3;
const int Ypin = A2;
const int Zpin = A1;

unsigned long leftPedal, rightPedal, rudderAxis, lastLeftPedal, lastRightPedal,
         lastRudderAxis, outboundLeft, outboundRight, outboundRudder;

unsigned long checkLeftPotTime, checkRightPotTime, checkRudderPotTime;

const int bufferSizeX = 10;
int bufferX[bufferSizeX];
const int bufferSizeY = 10;
int bufferY[bufferSizeY];
const int bufferSizeZ = 10;
int bufferZ[bufferSizeZ];
int indexX, indexY, indexZ;

byte pedalState;


void setup() {
  Serial.begin(115200);
  // Initialize Button Pins
  for (int i = 0; i < btnCount; i++){
    pinMode(buttonPin[i], INPUT_PULLUP);
  }
  pinMode(Xpin, INPUT_PULLUP);
  pinMode(Ypin, INPUT_PULLUP);
  pinMode(Zpin, INPUT_PULLUP);
  Joystick.begin(); //Starts joystick
  resetPedals();
}

void loop() {
  checkButtons();
  switch (pedalState) {
    case 1:
      checkPedalChangeX();
      break;
    case 2:
      checkPedalChangeY();
      break;
    case 3:
      checkPedalChangeZ();
      break;
    default:
      break;
  }
}

void checkPedalChangeX() {
  long totalX = 0;
  if (millis() - checkLeftPotTime >= 5ul) {
    if (leftPedal != lastLeftPedal) {
      for (int i = 0; i < 10; i++) {
        leftPedal = analogRead(Xpin);
        bufferX[indexX] = leftPedal;
        indexX++;
        delay(2);
        if (indexX > bufferSizeX) indexX = 0;
      }
      for (int i = 0; i < bufferSizeX; i++) {
        totalX += bufferX[i];
      }
      lastLeftPedal = totalX / bufferSizeX;
      lastLeftPedal = map(lastLeftPedal, 0, 1023, 0, 255);
    }
    checkLeftPotTime = millis();
  }
  setPedalX(lastLeftPedal);
}
void checkPedalChangeY() {
  long totalY = 0;
  if (millis() - checkRightPotTime >= 10ul) {
    if (rightPedal != lastRightPedal) {
      for (int j = 0; j < 10; j++) {
        rightPedal = analogRead(Ypin);
        bufferY[indexY] = rightPedal;
        indexY++;
        delay(2);
        if (indexY > bufferSizeY) indexY = 0;
      }
      for (int j = 0; j < bufferSizeY; j++) {
        totalY += bufferY[j];
      }
      lastRightPedal = totalY / bufferSizeY;
      lastRightPedal = map(lastRightPedal, 0, 1023, 0, 255);
    }
    checkRightPotTime = millis();
  }
  setPedalY(lastRightPedal);
}

void checkPedalChangeZ() {
  long totalZ = 0;
  if (millis() - checkRudderPotTime >= 20ul) {
    if (rudderAxis != lastRudderAxis) {
      for (int k = 0; k < 10; k++) {
        rudderAxis = analogRead(Zpin);
        bufferZ[indexZ] = rudderAxis;
        indexZ++;
        delay(2);
        if (indexZ > bufferSizeZ) indexZ = 0;
      }
      for (int k = 0; k < bufferSizeZ; k++) {
        totalZ += bufferZ[k];
      }
      lastRudderAxis = totalZ / bufferSizeZ;
      lastRudderAxis = map(lastRudderAxis, 0, 1023, 0, 255);

    }
    checkRudderPotTime = millis();
  }
  setPedalZ(lastRudderAxis);
}
void setPedalX(long lastLeftPedal) {
  outboundLeft = lastLeftPedal;
  Serial.print(F("X Axis: "));
  Serial.print(outboundLeft);
  pedalState = 2;
}
void setPedalY(long lastRightPedal) {
  outboundRight = lastRightPedal;
  Serial.print(F("  Y Axis: "));
  Serial.print(outboundRight);
  pedalState = 3;
}
void setPedalZ(long lastRudderAxis) {
  outboundRudder = lastRudderAxis;
  Serial.print(F("  Z Axis: "));
  Serial.println(outboundRudder);
  pedalState = 1;
}

void checkButtons(void) {
  for (int i = 0; i < btnCount; i++) {
    int currentButtonState = !digitalRead(buttonPin[i]);
    // Change Joystick buttons only if the state has stateChanged
    if (currentButtonState != lastButtonState[i]) {
      Joystick.setButton(i, currentButtonState);
      lastButtonState[i] = currentButtonState;
    }
  }
}

void resetPedals() {
  leftPedal = 0;
  rightPedal = 0;
  rudderAxis = 0;
  lastLeftPedal = 0;
  lastRightPedal = 0;
  lastRudderAxis = 0;
  outboundLeft = 0;
  outboundRight = 0;
  outboundRudder = 0;
  pedalState = 1;
  rudderAxis = analogRead(Zpin);
  rightPedal = analogRead(Ypin);
  leftPedal = analogRead(Xpin);
}

Sorry it took so long to get back to this. This seemed like the easiest solution, and it totally worked. Thanks!

1 Like