If you refactor the code into more of a state machine, you can avoid a lot of those if/else statements. Something like this (and use an enum to make which page you on easier for humans to read)
#include <HX711_ADC.h>
#include <EEPROM.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
const int RXPin = 2;
const int TXPin = 3;
const int GPSBaud = 9600;
const int HX711_dout = A4;
const int HX711_sck = A5;
HX711_ADC LoadCell(HX711_dout, HX711_sck);
TinyGPSPlus gps;
SoftwareSerial gpsSerial(RXPin, TXPin);
const unsigned long serialPrintInterval = 100;
float calibrationValue = 430.08;
long TareOffset = 8342702;
#include <LiquidCrystal.h>
LiquidCrystal lcd (10, 9, 8, 7, 6, 5);
const byte BtnA = 4;
const byte BtnB = A1;
const byte BtnC = A2;
const byte BtnD = A3;
enum { pHOME, pSELECT, pFILL, pQUALITY, pFILLING, pCONFIRM };
int page = pHOME;
float weight;
float kgs;
float units_ml;
float mass;
int petrol = 0;
int diesel = 0;
boolean SwA = false;
boolean SwB = false;
boolean SwC = false;
boolean SwD = false;
const unsigned long debounceDelay = 50;
boolean pass = false;
void checkIf_A_ButtonIsPressed()
{
static unsigned long lastTime;
static int lastState;
int state = digitalRead(BtnA);
if (state != lastState) lastTime = millis();
if (millis() - lastTime >= debounceDelay) {
if (state == 0) SwA = true;
}
lastState = state;
}
void checkIf_B_ButtonIsPressed()
{
static unsigned long lastTime;
static int lastState;
int state = digitalRead(BtnB);
if (state != lastState) lastTime = millis();
if (millis() - lastTime >= debounceDelay) {
if (state == 0) SwB = true;
}
lastState = state;
}
void checkIf_C_ButtonIsPressed()
{
static unsigned long lastTime;
static int lastState;
int state = digitalRead(BtnC);
if (state != lastState) lastTime = millis();
if (millis() - lastTime >= debounceDelay) {
if (state == 0) SwC = true;
}
lastState = state;
}
void checkIf_D_ButtonIsPressed()
{
static unsigned long lastTime;
static int lastState;
int state = digitalRead(BtnD);
if (state != lastState) lastTime = millis();
if (millis() - lastTime >= debounceDelay) {
if (state == 0) SwD = true;
}
lastState = state;
}
void DisplayFuel()
{
lcd.setCursor(19, 3);
if (petrol) lcd.print("P");
else if (diesel) lcd.print("D");
else lcd.print("?");
}
void makeDecision()
{
float minWeight, maxWeight;
lcd.setCursor(0, 1);
if (petrol) {
minWeight = 720.0;
maxWeight = 775.0;
}
else if (diesel) {
minWeight = 820.0;
maxWeight = 880.0;
}
else {
lcd.print("???");
return;
}
if (weight > minWeight && weight < maxWeight) {
lcd.print("Good.");
}
else {
lcd.print("Bad.");
}
}
void checkFuelQuality()
{
chkWeight();
makeDecision();
}
void getWeight() {
readWeight(true);
}
void chkWeight() {
readWeight(false);
}
void readWeight(bool report)
{
static unsigned long lastSerialTime;
bool newDataReady = false;
if (LoadCell.update()) newDataReady = true;
if (newDataReady) {
if (millis() - lastSerialTime >= serialPrintInterval) {
weight = LoadCell.getData();
lastSerialTime = millis();
}
if ( report ) {
Serial.print("Raw weight:");
Serial.println(weight);
Serial.print("Unit/ml factor:");
Serial.println(units_ml);
mass = weight * units_ml;
Serial.print("Mass:");
Serial.println(mass);
if (mass >= 1000) {
kgs = mass / 1000.0;
}
Serial.print("Converted in ltrs:");
Serial.println(kgs);
lcd.print(kgs);
lcd.print("Ltrs");
DisplayFuel();
}
}
}
void get_unit_mass()
{
units_ml = 500.0 / weight;
}
void setup()
{
Serial.begin(115200);
LoadCell.begin();
gpsSerial.begin(GPSBaud);
LoadCell.start(2000, true);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue);
LoadCell.setTareOffset(TareOffset);
Serial.println("Startup is complete");
}
pinMode(BtnA, INPUT_PULLUP);
pinMode(BtnB, INPUT_PULLUP);
pinMode(BtnC, INPUT_PULLUP);
pinMode(BtnD, INPUT_PULLUP);
lcd.begin(20, 4);
lcd.setCursor(5, 0);
lcd.print("Welcome to");
lcd.setCursor(2, 1);
lcd.print("Ready Automotive");
lcd.setCursor(0, 3);
lcd.print("Technology Solutions");
delay(3000);
lcd.clear();
}
void loop()
{
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
if (gps.location.isUpdated()) {
chkWeight();
Serial.print("<");
Serial.print(gps.location.lat(), 6);
Serial.print(",");
Serial.print(gps.location.lng(), 6);
Serial.print(",");
Serial.print(weight);
Serial.println(">");
}
}
checkIf_A_ButtonIsPressed();
checkIf_B_ButtonIsPressed();
checkIf_C_ButtonIsPressed();
checkIf_D_ButtonIsPressed();
drawMenu(page);
}
void drawMenu(int page)
{
static int currentPage = -1;
bool redraw = (page != currentPage );
if ( redraw ) {
lcd.clear();
}
switch (page) {
case pHOME:
if ( redraw ) {
lcd.setCursor(3, 1);
lcd.print("Fuel Volume:");
lcd.setCursor(5, 2);
}
getWeight();
if (SwA) {
SwA = false;
if (petrol == 1 || diesel == 1) page = pFILL;
else page = pCONFIRM;
}
if (SwD) {
SwD = false;
page = pSELECT;
}
break;
case pSELECT:
if ( redraw ) {
lcd.print("Select Fuel");
lcd.setCursor(0, 1);
lcd.print("Press:");
lcd.setCursor(0, 2);
lcd.print("A: Petrol B: Diesel");
}
if (SwA) {
SwA = false;
lcd.setCursor(0, 3);
lcd.print("Petrol Selected");
LoadCell.tareNoDelay();
delay(1000);
page = pFILL;
petrol = 1;
diesel = 0;
}
if (SwB) {
SwB = false;
lcd.setCursor(0, 3);
lcd.print("Diesel Selected");
LoadCell.tareNoDelay();
delay(1000);
page = pFILL;
diesel = 1;
petrol = 0;
}
break;
case pFILL:
if (redraw) {
lcd.print("Please fill");
lcd.setCursor(0, 1);
lcd.print("500ml Fuel");
lcd.setCursor(0, 2);
lcd.print("Press A");
lcd.setCursor(0, 3);
lcd.print("when done");
DisplayFuel();
}
if (SwA) {
SwA = false;
chkWeight();
get_unit_mass();
page = pQUALITY;
}
break;
case pQUALITY:
if ( redraw ) {
lcd.print("Fuel Quality is:");
checkFuelQuality();
lcd.setCursor(6, 1);
lcd.print("Proceed?");
lcd.setCursor(0, 2);
lcd.print("A -> Yes");
lcd.setCursor(12, 2);
lcd.print("B -> NO");
DisplayFuel();
}
if (SwA) {
SwA = false;
page = pFILLING;
pass = true;
}
if (SwB) {
SwB = false;
page = pHOME;
}
break;
case pFILLING:
if ( redraw ) {
lcd.print("Tank being filled");
lcd.setCursor(0, 1);
lcd.print("Please wait...");
lcd.setCursor(0, 3);
lcd.print("Press B to exit");
DisplayFuel();
}
lcd.setCursor(0, 2);
getWeight();
if (SwB) {
SwB = false;
page = pHOME;
}
break;
case pCONFIRM:
if ( redraw ) {
lcd.print("Please Select Fuel");
lcd.setCursor(0, 1);
lcd.print("Press D");
lcd.setCursor(0, 2);
lcd.print("to select Fuel");
}
if (SwD) {
SwD = false;
page = pSELECT;
}
break;
}
}