Issues with Constrain and Map working together !

Hello,

I got this bit of code from a bigger piece of code, to work with the PX26100DV pressure sensor that I have. But I have made a seperate sketch and still can’t seem to isolate the problem. My issue is I don’t seem to be getting accurate results for my map function.

Here is the code:

//////////////////////
//Pressure Variables//
//////////////////////
const int diffPressurePin=A1;
float diffPressure=0;
#define analogminDiff 209
#define analogmaxDiff 1023


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

void loop(){
  int diffreading = constrain (analogRead(diffPressurePin),analogminDiff, analogmaxDiff);
  Serial.print("Analog DiffPressure: "); Serial.println(diffreading);
  float diffVolt = map (diffreading, analogminDiff, analogmaxDiff, 1, 3); // Might need to change to 5 
  Serial.print("Volt Diff: "); Serial.println(diffVolt);
  if (diffVolt < 1.0167) {diffPressure = 0;}
      if (1.0167 <= diffVolt && diffVolt <= 1.05) {diffPressure= map(diffVolt, 1.0167, 1.05, 1,5);}
         if (1.05 < diffVolt && diffVolt <= 3) {diffPressure = map(diffVolt, 1.05, 3, 5.001, 100);}
}

diffreading seems to be reading fine, at zero pressure it’s at 290 as I defined it and diffVolt is equal to 1.0 as it should be too! BUT when diffreading increases with increased pressure diffVolt stays at 1.0, I only saw it increases to 2.0 at 692 and 700. But at 492 and below diffVolt stayed at 1.0 … shouldn’t it adjust to 1.001, 1.002, as diffreading increases past 209? Or am I understanding the map function wrong?

Any insight or guidance would be greatly appreciated !

Regards,
UST

You are understanding map wrong. It works with integers, not floating point numbers. It is easy to create your own with floating point.

The whole function is listed here:

Just create your own flt_map() function with floats instead of longs.

The map function uses long (32 bit integer) math, not float math

if (diffVolt < 1.0167) {diffPressure = 0;}
      if (1.0167 <= diffVolt && diffVolt <= 1.05) {diffPressure= map(diffVolt, 1.0167, 1.05, 1,5);}
         if (1.05 < diffVolt && diffVolt <= 3) {diffPressure = map(diffVolt, 1.05, 3, 5.001, 100);}
}

the map formula is simple so rewriting it for float is almost trivial.

y = (x - in_min) * (out_max - out_min) / (in_max - in_min);

try to use better coding style like below to make your code more readable.
rewrittten the 3 lines above in about 10 lines

  • making it failsafe (all values for diffVolt are handeld now)
  • minimizing the if statements executed by using else
  • don’t test what is already known.
  • expanded the map for floats.
if (diffVolt < 1.0167) 
{
  diffPressure = 0;
}
else if (diffVolt <= 1.05) 
{
  diffPressure = (diffVolt - 1.0167) * (5.0 - 1.0) / (1.05 - 1.0167);  
}
else if (diffVolt <= 3) 
{
  diffPressure = (diffVolt - 1.05) * (100.0 - 5.0) / (3.0 - 1.05);
}
else diffPressure = 100.0; // to be defined
}

you’re welcome

If you want small and fast code, just get rid of floats here. You are dealing with physical sensors that have a resolution of maybe 1000 (milliVolts, millibar, whatever), so an int is all you need for sensor values, maybe a long is necessary for storing intermediate results. Precision will be better because integer types have MORE significant digits than floats.

Wow! All of those are great responses, I really appreciate it :slight_smile:

Thank you robtillaart for going the extra step, I do usually write my code as you instructed but for myself when I want it to look more condensed I write it as I posted; but I should post it so it's easier to read for others! Thanks again :slight_smile:

Cheers,
UST

robtillaart ,

Very sorry to bother you again, last question, sort of a follow up on your answer. The code you gave is simple and great, thank you; but since maps don't deal with floats and they deal with long and int values, does that not mean I need to change this line as well:

 float diffVolt = map (diffreading, analogminDiff, analogmaxDiff, 1, 3);

Probably changing it float to int ? but this would then interfere with the code you setup for me, I could do something similar to what you did to map my diffVolt to diffreading?

Cheers,
UST

Once you've created your new super squishio flt_map function simply replace map in the line you mention to read flt_map;