# Water Pressure Sensor Value using map

I will be using a water pressure sensor in order to obtain a PSI value.

The data sheet from the sensor shows that 0.5v = 0 psi, and 5v = 100psi

Can someone let me know if the following line of code is correct to get the psi value using map, And also should the upper limit be 1024 or 1023. I still get a bit confused on what is correct…

``````waterpressure = map(incomingsignal, 102, 1024, 0, 100);    // I used 1023/5 * 0.5v =102.3  = 102
// to get the 0 PSI to start at 0.5v instead of
// using a 0, to start at 0 volts.
``````

Thanks a lot.

Instead of map, you can use

``````int psi = 100L*(incomingsignal - 102)/(1023-102);
``````

The "L" forces long integers to be used, to avoid overflow.

Thank you, could you explain "to avoid overflow" ?

Still learnin' this stuff...

Thanks,

katana_flyer:
Thank you, could you explain to prevent over flow ?

Still learnin' this stuff...

Thanks,

The largest value an int can hold is 32767. So let's say you had this in code:

``````unsigned long someVal = (10000 * 6) / 5;
``````

The 10000 * 6 gets done first. That's 60000. That doesn't fit into an int. So it overflows and in this case becomes a negative number. Then when you divide by 5 at the end you end up with a negative number and you come here to post about why the Arduino can't do math.

By putting a L or UL on the end of a number it forces it to be treated as a long or unsigned long and then you get up to 2 or 4 billion before it rolls over.

Ah Okay Delta_G, makes sense. Thanks for the explanation. So in my initial post here, where I use map, there would be no chance of any over flow problem right? There's no way it can get to over 32767.

Are there disadvantages of using map vs the example Jremington gave as seen here... ?

jremington:
Instead of map, you can use

``````int psi = 100L*(incomingsignal - 102)/(1023-102);
``````

The "L" forces long integers to be used, to avoid overflow.

Thank you,

katana_flyer:
Are there disadvantages of using map vs the example Jremington gave as seen here... ?

If you'll look at the implementation of MAP, you'll see that the two are one and the same. The only issue is that map is a compile time #define, so it comes with all the caveats of using #defines to build what should be functions.

The only issue is that map is a compile time #define

Really?

``````long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````

Looks like a regular function, to me.

Man I could have sworn that it bit me before. Maybe I'm thinking if abs. Oh well. Ignore what I said then.

Maybe I'm thinking if abs.

``````#define abs(x) ((x)>0?(x):-(x))
``````

Bingo.

Oh, and constrain() is a #define macro, too.

Oh, I bet it was constrain that bit me then. I know I spent a night trying to figure out a bug that came down to one of those functions evaluating an expression twice. That was the last time I relied on a function from the Arduino core to do math for me that I could just as well code out myself.