Several uses of multiMap() in one sketch how to make smart?

Hi,

for my Stirlingengine datalogger I use about 20 sensors on the Arduino Due
and I need to use multiMap() of Rob Tillaart (many thanks to him!) several times.

The link to the description of multiMap():

https://playground.arduino.cc/Main/MultiMap

I think I miss the forest for the trees but the only way for me was to give the function and variables
different names for each use of multiMap().

int MeanpressurePin=6;
int WorkingpressurePin=7;
int meanpressure;
int meanpressure10;
int hmeanpressure;
int lmeanpressure;
int workingpressure;
int workingpressure10;
int hworkingpressure;
int lworkingpressure;
int out[] = { 0, 15, 130};
int in[] = { 362, 660, 2790};
int val;
int out1[] = { 0, 15, 130};
int in1[] = { 422, 830, 3800};
int val1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  analogReadResolution(12);
}

void loop() {

    meanpressure=analogRead(MeanpressurePin);
    workingpressure=analogRead(WorkingpressurePin);
    val=analogRead(MeanpressurePin);
    meanpressure10=multiMap( val, in, out, 3);
    val1=analogRead(WorkingpressurePin);
    workingpressure10=multiMap1( val1, in1, out1, 3);

    hmeanpressure = meanpressure10/10;                           // for pre-decimal position
    hworkingpressure = workingpressure10/10;
    lmeanpressure = meanpressure10-hmeanpressure*10;            // for fractional part
    lworkingpressure = workingpressure10-hworkingpressure*10;
    

    
    Serial.print(hmeanpressure);
    Serial.print(",");
    Serial.print(lmeanpressure);
    Serial.print("      ");
    Serial.print(hworkingpressure);
    Serial.print(",");
    Serial.println(lworkingpressure);
    
    delay(600);
    
}

 // note: the _in array should have increasing values
int multiMap(int val, int* _in, int* _out, uint8_t size) //for mapping mean pressure
{
  // take care the value is within range
  // val = constrain(val, _in[0], _in[size-1]);
  if (val <= _in[0]) return _out[0];
  if (val >= _in[size-1]) return _out[size-1];

  // search right interval
  uint8_t pos = 1;  // _in[0] allready tested
  while(val > _in[pos]) pos++;

  // this will handle all exact "points" in the _in array
  if (val == _in[pos]) return _out[pos];

  // interpolate in the right segment for the rest
  return (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
}


 // note: the _in array should have increasing values
int multiMap1(int val1, int* _in1, int* _out1, uint8_t size)    //for mapping working pressure
{
  // take care the value is within range
  // val = constrain(val, _in[0], _in[size-1]);
  if (val1 <= _in1[0]) return _out1[0];
  if (val1 >= _in1[size-1]) return _out1[size-1];

  // search right interval
  uint8_t pos = 1;  // _in[0] allready tested
  while(val1 > _in1[pos]) pos++;

  // this will handle all exact "points" in the _in array
  if (val1 == _in1[pos]) return _out1[pos];

  // interpolate in the right segment for the rest
  return (val1 - _in1[pos-1]) * (_out1[pos] - _out1[pos-1]) / (_in1[pos] - _in1[pos-1]) + _out1[pos-1];
}

Its works well but I think it could be made much smarter.

Just for your fun a video of my datalogger still with Uno and small display:

Thanks for your help and best wishes

Ralf

Its works well but I think it could be made much smarter.

Yes, it could. There is no reason to have several functions that do almost the same thing, when each instance uses no global variables.

What problem were you trying to solve when you did that?

As PaulS pointed out one multiMap() can handle all

please check out your slightly modified code
// added some spaces/lines for readability + const for pin definitions

const int MeanpressurePin = 6;
const int WorkingpressurePin = 7;

int meanpressure;
int meanpressure10;
int hmeanpressure;
int lmeanpressure;

int workingpressure;
int workingpressure10;
int hworkingpressure;
int lworkingpressure;

int out[] = { 0, 15, 130};
int in[] = { 362, 660, 2790};
int val;

int out1[] = { 0, 15, 130};
int in1[] = { 422, 830, 3800};
int val1;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  analogReadResolution(12);
}


void loop() {

  meanpressure = analogRead(MeanpressurePin);
  workingpressure = analogRead(WorkingpressurePin);

  val = analogRead(MeanpressurePin);
  meanpressure10 = multiMap( val, in, out, 3);

  val1 = analogRead(WorkingpressurePin);
  workingpressure10 = multiMap( val1, in1, out1, 3);

  hmeanpressure = meanpressure10 / 10;                           // for pre-decimal position
  hworkingpressure = workingpressure10 / 10;
  lmeanpressure = meanpressure10 - hmeanpressure * 10;            // for fractional part
  lworkingpressure = workingpressure10 - hworkingpressure * 10;

  Serial.print(hmeanpressure);
  Serial.print(",");
  Serial.print(lmeanpressure);
  Serial.print("      ");
  Serial.print(hworkingpressure);
  Serial.print(",");
  Serial.println(lworkingpressure);

  delay(600);
}


// note: the _in array should have increasing values
int multiMap(int val, int* _in, int* _out, uint8_t size)
{
  // take care the value is within range
  // val = constrain(val, _in[0], _in[size-1]);
  if (val <= _in[0]) return _out[0];
  if (val >= _in[size-1]) return _out[size-1];

  // search right interval
  uint8_t pos = 1;  // _in[0] allready tested
  while(val > _in[pos]) pos++;

  // this will handle all exact "points" in the _in array
  if (val == _in[pos]) return _out[pos];

  // interpolate in the right segment for the rest
  return (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
}

Many thanks for your kind answer.
I knew I was fundamently missing something.

Many thanks for your help and best wishes

Ralf