Mapping 2 Joysticks using the ADS1115 ADC

Hello i have trouble mapping correctly the values from 2 joysticks.

I use the ADS1115 to read all the analog outputs from the joysticks but i cant get the value right.

I need to map the joysticks in a way to control servo motor angles.

The problem is that i cannot get a stable reading due to the 16 bit resolution and cannot find a way to work around it. I do not need the 16 bit resolution only the I2C interface of the ADS1115.

The range of the output varies between the joystick axis
for example i made a sketch to find the highs and lows of each axis and the results are
HIGH LOW
X1 23564 15
Y1 23564 1818
X2 23504 98
Y2 23510 19

When mapping the values and the joysticks are idle i get the following angles
X1 87
Y1 80-81 (it fluctuates constantly)
X2 86-87 (it fluctuates constantly)
Y2 93

Code :

#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads;

int adc0, adc1, adc2, adc3; // ADC Readings
int X1angle,Y1angle,X2angle,Y2angle; //Joystick Angle Variables

void setup()
{
  ads.begin();
}
void loop()
{
  adc0=ads.readADC_SingleEnded(0);
  adc1=ads.readADC_SingleEnded(1);
  adc2=ads.readADC_SingleEnded(2);
  adc3=ads.readADC_SingleEnded(3);

  X1angle=map(adc2,15,23564,0,180); 
  Y1angle=map(adc3,1818,23564,0,180);
  X2angle=map(adc0,98,23504,0,180); 
  Y2angle=map(adc1,19,23510,0,180); 
}

Edit : Forgot to mention that the code posted here is just for the adc in the project the whole sketch is large and none of the other bits of code relate to the adc.

ChristosS:
The problem is that i cannot get a stable reading...

When mapping the values and the joysticks are idle i get the following angles
X1 87
Y1 80-81 (it fluctuates constantly)
X2 86-87 (it fluctuates constantly)
Y2 93

One solution to eliminate that sort of flicker is to compare each new X or Y value to the previous one. If the absolute value of the difference is greater than a threshold, use or display the new value. Otherwise, do nothing.

The ADS1115 is an absolute (voltage) A/D, not a ratiometric A/D like use in most Arduinos.
And is therefore a poor choice for a (ratiometric) pot/joystick.
You never will get a stable reading with this combination.
Leo..

What if the ADS1115 is used in differential mode: one of the analog inputs is Vdd (same as powers the pot and ADS) and the other analog input is from the pot wiper.

I suppose measuring the pots as well as VCC with one channel (for compensation) could also work.
Seems like a poor solution though.

Not sure why an external A/D is used for a joystick.
And which Arduino is used.
Leo..

I used the ADS1115 because i only needed an ADC to read the joysticks that could communicate over 2 wires using I2C. Else i would just use the on board ADC.

My problem is not the output fluctuation but the calibration of the system.

With the different highs and lows i cannot properly calibrate the controller when the joysticks are idle.Using the map function i get different values for every axis.

I want to calibrate the system so when the joysticks are in the idle position the arduino reads 90 for all axis regardless.

ChristosS:
The problem is that i cannot get a stable reading...

<>

ChristosS:
My problem is not the output fluctuation but the calibration of the system.

:confused:

ChristosS:
I want to calibrate the system so when the joysticks are in the idle position the arduino reads 90 for all axis regardless.

If you are happy with the slope, then just change the intercept.

ChristosS:
I used the ADS1115 because i only needed an ADC to read the joysticks that could communicate over 2 wires using I2C. Else i would just use the on board ADC.

A joystick (two pots) would use the same number of wires. VCC, GND, IN1, IN2.

I think the solution is to add a generous amount of hysteresis to the returned A/D value before mapping it to 0-180.
Attached is an example I wrote for 0-1023 to 0-255.
Leo..

// converts the position of a 10k lin(B) pot to a byte (0-255)
// pot connected to A0, 5volt and ground

int rawValue; // raw reading
int oldValue; // for deadband
byte Byte; // final byte
byte oldByte; // for printing

void setup() {
  Serial.begin(9600);
}

void loop() {
  // rawValue = analogRead(A0); // dummy read, if needed
  rawValue = analogRead(A0); // read pot
  if (rawValue < (oldValue - 2) || rawValue > (oldValue + 2)) { // add some deadband
    oldValue = rawValue; // update value
    Byte = oldValue >> 2; // convert 10-bit to 8-bit
    if (oldByte != Byte) { // only print if value changes
      Serial.print("Byte is: ");
      Serial.println(Byte);
      oldByte = Byte; // update value
    }
  }
}

DaveEvans:
<>

:confused:

If you are happy with the slope, then just change the intercept.

I know i am sending mixed messages. I was not clear.

I want the ADC values when the joysticks are idle (home position) to map at 90 degrees.

That is my main problem the fluctuation i can deal with with the solution Wawa provided above. But i cant have a solid starting point when the axis homing position read between 80 and 93 degrees when mapped.

Thanks for understanding and your patience by the way :smiley:

EDIT : Also the high and low reading of each axis changes constantly due to minor fluctuations in voltage in the circuit.

Which suggestions have you tried? Is there still a problem?

You don't see a varying zero point as fluctuation?

Not sure which Arduino you're using, but it could be much better if you power the joystick from the 3volt supply, and set the PGA of the ADS to match.
Leo..

The joystick and the ADS are being powered by a 5 V rail because they are part of a controller which contains an LCD an IO Expander and a rotary encoder. And the controller connect to the MCU (in my case and arduino Nano) using 4 wires (SCL SDA VCC GND).

Also the solution i tried was to check the angle value and if it is different from the previous angle by 2 (when mapped) then the motors move. This solved the constant jiggle of the motors

You should have added a diagram in the first post (one of the forum posting rules).
As said in post#2, pots and an ADS1115 is a bad combination.
A bad supply (noise/ripple) for the pots is going to make things worse.
You could fix this by powering the joystick from a tiny linear 3.3volt regulator.
Leo..

I am sorry for not adding a diagram but i solved the problem with your help. The problem was an unstable power supply i am now powering the controller components via a 5V regulator filtered by a 0.1 and a 10 μF capacitor.