Example loop() to show separating the update of the LCD, not using clear() and millis() timing to get rid of delay and do "multitasking". Illustration, not tested.
void loop ()
{
doEthernet();
updateLCD();
}
void doEthernet() // called very time through loop, but executes only every 3 seconds.
{
static unsigned long timer = 0;
unsigned long interval = 3000;
if (millis() - timer >= interval)
{
timer = millis();
float inputReading = distanceSensor.measureDistanceCm();
int CurPercentage = (100 - (inputReading / MaxHeight * 100));
int CurVol = MaxVol / 100 * CurPercentage;
// update Progress Bar
updateProgressBar(CurPercentage, 100, 1);
// Wait for an incoming connection
EthernetClient client = server.available();
if (!client)
return;
Serial.println(F("New client"));
while (client.available()) client.read();
StaticJsonDocument<100> doc;
doc["data"] = CurPercentage;
// Write response headers
client.println(F("HTTP/1.0 200 OK"));
client.println(F("Content-Type: application/json"));
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(measureJsonPretty(doc));
client.println();
serializeJsonPretty(doc, client);
client.stop();
}
}
void updateLCD() // called very time through loop, but executes only every 1 second.
{
static unsigned long timer = 0;
// I know that there is no use updating the LCD faster than the ethernet
// just showing that you can
unsigned long interval = 1000;
if (millis() - timer >= interval)
{
timer = millis();
lcd.setCursor(0, 0);
lcd.print(" "); // overwrite old data with spaces
lcd.setCursor(1, 0); // reset cursor
lcd.print(CurVol);
lcd.print(" L - ");
lcd.print(CurPercentage);
lcd.print(" %");
}
}