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