If trigger statement not working on Uno board

I cobbled together some different code for a DHT11 sensor and a CO2meter.com k-22 (Thanks to Andrew at CO2 sensor who wrote the I2C code.)sensor. I put in an If statement, which turns co2 on at 1300, and off at 1500. I used this same code reading the DHT11 sensor to turn fans on for temp or humidity and that works fine… but for some reason, pin 13 LED is just stays dimly lit and reads 1.4V, instead of 5V. The only part that doesn’t work is,

// CO2 Control
if (co2Value < co2ON) // Set CO2 to turn ON
{
digitalWrite(co2Pin, HIGH);
}
else if (co2Value > co2OFF) // Set CO2 to turn OFF
{
digitalWrite(co2Pin, LOW);
}

The rest of the code including the temp/humidity triggers work fine. Any help/hints would be awesome!

#include <Wire.h>
#include <dht11.h>
#define DHT11PIN 2 // Data Pin on Arduino
dht11 DHT11;

//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}

int co2Addr = 0x68;
int dehumidPin = 11;
int aircoolerPin = 12;
int co2Pin = 13;
int co2ON = 1300; // Set the low co2 PPM trigger
int co2OFF = 1500; // Set the high co2 PPM trigger
int rhHIGH = 50; // Set the high humidity trigger (when to turn dehumidifier ON)
int rhLOW = 40; // Set the low humidity trigger (When to turn dehumidifier OFF)
int tHIGH = 86; // Set the high temperature trigger (When to turn cooler ON)
int tLOW = 83; // Set the low temperature trigger (When to turn cooler OFF)

void setup() {
Serial.begin(9600);
Wire.begin ();
pinMode(aircoolerPin, OUTPUT);
pinMode(dehumidPin, OUTPUT);
Serial.println(“Enivronmental Control Unit”);
}
int readCO2()
{
int co2_value = 0;

Wire.beginTransmission(co2Addr);
Wire.send(0x22);
Wire.send(0x00);
Wire.send(0x08);
Wire.send(0x2A);
Wire.endTransmission();

delay(10);

Wire.requestFrom(co2Addr, 4);
byte i = 0;
byte buffer[4] = {
0, 0, 0, 0 };

while(Wire.available())
{
buffer = Wire.receive();

  • i++;*
  • }*
  • co2_value = 0;*
  • co2_value |= buffer[1] & 0xFF;*
  • co2_value = co2_value << 8;*
  • co2_value |= buffer[2] & 0xFF;*
  • byte sum = 0; //Checksum Byte*
  • sum = buffer[0] + buffer[1] + buffer[2]; //Byte addition utilizes overflow*
  • if(sum == buffer[3])*
  • {*
  • return co2_value;*
  • }*
  • else*
  • {*
  • return 0;*
  • }*
    }
    void loop() {
  • int co2Value = readCO2();*
  • if(co2Value > 0)*
  • {*
  • Serial.print("CO2 Value: ");*
  • Serial.print(co2Value);*
  • }*
  • else*
  • {*
  • Serial.print(“Checksum/Com Fail”);*
  • }*
  • {*
    // The following is for DHT11 only
  • int chk = DHT11.read(DHT11PIN);*
  • Serial.print(" DHT11: ");*
  • switch (chk)*
  • {*
  • case 0:*
  • Serial.print(“OK”);*
  • break;*
  • case -1:*
  • Serial.print(“Checksum error”);*
  • break;*
  • case -2:*
  • Serial.print(“Time out error”);*
  • break;*
  • default:*
  • Serial.print(“Unknown error”);*
  • break;*
  • }*
  • Serial.print(" Humidity (%): ");*
  • Serial.print((float)DHT11.humidity, 1); *
  • Serial.print(" Temperature (F): ");*
  • Serial.println(Fahrenheit(DHT11.temperature), 1);*
    // The following is for setting Environmental Triggers
  • // Temperature Control*
  • if (Fahrenheit(DHT11.temperature) > tHIGH)*
  • {*
  • digitalWrite(aircoolerPin, HIGH);*
  • }*
  • else if (Fahrenheit(DHT11.temperature) <= tLOW)*
  • {*
  • digitalWrite(aircoolerPin, LOW);*
  • }*
  • // Humidity Control *
  • if (DHT11.humidity > rhHIGH)*
  • {*
  • digitalWrite(dehumidPin, HIGH);*
  • }*
  • else if (DHT11.humidity <= rhLOW)*
  • {*
  • digitalWrite(dehumidPin, LOW);*
  • }*
  • // CO2 Control*
  • if (co2Value < co2ON) // Set CO2 to turn ON*
  • {*
  • digitalWrite(co2Pin, HIGH);*
  • }*
  • else if (co2Value > co2OFF) // Set CO2 to turn OFF*
  • {*
  • digitalWrite(co2Pin, LOW);*
  • }*
  • delay(2000);*
  • }*
    }
    [/quote]

Just a guess: you might want a pinMode(co2Pin, OUTPUT) in your startup.

LOL! I guess sometimes you just need a fresh pair of eyes :slight_smile:

thanks