Web Server are laggy, so slow or even can't be opened

I'm want to make a code for 3 different system, that is waterpump, light and motor servo. when I adding servo code it will causing the web server can't be accessed.

here is my full code

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetServer.h>
#include <Servo.h>
#include <Wire.h>
#include <BH1750.h>

//Isopods
#define SERVO_PIN 10
#define SECOND 1000L
#define MINUTE (60 * SECOND)
#define HOUR (60 * MINUTE)
//#define DAY (24*HOUR)
int servoMode = 0;
int servoCount = 0;

const int FILL_DEGREE = 5;       //starting hole location, fill slider with fish food
const int DISPENSE_DEGREE = 45;  //dispensing hole location
//uncomment for actual run
#define DAY (30 * SECOND)  //test setup

//Water
#define WATER_PIN 7
#define SOIL_SENSOR_PIN A0
int maxVals;
int minVals;

//light
#define LIGHT_PIN 5
BH1750 lightMeter;



EthernetServer server(80);

Servo servo1;
void setup() {
  pinMode(WATER_PIN, OUTPUT);
  pinMode(SOIL_SENSOR_PIN, INPUT);
  digitalWrite(WATER_PIN, LOW);  // initially turn off the relay
  servo1.attach(SERVO_PIN);

  pinMode(LIGHT_PIN, OUTPUT);
  digitalWrite(LIGHT_PIN, LOW);

  Wire.begin();
  lightMeter.begin();

  // Initialize Ethernet connection and server
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  IPAddress ip(192, 168, 1, 177);
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
}
void loop() {
  uint16_t lux = lightMeter.readLightLevel();

  if (lux < 15) {
    digitalWrite(LIGHT_PIN, HIGH);
  } else {
    digitalWrite(LIGHT_PIN, LOW);
  }
  
  EthernetClient client = server.available();
  if (client) {
    String request = client.readStringUntil('\r');
    if (request.indexOf("/HIGH") != -1) {
      maxVals = 305;
      minVals = 255;
    } else if (request.indexOf("/MEDIUM") != -1) {
      maxVals = 310;
      minVals = 275;
    } else if (request.indexOf("/LOW") != -1) {
      maxVals = 315;
      minVals = 295;
    }
    if (client) {
      bool currentLineIsBlank = true;
      while (client.connected()) {
        if (client.available()) {
          char c = client.read();
          if (c == '\n' && currentLineIsBlank) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println();
            client.println("<HTML>");
            client.print("<HEAD>");
            client.print("<meta http-equiv=\"refresh\" content=\"3\">");
            client.print("<TITLE />Arduino Web Server</title>");

            client.println("");
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<body>");
            client.println("<h1>Arduino Water Control</h1>");
            client.println("Level Humidity Terrarium:");
            client.println("<a href=\"/HIGH\"><button>High</button></a>");
            client.println("<a href=\"/MEDIUM\"><button>Medium</button></a>");
            client.println("<a href=\"/LOW\"><button>Low</button></a>");
            client.print("<br>");
            client.println("</body>");
            client.println("</html>");
            int sensorVal = analogRead(SOIL_SENSOR_PIN);
            client.print("</p");
            client.println("<p>Soil Humidity value to turn on the Watering System: >");
            client.print(maxVals);
            client.println("</p");
            client.println("<p>Maximum soil humidity:");
            client.print(minVals);
            client.print("<p>Soil Moisture Value: ");
            client.print(sensorVal);
            client.print("</p>");
            client.println("Watering is:");

            if (sensorVal > maxVals) {

              client.println("ON");
              client.print("<p>Soil is too dry, need to refill the water</p>");
              digitalWrite(WATER_PIN, HIGH);  // turn on the relay
            } else if (sensorVal < minVals) {
              client.println("OFF");
              client.print("<p>Soil is too wet</p>");
              digitalWrite(WATER_PIN, LOW);  // turn off the relay
            } else {
              client.println("OFF");
              client.print("<p>Soil is good</p>");
              digitalWrite(WATER_PIN, LOW);
            }
            client.println("<h1>Arduino Light Control</h1>");
            client.print("Light level: ");
            client.print(lightMeter.readLightLevel());
            client.print(" lux<br>");
            client.print("Relay is ");
            if (digitalRead(LIGHT_PIN) == HIGH) {
              client.print("ON");
            } else {
              client.print("OFF");
            }
            String isopoda = client.readStringUntil('\r');
            client.println("<h1>Arduino Servo Control</h1>");
            if (isopoda.indexOf("/FIVE") != -1) {
              servoCount = 5;
            } else if (isopoda.indexOf("/TEN") != -1) {
              servoCount = 10;
            } else if (isopoda.indexOf("/FIFTEEN") != -1) {
              servoCount = 15;
            }
                 
            client.println("<a href=\"/FIVE\"><button>Five</button></a>");
            client.println("<a href=\"/TEN\"><button>Ten</button></a>");
            client.println("<a href=\"/FIFTEEN\"><button>Fifteen</button></a>");
           for (int i = 0; i < servoCount; i++) {
                servo1.write(150);      // tell servo to go to fill position
                delay(1000);                    // delay for 1 second
                servo1.write(70);  // tell servo to go to dispense location
                delay(1000);  
              }
              delay(30000);
           
            break;
          } else if (c == '\n') {
            currentLineIsBlank = true;
          } else if (c != '\r') {
            currentLineIsBlank = false;
          }
        }
      }
      delay(1);
      client.stop();
    }
  }

}

Thanks for any help!

Do you think your Arduino is able to check web request or do anything during all those delays ?

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

For extra information and examples look at

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