Use dht11 and php to do instantaneous control and automatic control of the switc

Use dht11 to do automatic control and timely control
But when I switch from instant control to automatic control will occur when the delay
How can we remove the delay?
This is my code

arduino

#include <dht.h>
#define dht_dpin 8

dht DHT;

int relay1=11; //設定繼電器1為第11孔
int relay2=12; //設定繼電器2為第12孔
int incomingByte; // a variable to read incoming serial data into

int temp,hum;

int mode = 0;

void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:

pinMode(relay1,OUTPUT); //設定繼電器1??出
pinMode(relay2,OUTPUT); //設定繼電器2??出
}

void loop() {

switch(mode){
case 0:{
Serial.println(“0”);
do{doing();
}
while(Serial.available() > 0);
break;
}
case 1:{
Serial.println(“1”);
control();

}
}
}

void control(){
DHT.read11(dht_dpin);
temp=DHT.temperature;
hum=DHT.humidity;
if(temp >= 28&& hum>=60){
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
//Serial.println(“01”);
//Serial.println(temp);
//Serial.println(hum);
delay(1000);
if(Serial.available() > 0){
incomingByte = Serial.read();
if (incomingByte == ‘k’) {

mode = 0;
}
else
mode = 1;

}
}
else if(temp < 28&&hum >= 60){
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
//Serial.println(“02”);
//Serial.println(temp);
//Serial.println(hum);
delay(1000);
if(Serial.available() > 0){
incomingByte = Serial.read();
if (incomingByte == ‘k’) {
mode = 0;
}
else
mode = 1;

}
}
else if(temp >=28&&hum < 60){
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
//Serial.println(“03”);
//Serial.println(temp);
//Serial.println(hum);
delay(1000);
if(Serial.available() > 0){
incomingByte = Serial.read();
if (incomingByte == ‘k’) {
mode = 0;
}
else
mode = 1;
}
}
else if(temp<28&&hum <60){
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
//Serial.println(“04”);
//Serial.println(temp);
//Serial.println(hum);
delay(1000);
if(Serial.available() > 0){
incomingByte = Serial.read();
if (incomingByte == ‘k’) {
mode = 0;
}
else
mode = 1;
}
}
}

void doing(){
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
if (incomingByte == ‘j’) {
mode = 1;
}
if (incomingByte == ‘H’) {
digitalWrite(relay1, HIGH);

}
if (incomingByte == ‘L’) {
digitalWrite(relay1, LOW);

}
if (incomingByte == ‘Q’) {
digitalWrite(relay2, HIGH);

}
if (incomingByte == ‘N’) {
digitalWrite(relay2, LOW);

}
}
}

php code

<?php function openSerial($command) { $openSerialOK = false; try { exec("mode com4: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off"); $fp =fopen("com5", "w"); //$fp = fopen('/dev/ttyUSB0','r+'); //use this for Linux $openSerialOK = true; } catch(Exception $e) { echo 'Message: ' .$e->getMessage(); } if($openSerialOK) { fwrite($fp, $command); //write string to serial fclose($fp); } } openSerial("Without this line, the first control will not work. I don't know way."); if(isset($_POST['submit1'])) { openSerial("H"); } if(isset($_POST['submit2'])) { openSerial("L"); } if(isset($_POST['submit3'])) { openSerial("Q"); } if(isset($_POST['submit4'])) { openSerial("N"); } if(isset($_POST['submit5'])) { openSerial("k"); } if(isset($_POST['submit6'])) { openSerial("j"); } ?>

while(Serial.available() > 0);

This will hang if there is something in the buffer.