Map values different

I'm using an ESP32 and a ADS1115 to read an hall effect joystick which gives 2.56V center then pull it backwards it goes down to 0.5V and push it forwards it goes up to 4.59V and what I'm trying to achieve is using the map function.

So when I push it forwards one map function increases the value to 255 and the other map function is to decrease to 0 and the opposite when I pull it backwards . But the trouble is while sitting in the center and not been moved the map values are different one reads 127 and the other reads 128, Which I thought they should read the same ?

Not sure why this is happening ?

#include <Wire.h>
#include <TimedAction.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);
uint16_t Send_Value[8];
uint16_t  potValue;
int16_t adc0;
float voltageFloat = 0;
//==================================================================================//
void TimerService01();// ADC ROUTINE Timed action
TimedAction Timedact01 = TimedAction(100, TimerService01);// mS
void setup() {
  Serial.begin (115200);
  ads.begin();
  ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- esp32 ads1115V  1 bit = 3mV      0.1875mV (default)

}
//==================================================================================//
void loop() {
  Timedact01.check(); // Get the A/D convesrion
  // Send_Value[4] = map(potValue, 2889, 24450, 0,255); //map 0.5V to 4.59V
  //Send_Value[5] = map(potValue, 2889, 24450, 255,0); // Map 4.59V to 0.5V
  Serial.print(potValue);
  Serial.print(" : ");
  Serial.print( voltageFloat);
  Serial.print(" : ");
  Serial.print(Send_Value[4]);
  Serial.print(" : ");
  Serial.println(Send_Value[5]);

}


void TimerService01() {
  // Reading Joystick potentiometer value
  potValue = ads.readADC_SingleEnded(0);
  Send_Value[4] = map(potValue, 2889, 24450, 0, 255); //map 0.5V to 4.59V
  Send_Value[5] = map(potValue, 2889, 24450, 255, 0); // Map 4.59V to 0.5V
  voltageFloat = (potValue * 6.144) / 32767; // see text
}

//==================================================================================//

and here is the output

13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13676 : 2.56 : 128 : 127
13682 : 2.57 : 128 : 127
13682 : 2.57 : 128 : 127
13682 : 2.57 : 128 : 127

Why do you think it should be the same? As with your formulas the first value is equal to 255 - second one.
So 255 - 127 = 128 is correct answer

1 Like

Why do you want to get two values that both represent the joystick position but in different ways?
And what do you intend to do with the values? Maybe representing a 15 bit value as an 8 bit value isnt a good idea - otherwise why not use the ESP32's ADC?

I just thought that if the centre value is 13767 that they should read the same value which is 128.

This is part of a system that uses a Hall effect joystick that has 2 output voltages where 2.5v centre and one side increases and other side decreases, which is used to indicate forwards and backwards plus a fail safe system. There is 8 of them which all goes into a master unit then they convert this gets converted to send the data over canbus.
Reading the canbus data I can see that one of the joystick uses 2 bits which I can see 2x 0X80 has neutral position then when you push it forwards the one goes to 0 and the other goes to FF working through the range depending on how far you push it.
What I want to do is use something like PS3/4 controller using the buttons to select which joystick to use on the system and then use the joystick side on the controller to do the same job. but using an ESP32 to check and get things working first. I'm using a single hall effect joystick at the moment but will swap to the controller once ting are working.

But the joystick on the controller only has a single output so I wanted to use the map function to create the same effect by remotely connecting it to the canbus system sending the same values has the original system.
Hope this makes sense

no, the centre between 2889 an 24450 is 13669.

You not able to get the same value for two map functions just because the centre of 0 - 255 range is 127.5 - is not integer value.

What is the problem with the fact, that two values are not the same?

Because 128(0X80) is the neutral position and it causes an issue if one is out at neutral position because it thinks it’s the joystick is moved and starts to operate, May be I’ll try using the ESP32 ADC set either 12/10 bit plus use a resistor divider means that the voltage is 5 and see if that gets better results

...overcomplicated....

Why do not use the one map function for both values and calculate first value from the map func directly and the second as 256 - first? It will give you a desired result - the centre of the both ranges will be exactly 128.
The only thing you will need to add - a limit the second value against exceeding the 255, say, by simple if condition

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