Noob having issues with triggering IF statments

Hi Guys and Gals,

new to the forum so hello everyone whom reads it,

I have been been having issues with getting relays to trigger from a sensor value on I2C,
I have been looking all over the net for a few days now but no progress,

i have a simple monitor system that works fine and spools the data to serial monitor,
but i can not trigger the other relays from a "set point" value,

i know i have messed the script up but i cant see where.

consists of
Uno,
CCS811 CO2 sensor,
BME280 temp humdity sensor
4 channel "Arduino type" relay

#include <SparkFunBME280.h>
#include <SparkFunCCS811.h>

//Resources:

//Uses Wire.h for i2c operation
//The BME280 and the CCS811 are both on the I2C bus, in parallel
//use the hardware pins for clock (SCl) and data (SDA)

// Hardware Connections (Breakoutboard to Arduino)
//3.3V to 3.3V pin
//GND to GND pin
//SDA to A4
//SCL to A5

#define BME280_ADDR 0x76 //Default I2C Address
#define CCS811_ADDR 0x5A //Alternate I2C Address

// Define relay Pin and names

#define Fan 2
#define Trickle 4
#define Purge 7
#define Humidity 8

//Global objects for sensors
CCS811 myCCS811(CCS811_ADDR);
BME280 myBME280;

int time=0;

void setup()
{
Serial.begin(9600);

pinMode (Fan, OUTPUT);
pinMode (Trickle, OUTPUT);
pinMode (Purge, OUTPUT);
pinMode (Humidity, OUTPUT);

Serial.println("Please Allow 20 Mins For The CO2 & TVOC Sensor To Self Calibrate.");

Serial.println();

//This begins the CCS811 sensor and prints error status of .begin()
myCCS811.begin();

//For I2C, enable the following and disable the SPI section
myBME280.settings.commInterface = I2C_MODE;
myBME280.settings.I2CAddress = 0x76;

//Initialize BME280
//For I2C, enable the following and disable the SPI section
myBME280.settings.commInterface = I2C_MODE;
myBME280.settings.I2CAddress = 0x76;
myBME280.settings.runMode = 3; //Normal mode
myBME280.settings.tStandby = 0;
myBME280.settings.filter = 4;
myBME280.settings.tempOverSample = 5;
myBME280.settings.pressOverSample = 5;
myBME280.settings.humidOverSample = 5;

delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.

//Calling .begin() causes the settings to be loaded
myBME280.begin();

}
//---------------------------------------------------------------
void loop()
{
digitalWrite(Fan, LOW); // NC terminals
digitalWrite(Trickle, HIGH);
digitalWrite(Purge, HIGH);
digitalWrite(Humidity, HIGH); // Humidifier

//Check to see if data is available
if (myCCS811.dataAvailable())
{
//Calling this function updates the global tVOC and eCO2 variables
myCCS811.readAlgorithmResults();
//printInfoSerial fetches the values of tVOC and eCO2
//printInfoSerial();
if(time%20==0) Serial.println(" ");

const int x=8;
int y=12;
const int delty=21;

float BMEtempC = myBME280.readTempC();
float BMEhumid = myBME280.readFloatHumidity();

//This sends the temperature data to the CCS811
myCCS811.setEnvironmentalData(BMEhumid, BMEtempC);
}
else if (myCCS811.checkForStatusError())
{

delay(2000); //Wait for next reading
time+=2;
}

//getCO2() gets the previously read data from the library

Serial.println("Reading Sensors");
delay (1500);
Serial.print(" CO2: ");
Serial.print(myCCS811.getCO2());
Serial.println(" ppm");
delay(1500);

//getTVOC() gets the previously read data from the library
Serial.print(" TVOC: ");
Serial.print(myCCS811.getTVOC());
Serial.println(" ppb");
delay(1500);

Serial.print(" Temp: ");
Serial.print(myBME280.readTempC(), 2);
Serial.println(" C");
delay(1500);

Serial.print(" Humidity: ");
Serial.print(myBME280.readFloatHumidity(), 2);
Serial.println(" %");
delay(1500);

Serial.println();
//// this is where I'm stuck ////

if (myBME280.readTempC() > 18)
{
digitalWrite(Trickle, LOW);
}

if (myCCS811.getCO2() < 1200);
digitalWrite(Purge, LOW);

}

Hi jungle_jeffrey,

welcome to the arduino-forum.
As you are a newbee I want to give some advice.

If you post code use the </> -button

which wil create a "[ code ] [ / code ]" paste your code between the "]" and the "[" in the middle.

OK you are a noob but IMHO at least you should be able to look through your code for opening brackets and closing brackets.

I mean these "curly" ones "{" and "}" each block of code that belongs to a while-loop a for-loop or an if-statement s embraced by these "{" and "}" brackets to make the compiler understand execute these lines of code only when ....

example

  if (myBME280.readTempC() > 18)
  {
      digitalWrite(Trickle, LOW);
  }

me personal I have the habit to put the opening bracket "{" one line below the if-statement at the same columm as the "i" of the word "if"

and the closing bracket "}" in the same columm.
If you place the cursor directly beneath the bracket (left or right) the Arduino-IDE will paint a box around the corresponding bracket. So you can easily see where ist the closing bracket to the opening bracket or vice versa where ist the opening bracket to the closing bracket.

I'm pretty sure you will find it.

best regards

Stefan

Lose the semi-colon:

 if (myCCS811.getCO2() < 1200);

Thank you guys,

