Store Min and Max value

I'm trying to create an Min&Max feature for a meter like on my fluke meter, Where I connect it to the voltage source and press the Min&Max button so that when you increase or decrease the voltage I can display the lowest and highest value on the LCD.
Where my code stands at the moment I'm only working on the Min value at the moment, Then when this is sorted I will than add the Max code.
The button part is working where I press and hold the button for 3 seconds it displays the Min and Max, Then if I press the button for another 3 seconds the readings disappear which is what I want to do, As I don't want to display them all the time or set V-in to 4.98V on power up.
The problem I seem to be facing is when I press the button for 3 seconds and then this disaply's the Min Value well that's I was thinking but when I take the voltage higher the Minvalue just updates to the same value as the real time voltage.

I've tried several ways but no matter which wya I try I get the same result.
This is the code.

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
static const int buttonPin = 2;                    // switch pin
boolean MinMAxState = true;
long buttonTimer = 0;
long longPressTime = 3000;
boolean buttonActive = false;
boolean longPressActive = false;
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
int sensorValue = 0;        // value read from the pot
float Volt_reading = 0;
float valueMin ;
void setup() {
  Serial.begin(9600);                 // Initialise the serial monitor
  lcd.begin(20, 4);
  lcd.clear();
  pinMode(buttonPin, INPUT);          // set buttonPin as input
  Serial.println("Press button");
}



void loop() {
  sensorValue = analogRead(analogInPin);
  Volt_reading = (sensorValue * 5.0) / 1024.0; // Convert the reading to decimal
  lcd.setCursor(0, 0);
  lcd.print("V IN:");
  lcd.print(Volt_reading); //s how the reading

  if ( valueMin < Volt_reading  ) {
    valueMin = Volt_reading; // Record the lowest value
  }
  // Display max and Min part
  if (digitalRead(buttonPin) == HIGH) {
    if (buttonActive == false) {
      buttonActive = true;
      buttonTimer = millis();
    }
    // needs button press for 3 seconds to disaply the Min & Max Values
    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
      longPressActive = true;
      MinMAxState = ! MinMAxState;
    }

  } else {
    //short Button press do noting
    if (buttonActive == true) {
      if (longPressActive == true) {
        longPressActive = false;
      }
      buttonActive = false;
    }
  }
  // Onlu show the Min and Max Value if button press for 3 seconds
  if ( ! MinMAxState) {
    lcd.setCursor(0, 1);
    lcd.print("MIN:");
    lcd.print(Volt_reading);

    lcd.setCursor(0, 2);
    lcd.print("MAX:");
    lcd.print("13.98");
  }
  else {
    lcd.setCursor(0, 1);
    lcd.print("MIN:");
    lcd.print("     ");

    lcd.setCursor(0, 2);
    lcd.print("MAX:");
    lcd.print("     ");
  }

}

I would like to get it to work like my fluke meter so I can see what the voltage drop's to. This is part of a bigger project but though get this part working first then move on from there plus keep the code small for others to read.

Not sure where I'm going wrong or over thinking

Your comparison is backward. You want to replace valueMin if Volt_reading is LESS THAN valueMin.

You should start 'valueMin' at some HIGH value, not at zero. I would use 6.0.

Thanks John, I also spotted that I was printing the Volt_reading instead of the valueMin value

You are overthinking. Keep all your logic as int with the value from the AD converter. Make the minimum and maximum always as integer. All your compares should be integer.
ONLY at the point of displaying your values should you convert the int value to a float value and only for display that you can read. That will also be the way your Fluke works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.