I want to know what is wrong with this code it keeps getting results late

I modified a code to suit my need. I only introduced Servo motor control logic to the code
My objective is to make Servo go from 0~180 and read data from CO2 sensor 400 times and make Servo to go 180~0.
Before I modified the code, it worked well (without Servo motor)

#include <SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial mySerial(10,11);
byte sensorData[8]={}; 
double startTime = millis();
double currentTime = startTime
int timeOver = 0 ;
Servo first;
int angle;
int index;

void send2Sensor(byte commandCode){ //function requesting Co2 values to sensor
  byte aa=0x11;                        //data request value 
  byte bb=0x01;                        //
  byte cc= commandCode;                //command code 
  byte dd= (-(aa+bb+cc));              //error detection code 
  
 mySerial.write(aa);                  // requesting Co2 sensor to give co2 values

 mySerial.write(bb);
 mySerial.write(cc);
 mySerial.write(dd);  
}

void serialPrintSensorData(){                  // displaying data in serial monitor 
       
   int co2Value = 0; 
       co2Value =  (( sensorData[2] * 256 ) + ( sensorData[3]) ); 
       if ( co2Value >0 ) {

      //Serial.print( " Sampling Time = " );
      Serial.print(currentTime); Serial.print(" ");
     // Serial.println(" second        ");
       
     // Serial.print( " CO2 = " );
      Serial.print(co2Value);
     // Serial.println(" ppm         ");
      Serial.println();  
       }
}

void recieveSensorData(){
    mySerial.readBytesUntil(0x16, sensorData, 7);
}

void co400times(){
  
currentTime = (millis() - startTime) / 1000 ;

send2Sensor(0x01);            //request Co2 values
recieveSensorData();       // recieve sensor values 
serialPrintSensorData();   // print in serial monitor

}
/////////////////// this is the servo motor control logic ///////////
void Servo_Control(int index){
if (index==0){
for (angle=0; angle<=180; angle++){
  first.write(angle);
  delay (15);
}
}
else { 
for (angle=180; angle>=0; angle--){
 first.write(angle);
  delay (15);
}
}}
//////////////////////////////////////////////////
void setup() {
 Serial.begin(9600);
 mySerial.begin(9600); 
 first.attach(2);
}


void loop() {

 if (timeOver==0)
Servo_Control(0);//at first, turn the Servo to 180

  co400times();
 timeOver++; 
 if ( timeOver == 400 ) { timeOver=0; //if the values were printed 400 times
                                     //I close servo to 0 and wait for 30 second
                                     //and go to the starting point of the loop
 Servo_Control(1); delay (30000);
 startTime= millis();
 }
 
 delay(5000);// sensor values will be printed 5 seconds each 
 
}

this is the original code that worked properly without Servo motor

//===================================================

// CM1101 CO2 SENSOR TEST

//   BY KIM IN KEUN ( 2016. 05.2 )

//===================================================

#include <SoftwareSerial.h>




SoftwareSerial mySerial(10,11);

byte sensorData[8]={}; 

double startTime = millis();

double currentTime = startTime;

boolean timeOver = 0 ;




void send2Sensor(byte commandCode){    //센서에 CO2 측정값을 요청하는 함수

  byte aa=0x11;                        //데이타요청 전치값

  byte bb=0x01;                        //이후 전송할 바이트수 

  byte cc= commandCode;                //명령코드

  byte dd= (-(aa+bb+cc));              //오류 검출코드

  

 mySerial.write(aa);                  // CO2 센서에 측정값 요청명령 전송

 mySerial.write(bb);

 mySerial.write(cc);

 mySerial.write(dd);  

}




void serialPrintSensorData(){                  // 배열변수에 저장된 데이타 시리얼 모니터에 표시하기

       

   int co2Value = 0; 

       co2Value =  (( sensorData[2] * 256 ) + ( sensorData[3]) ); 

       if ( co2Value >0 ) {




      Serial.print( " Sampling Time = " );

      Serial.print(currentTime);

      Serial.println(" second        ");

       

      Serial.print( " CO2 = " );

      Serial.print(co2Value);

      Serial.println(" ppm         ");

      Serial.println();  

       }

}




void recieveSensorData(){

    mySerial.readBytesUntil(0x16, sensorData, 7);

}




void co400times(){

  

timeOver = 0;    

currentTime = (millis() - startTime) / 1000 ;

if ( currentTime < 40 ) { 

send2Sensor(0x01);            // CO2 측정값 요청

recieveSensorData();       // 센서에서 측정값을 받음 

serialPrintSensorData();   // 배열변수에 저장된 측정값을 시리얼 모니터에 출력함

}

   timeOver = 1 ;

}




void setup() {

 Serial.begin(9600);

 mySerial.begin(9600); 

}







void loop() {




  // for Servo  

  co400times();

 // if ( timeOver == 1 ) {  for Servo }

  currentTime =0;

  delay(3000);




}

The results comes out after around 20 seconds and for some little time it prints 5seconds each then stops and I have to wait for 20~40 seconds for a response.

Maybe the delay(30000) has something to with it?

...R

I don't think so, the sensor values that I read from serial monitor stops for about 9~10 seconds and reads again and C02 level is not correct response time is 60 seconds so I must get 400ppm after 60 seconds but I don't get it with this code

Your formatting is truly atrocious. And this doesn't help:

 delay(5000);// sensor values will be printed 5 seconds each

Try using 'unsigned long' for your time recording instead of 'double', too.

KwonYS: I don't think so,

You need to move from "thinking" to "knowing" - personally I would remove the delay() and see what happens. That is a quick and simple test.

When you "know" you can (if necessary) then focus your attention on other potential causes.

...R