I see what you mean about the curly brackets,
i have changed it to only trigger the "trickle" relay but still no joy,

  if (myBME280.readTempC() > 18)
  {
      digitalWrite(Trickle, LOW);
  }
}

so i closed the IF statement then closed the script

when i remove the ; it displays a token error

Post your new (broken) code.

i just do not see it,

if (myBME280.readTempC() > 18)
  {
      digitalWrite(Trickle, LOW);
  }
}

what am I missing .readTempC more than 18 = trickle set to low

but every time it just seems to serial print the values but not act/read the If statement above :confused:

Broken code

#include <SparkFunBME280.h>
#include <SparkFunCCS811.h>




         //Resources:
         
//Uses Wire.h for i2c operation
//The BME280 and the CCS811 are both on the I2C bus, in parallel
//use the hardware pins for clock (SCl) and data (SDA)

//  Hardware Connections (Breakoutboard to Arduino)
//3.3V to 3.3V pin 
//GND to GND pin
//SDA to A4
//SCL to A5

#define BME280_ADDR 0x76 //Default I2C Address
#define CCS811_ADDR 0x5A //Alternate I2C Address

// Define relay Pin and names

#define Fan 2
#define Trickle 4
#define Purge 7
#define Humidity 8


//Global objects for sensors
CCS811 myCCS811(CCS811_ADDR);
BME280 myBME280;

int time=0;

void setup()
{
  Serial.begin(9600);
  
  pinMode (Fan, OUTPUT);
  pinMode (Trickle, OUTPUT);
  pinMode (Purge, OUTPUT);
  pinMode (Humidity, OUTPUT);
  

  Serial.println("Please Allow 20 Mins For The CO2 & TVOC Sensor To Self Calibrate.");

    Serial.println();

  //This begins the CCS811 sensor and prints error status of .begin()
  myCCS811.begin();
 
  

  //For I2C, enable the following and disable the SPI section
  myBME280.settings.commInterface = I2C_MODE;
  myBME280.settings.I2CAddress = 0x76;

  //Initialize BME280
  //For I2C, enable the following and disable the SPI section
  myBME280.settings.commInterface = I2C_MODE;
  myBME280.settings.I2CAddress = 0x76;
  myBME280.settings.runMode = 3; //Normal mode
  myBME280.settings.tStandby = 0;
  myBME280.settings.filter = 4;
  myBME280.settings.tempOverSample = 5;
  myBME280.settings.pressOverSample = 5;
  myBME280.settings.humidOverSample = 5;

  delay(10);  //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.

  //Calling .begin() causes the settings to be loaded
  myBME280.begin();


}
//---------------------------------------------------------------
void loop()
{
  digitalWrite(Fan, LOW); // NC terminals
  digitalWrite(Trickle, HIGH);
  digitalWrite(Purge, HIGH);
  digitalWrite(Humidity, HIGH); // Humidifier
 
  
  //Check to see if data is available
  if (myCCS811.dataAvailable())
  {
    //Calling this function updates the global tVOC and eCO2 variables
    myCCS811.readAlgorithmResults();
    //printInfoSerial fetches the values of tVOC and eCO2
    //printInfoSerial();
    if(time%20==0) Serial.println(" ");



    const int x=8;
    int y=12;
    const int delty=21;
    
    float BMEtempC = myBME280.readTempC();
    float BMEhumid = myBME280.readFloatHumidity();


    //This sends the temperature data to the CCS811
    myCCS811.setEnvironmentalData(BMEhumid, BMEtempC);
  }
  else if (myCCS811.checkForStatusError())
  {


  delay(2000); //Wait for next reading
  time+=2;
}

  //getCO2() gets the previously read data from the library

  Serial.println("Reading Sensors");
  delay (1500);
  Serial.print(" CO2:  ");
  Serial.print(myCCS811.getCO2());
  Serial.println(" ppm");
  delay(1500);

  //getTVOC() gets the previously read data from the library
  Serial.print(" TVOC: ");
  Serial.print(myCCS811.getTVOC());
  Serial.println(" ppb");
  delay(1500);


  Serial.print(" Temp: ");
  Serial.print(myBME280.readTempC(), 2);
  Serial.println(" C");
  delay(1500);

  Serial.print(" Humidity: ");
  Serial.print(myBME280.readFloatHumidity(), 2);
  Serial.println(" %");
  delay(1500);
  
  Serial.println();


  if (myBME280.readTempC() > 18)
  {
      digitalWrite(Trickle, LOW);
  }
}

I assume that the temperature is actually greater than 18?

Put a serial print inside the if to see whether execution goes there - it may simply be that it is working but you have a wiring issue.

i edited it to

 if (myBME280.readTempC() > 18)
  {
  Serial.print(myBME280.readTempC(), 2);
  Serial.println(" C");
  delay(1500);
      digitalWrite(Trickle, LOW);
  }
}

the serial monitor screen print attached

serial monitor output co2.PNG

It's working I think. Trouble is, microseconds after you set trickle LOW, loop runs again and you set it HIGH.

Thank you bill,

yes the timing is too fast for LED's let alone relays,

i commented out my "set relays 2-4 to off "

void loop()
{
  digitalWrite(Fan, LOW); // NC terminals
  //digitalWrite(Trickle, HIGH);
 // digitalWrite(Purge, HIGH);
 // digitalWrite(Humidity, HIGH); // Humidifier

it works and triggers the relay,
thank you so much i can carry on building the if statements for the other relays,