Map values from integer to float

Hello,

I came across this post: https://forum.arduino.cc/index.php?topic=3922.0

However implementing it.... I'm lost.

I have a pressure transducer. Reads 0-16 bar. 5V supply, Outputs 10% min, 90% max so the ADC value is 100 for minimum, 920 for maximum (confirmed with testing, it's close). I want to map the ADC value to float, to get decimal point precision.

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

I don't understand (or rather can't get it to work). basic setup (whole ino is way, way too big to post trying to cut only necessary parts)

int boilerPressureRaw;
float boilerPressure;
boilerPressureRaw = analogRead(A2);

???? 

Serial.print(F("Boiler Pressure Is: "));
Serial.print(boilerPressure);
Serial.println(F(" bar"));

I've come a long way, for something that seems so trivial to not work. I don't even understand where the value is returned to, or what in that example at the top is an actual variable, or just a text placeholder.

I know my min/max values for in/out but not how to implement them into the example above, and as I said, where is that value even returned to?

Input Min: 100 Input Max: 920 Out Min: 0 Out Max: 16

Please, and thank you.

Why not map things as integers then convert to your required float ?

You add that function into your code (after your loop() function is fine) and then you call it and the return value is assigned to a variable

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

int boilerPressureRaw;
float boilerPressure;
boilerPressureRaw = analogRead(A2);

boilerPressure = mapFloat(bilerPressureRaw, 100.0, 920.0, 0.0, 16.0);

Serial.print(F("Boiler Pressure Is: "));
Serial.print(boilerPressure);
Serial.println(F(" bar"));
// ...

blh64: You add that function into your code (after your loop() function is fine) and then you call it and the return value is assigned to a variable

God I feel like such an idiot. I had no idea that a new function was being declared. Didn't know a function would have "float" in front of it. Only functions i've worked with are void. Now as I'm typing this it makes sense what void even MEANS in the first place.

I swear, it's like every time I think I have a grasp on fundamentals, I am learning something new.

Thank you, thank you. Works perfectly, and better yet I understand why it didn't work, and why it does now.

larryd: Why not map things as integers then convert to your required float ?

1) Didn't know that was possible and 2) Wouldn't even know how to begin haha!

When I tried the initial map, it was only returning rounded, whole numbers.

Would I need to declare my map output as a long, not int?

I tried:

boilerPressure = map(boilerPressureRaw, 100, 920, 0, 16);

and while I was getting the "correct" output, it was integer form. So an expected return of 3.2 bar was output as 3 bar.

How would it output as an integer that I could then divide to get a float?

Here is a starting point:


int boilerPressureRaw;
int boilerPressure;
float boilerPressureFloat;
. . .
boilerPressureRaw = analogRead(yourAnalogPin);
boilerPressure = map(boilerPressureRaw, 0, 1023, 0, 1600);

boilerPressureFloat = boilerPressure/100;

larryd: Here is a starting point:


int boilerPressureRaw; int boilerPressure; float boilerPressureFloat; . . . boilerPressureRaw = analogRead(yourAnalogPin); boilerPressure = map(boilerPressureRaw, 0, 1023, 0, 1600);

boilerPressureFloat = boilerPressure/100;

Ah! I got it! Thank you so much! Never would have thought to map the output to a base 10 value higher, then divide. I need to think outside the box, and not so literally more often.

Thank you both, very very much!

You probably should use:

boilerPressureFloat = boilerPressure/100.0;