# Scale function

I've been looking for some good scale lib's and i decided to rewrite my Siemens S7 code and put it in a arduino!

I really don't know if it is the best approach but i really think that it is very easy and also very easy to calibrate.

If you know a better way just reply....

``````// H_out = Highest possible output
// L_out = Lowest possible output
// H_in = Highest possible input
// L_in = Lowest possible input
// calib = final calibration
// input = the scaling start-off (probably te analog input)

float Scale_sensor(float H_out, float L_out, float H_in, float L_in, float Calib, float Input){

float result;
float Part_1 = H_out - L_out;
float Part_2 = Input - L_in;
float Part_3 = Part_2 * Part_1;
float Part_4 = H_in - L_in;
float Part_5 = Part_3 / Part_4;
float Part_6 = L_out + Part_5;
result = Part_6 + Calib;

return result;
}
``````

I used this with some NTC sensors and it works perfect!

I think the map function does the same thing, but good work! I think the map function does the same thing

...but with integer arithmetic

Yes you're right sorry.

Good function kenny_-_,

map() does indeed the same in int domain except for the calibration value which is often needed.

Some remarks:

• write it without intermediate variables, compiler can handle this oneliner.
• keep the order of the parameters the same as the map function (acceptance in the community); maybe call it mapCalibrate() ?
• do the divide first and then 2nd multiply to prevent overflow.

==> return L_out + ((Input - L_in) /(H_in - L_in)) * (H_out - L_out) + Calib;

Yes really good thank you i'll keep that in mind, what it was that i literary copied it from s7-ladder to vb.net (for some other project) and also to the arduino.
That's why it's written like this (and yes i'd have to say that i didn't put to much effort in the translation) but now i will change it to
your formula, it'll be better i agree!

Sometimes it's good to post something to the community because you will get something back, thanks guys!

``````// H_out = Highest possible output
// L_out = Lowest possible output
// H_in = Highest possible input
// L_in = Lowest possible input
// calib = final calibration
// input = the scaling start-off (probably te analog input)

float mapCalibrate(float H_out, float L_out, float H_in, float L_in, float Calib, float Input){

return  L_out +  ((Input - L_in) /(H_in - L_in)) * (H_out - L_out) + Calib;
}
``````

(for the record)