air quality sensor not working

hi guys i am a bit confused here with my project, my air quality sensor works in its own sketch but when i put it in the complete sketch it gives a zero reading,

#include <Wire.h>
#include <DS3231.h>
#include <LiquidCrystal_I2C.h>
#include "Adafruit_CCS811.h"
#include "Adafruit_SHT31.h"
Adafruit_CCS811 ccs;
#define EXHAUST 9
int i;
#define HUMIDIFIER 10
#define SETPOINT 95.0
#define DEADBAND 2.0
#define ON false
#define OFF true
const byte RelayPin = 8;
DS3231  rtc(SDA, SCL);
Time t;
LiquidCrystal_I2C lcd(0X27, 2, 1, 0, 4, 5, 6, 7);
int LIGHT = 7;
const int OnHour = 06;
const int OnMin = 01;
const int OffHour = 18;
const int OffMin = 01;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
byte degree[8] =
{
  0b00011,
  0b00011,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
};
void setup() {
  Serial.begin(9600);
  
 
  if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
    Serial.println("Couldn't find SHT31");
    while (1) delay(1000);
  }

  if (!ccs.begin()) {
    Serial.println("Failed to start sensor! Please check your wiring.");
    while (1);
  }
 
  // Wait for the sensor to be ready
  while (!ccs.available());
   while (!Serial);
  rtc.begin();
   pinMode(LIGHT, OUTPUT);
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, LOW);
  lcd.begin(20, 4);
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.setCursor(0, 0);
  lcd.print("Temp");
  lcd.setCursor(17, 0);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity");
  lcd.setCursor(17, 1);
  lcd.print("%");
  lcd.setCursor(0, 2);
  lcd.print("CO2");
  lcd.setCursor(17, 2);
  lcd.print("PPM");
  Serial.println("CCS811 test");
  pinMode(HUMIDIFIER, OUTPUT);
  Serial.println("SHT31 test");
}
void SetRelayOnState(bool newOnState)
{
  static bool currentOnState = true;
  // Only print when the state changes
  if (newOnState == currentOnState)
    return; // Nothing to do
  currentOnState = newOnState;
  if (currentOnState)
  {
    digitalWrite(RelayPin, LOW);
    Serial.println("RELAY ON");
  }
  else
  {
    digitalWrite(RelayPin, HIGH);
    Serial.println("RELAY OFF");
  }
}


void loop() {
  
  if (ccs.available()) {
    if (!ccs.readData()) {
      
      Serial.print("CO2: ");
      Serial.print(ccs.geteCO2());
      Serial.print("ppm, TVOC: ");
      Serial.println(ccs.getTVOC());
      float t = sht31.readTemperature();
      float h = sht31.readHumidity();
      lcd.setCursor(0, 0);
      lcd.setCursor(11, 0);
      lcd.print(t, 2);
      lcd.setCursor(11, 1);
      lcd.print(h, 2);
      lcd.setCursor(11, 2);
      lcd.print(ccs.geteCO2());
      if(ccs.geteCO2()>1000)
      {
        digitalWrite(EXHAUST, ON);
      }
      else
      {
        digitalWrite(EXHAUST, OFF);
      }
      if (digitalRead(HUMIDIFIER) == OFF)
      {
        if (h > SETPOINT + DEADBAND)
        {
          digitalWrite(HUMIDIFIER, ON);

        }
      }
      else
      {
        if (h < SETPOINT - DEADBAND)
          digitalWrite(HUMIDIFIER, OFF);
      }
    }
  } static int currentMinute = -1;
  t = rtc.getTime();
  // Show time at the start of each new minute
  if (t.min != currentMinute)
  {
    currentMinute = t.min;
    Serial.print(t.hour);
    Serial.print(" hour(s), ");
    Serial.print(t.min);
    Serial.println(" minute(s)");
  }
  if (t.min % 30 < 15)
  {
    // For the first half of every half hour, turn on the Relay
    SetRelayOnState(true);
  }
  else
  {
    // For the second half of every half hour, turn off the Relay
    SetRelayOnState(false);
  }
if (t.hour == OnHour && t.min == OnMin)
  { digitalWrite(LIGHT, HIGH);
    Serial.println("LIGHT ON");
  }
  else if (t.hour == OffHour && t.min == OffMin) {
    digitalWrite(LIGHT, LOW);
    Serial.print("LIGHT OFF");
  }
}
if i remove this section it works fine but i need this so what can i do
[codestatic int currentMinute = -1;
  t = rtc.getTime();
  // Show time at the start of each new minute
  if (t.min != currentMinute)
  {
    currentMinute = t.min;
    Serial.print(t.hour);
    Serial.print(" hour(s), ");
    Serial.print(t.min);
    Serial.println(" minute(s)");
  }
  if (t.min % 30 < 15)
  {
    // For the first half of every half hour, turn on the Relay
    SetRelayOnState(true);
  }
  else
  {
    // For the second half of every half hour, turn off the Relay
    SetRelayOnState(false);
  }]

i have narrowed it down to this statement but not sure why t = rtc.getTime();

Do you have some sort of RTC connected, and have you verified that it actually works?

i have aq ds3231 rtc connected and when i run the test sketch it works just fine

you need to show how it is all connected. A hand drawing and photo would be appropriate.

i did manage to find the error, it was in fact a defective rtc causing the fault, i fitted a new one and it worked perfectly

thanks for the help :slight_smile: :slight_smile: :slight_smile: :slight_smile: