I Need help with the formula. its reading an analogue vacuum pressure gauge. it reads in inches of mercury (Hg) 0Hg to - 30Hg . -30 being -1 Bar of pressure.
Its all working, but im being fussy and need some advise on the formula.
The gauge only ever reaches -20Hg as thats all the pump is capable of. However, my problem lies with the coding it starts at -40Hg and goes DOWN to -20Hg. I want it to start at 0Hg and go UP to -20Hg.
Now this is the code that my friend wrote for it:
Pwatch = analogRead(Psen);
lcd.setCursor(0, 1);
lcd.print("Pressure:");
lcd.print (((25*Pwatch)/2550)*4);
lcd.print("Hg ");
delay (25);
............................................................................
I tried manipulating it but whatever i do to make that initial -40Hg start at 0Hg it effects the rest of the code and doesnt go up or down it just constantly reads 0Hg even though the mechanical vacuum gauge is reading -20Hg.
Can you suggest any ways in which i can achieve what i want it to do? Thanks A lot
P.s This is the data Sheet link http://www.freescale.com/files/sensors/doc/data_sheet/MPXV6115V.pdf
What values do you see if you just print Pwatch? Should be 0-1023.
Which end of the range correlates to 0Hg and which to -20Hg?
1023 when it isnt turned on - doing nothing
So as the pressure drops, the value drops?
Hg = 1023 - (Pwatch)
at 0, 1023 - 1023 = 0
at -5, 1023 - 767 = 256
at -10, 1023 - 512 = 511
at -15, 1023 - 256 = 767
at -20, 1023-0 = 1023
Likely your results are not that linear, but you get the concept?
try this
Pwatch = analogRead(Psen);
int Pressure = Pwatch * 0.039215686; // this is al constants evaluated
easier might be
P = map(analogRead(Psen), 0, 1023, minHg, maxHg);
thanks for the responses, i will have to try them tomorrow and let you know. Thanks 
i updated my code with the following
Pwatch = analogRead(Psen);
P = map(Pwatch,0,1023,0,20);
lcd.setCursor(0, 1);
lcd.print("Pressure:");
lcd.print(P);
lcd.print("Hg ");
delay (25);
it does what i want it to now. Thank you very much
I just need to tweek it more more time
when it reads -10Hg on the mechanical vacuum gauge its reading -4Hg or -5Hg on my LCD screen.
how do i manipulate the formula now...is it to do with the 0,20 part of the code. do i change that 20 to a 10 or 40? i dont know about coding (as you can tell)
Thanks again
sorry i meant the code is:
Pwatch = analogRead(Psen);
P = map(Pwatch,1023,0,0,20);
lcd.setCursor(0, 1);
lcd.print("Pressure:");
lcd.print(P);
lcd.print("Hg ");
delay (25);
i swapped the 0,1023 around to 1023,0
Yes, play with the map line.
You could also make a 1024 byte (1Kbyte) look up table (stored in flash) to be more precise:
const byte lookupArray[] = {
20,20,20,20, // as many as correspond to 0,1,2,3,4 from analogRead
:
15,15,15,15,
:
10,10,10,10,
:
5,5,5,5,
:
0,0,0,0, // as many as correspond to 1020, 1021,1022,1023 - fill in all 1024 values (0 to 1023)
}
Then:
HG = lookupArray[analogRead(Psen)];