# Min and Max reading from analog sensor

Hi to all,

I have to measure some pressure with analog sensor 4-20mA and I need Min and Max reading from it, just to display it.
Now I need your help because I dont know how to start and if this Is a possible to do. Do I need some specila formula to calculate outputValue ?

Second thing is how to get rid of strange signs behind “BAR” on the picture?

Third thing is if can I read the value in 1 or 2 decimals like 100,50 BAR, because now i dont have ?

Below is my curent code:

``````#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int sensorValue = 0;
int outputValue = 0;

void setup()
{
lcd.begin(40, 4);
lcd.print("  PRESSURE TESTER  ");
}

void loop() {

outputValue = ((sensorValue-204)*250)/(1023-204);

if(outputValue>-0.5)

{
lcd.setCursor(0,1);
lcd.print("Sensor 1 = ");
lcd.print(outputValue);
lcd.println(" BAR ");

lcd.setCursor(0,2);
lcd.print("Pmax = ");
lcd.println(" BAR ");

lcd.setCursor(0,3);
lcd.print("Pmin = ");
lcd.println(" BAR ");

}
else
{

lcd.setCursor(0,1);
lcd.println(" Sensor 1 Error "); //Show error if value is less than 4mAmp
}
delay(200);        // delay in between reads for stability
}
``````

`lcd.println` Could that be your problem?

kikitron:
I have to measure some pressure with analog sensor 4-20mA and I need Min and Max reading from it, just to display it.
Now I need your help because I dont know how to start and if this Is a possible to do. Do I need some specila formula to calculate outputValue ?

Create a data structure that holds the current min/max information. Whenever you do a new measurement, check if the value is smaller than min or bigger than max. If so, update accordingly.

EDIT: Something along these lines:

``````template <class T>
class MinMax
{
private:
T min;
T max;
public:

void initialize(T initialValue)
{
min = initialValue;
max = initialValue;
}

void update(T value)
{
if(value < min)
min = value;
if(value > max)
max = value;
}

T getMin()
{
return min;
}

T getMax()
{
return max;
}
};
``````

Use it like this:

``````// Create a new instance of the MinMax class with the type being int
MinMax<int> sensorMinMax;

// intialize the min/max values to 4 (or your first measurement)
// This is usefull, if your data range is offset from 0. Otherwise the
// minimum value will always stay at 0.
sensorMinMax.initialize(4);

// update the min/max value. After this operation getMin() returns 1
sensorMinMax.update(1);

// update the min/max value. After this operation getMax() returns 5
sensorMinMax.update(5);
``````