RobUK:
Less than 1v would be considered "under range" and over 5v "over range". Would be nice if the display could show those words.
Here is a code for float-to-float interpolation from a table.
I included an example program that simulates input voltages in the range 0.980 ... 5.019 Volt and shows how to handle "under range" and "over range" text messages.
// code for thread http://forum.arduino.cc/index.php?topic=297755.0
// The look up table. First numbers are voltage, second numbers are what I want to lookup and display.
struct interpolate_t {float voltage; float pressure;};
interpolate_t pressureTable []= {
{1, 0.0001},
{1.025, 0.000231},
{1.05, 0.000621},
{1.1, 0.00136},
{1.2, 0.00297},
{1.3, 0.00461},
{1.4, 0.00651},
{1.5, 0.0102},
{1.6, 0.0147},
{1.7, 0.0191},
{1.8, 0.0295},
{1.9, 0.0416},
{2, 0.0561},
{2.1, 0.072},
{2.2, 0.0894},
{2.3, 0.113},
{2.4, 0.145},
{2.5, 0.176},
{2.6, 0.222},
{2.7, 0.316},
{2.8, 0.413},
{2.9, 0.54},
{3, 0.682},
{3.1, 0.841},
{3.2, 1.06},
{3.3, 1.33},
{3.4, 1.6},
{3.5, 1.87},
{3.6, 2.26},
{3.7, 2.75},
{3.8, 3.24},
{3.9, 3.73},
{4, 4.39},
{4.1, 5.29},
{4.2, 6.27},
{4.3, 7.63},
{4.4, 9.39},
{4.5, 12.7},
{4.6, 16.7},
{4.7, 22.4},
{4.75, 28.8},
{4.8, 35.3},
{4.85, 44.8},
{4.9, 66.5},
{4.95, 141},
{4.975, 616},
{5, 1000},
};
#define TABLEENTRIES sizeof(pressureTable)/sizeof(pressureTable[0])
#define UNDERRANGEVALUE 0.0
#define OVERRANGEVALUE 10000.0
float interpolate_f2f(float inValue)
{
if (inValue<pressureTable[0].voltage) return UNDERRANGEVALUE;
if (inValue> pressureTable[TABLEENTRIES-1].voltage) return OVERRANGEVALUE;
int i;
for (i=1;i<TABLEENTRIES;i++)
{
if ( pressureTable[i].voltage>inValue) break;
}
if (i>TABLEENTRIES-1) i=TABLEENTRIES-1;
return pressureTable[i-1].pressure + (pressureTable[i].pressure-pressureTable[i-1].pressure) * (inValue-pressureTable[i-1].voltage) / (pressureTable[i].voltage-pressureTable[i-1].voltage);
}
void setup() {
Serial.begin(9600);
for (int i=980;i<5020;i++) // simulate input in millivolts
{
float voltage=i/1000.0;
float pressure= interpolate_f2f(voltage);
Serial.print(voltage,3);
Serial.print('\t');
if (pressure==UNDERRANGEVALUE)
Serial.println("under range");
else if (pressure==OVERRANGEVALUE)
Serial.println("over range");
else
Serial.println(pressure,6);
}
}
void loop() {
}
If you need to save RAM, you would have to put the table into PROGMEM instead RAM.
Edit: Fixed calculation (first version was wrong)