Different delays in if-statements

Is it possible to use one delay for an if, and then another for the else? And can this be done without using any external delays?
I have tried to do it in this code, but it ended up printing constantly with "no" delay for 12 hours:

#include <DHT.h>
#include <SoftwareSerial.h>
#include <SDS011.h>
#include <TinyGPS++.h>
#include <SD.h>

// Klargjør for temperatur og fuktighet
#define DHTPIN 9
DHT dht22(DHTPIN, DHT22);

// Klargjør led-lysene rød og grønn
#define LED_RED A1
#define LED_GREEN A0

// Klargjør gps modul
#define GPS_RX 7
#define GPS_TX 6
SoftwareSerial gpsCom(GPS_RX, GPS_TX);
TinyGPSPlus gps;

// Klargjør PM modul
#define PM_TX 2
#define PM_RX 3
SDS011 sds;

#define SD_CS_PIN 10
File file;

int counter = -1;
int counter2 = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(SD_CS_PIN, OUTPUT);

Serial.begin(9600);

SD.begin(SD_CS_PIN);

gpsCom.begin(9600);

sds.begin(PM_TX, PM_RX);

char filename [] = "Airbit-1.txt";

if (SD.exists(filename)) {
  file = SD.open(filename, O_WRITE | O_APPEND);
  file.println("--------------------");
  file.println("Filen ble åpnet på nytt.");
} else {
file = SD.open(filename, O_CREAT | O_WRITE);
file.println("Dette er starten av filen.");
}
file.flush();

}

void loop() {
  // put your main code here, to run repeatedly:
gpsCom.listen();
bool gpsEncodeComplete = false;
do{
  if(!gpsCom.available()) {
    
    continue;
  }
  gpsEncodeComplete = gps.encode(gpsCom.read());
  if (!gpsEncodeComplete){

continue;
  }
}while (!gpsEncodeComplete); {

bool gpsValid = gps.location.isValid();
bool gpsUpdated = gps.location.isValid();
bool isUseful = gpsValid && gpsUpdated;

 
// nullstill temperatur og fuktighet
float temperature = 0;
float humidity = 0;
temperature = dht22.readTemperature();
humidity = dht22.readHumidity();


float pm25, pm10;
int error;
do{
error = sds.read(&pm25, &pm10);
} while (error != 0);
 

if (!isUseful) {
  file.print("NoGPS");
  file.print(", ");
  file.print(counter);
  file.print(", ");

  file.print(temperature);
  file.print(", ");
  file.print(humidity);
  file.print("%");
  file.print(", ");

  file.print(pm25);
  file.print(", ");
  file.print(pm10);
  file.println("   ");

  file.flush();

  delay(2000);
} else if (gps.time.hour() >=5 && gps.time.hour() <=17) {

  file.print(gps.date.day());
  file.print(".");
  file.print(gps.date.month());
  file.print(".");
  file.print(gps.date.year());
  file.print(" ");
  file.print(gps.time.hour());
  file.print(":");
  file.print(gps.time.minute());
  file.print(", ");
  file.print(counter2);
  file.print(", ");

  file.print(temperature);
  file.print(", ");
  file.print(humidity);
  file.print("%");
  file.print(", ");

  file.print(pm25);
  file.print(", ");
  file.print(pm10);
  file.println("   ");

  file.flush();

  counter2 = counter2 + 1;

  delay(900000);
} else {
  file.print("GPSWait");
  file.print(", ");
  file.print(counter);
  file.print(", ");
  
  file.print(temperature);
  file.print(", ");
  file.print(humidity);
  file.print("%");
  file.print(", ");

  file.print(pm25);
  file.print(", ");
  file.print(pm10);
  file.println("   ");

  file.flush();
  delay(900000);
}

counter = counter + 1;

}
}

Your code is horribly formatted! Do you know the IDE Auto Format (Ctrl-T) functionality?

These are two instances of the SoftwareSerial class but you cann thge listen method only on one of them, so the other one will never receive anything.

Of course it is. If it is always doing the if you never execute the else.

Did you try printing the value of the controlling variable? Maybe a surprise there.

a7