Guarduino Sketch. Help needed please.

Hello to all and thank you inadvance for the help you may bring to my project. I am new in the Arduino world and thank you fir your patience as well.

I am putting together a guarduino automated guardening system and have problems with the sketch.

My project consists on the following:

-light sensor
-DHT 22 (temp and humidity sensor)
-CO2 sensor
-Soil humidity probes
-LCD screen. 2x16: LCD Keypad Shield for Arduino Duemilanove LCD1602
-Relays:
-1 relay for the CO2 injection. MG-811 sensor module.
CO2 Parameters. ON Only when lights are on (Light value > 800) and the PPM is below or equal to 1250PPM. Maintain within these parameter.
If PPM greater than 2250 PPM shut off CO2 and turn on evacuation hood and turn off CO2.
-1 relay for evacation hood constant ON
-1relay for evacuation hood constant Off.

Optionally once these issues are solved; I would like to add the RTC DS3231 module and be able to use the push buttons on the LCD shield in order to change the values and parameters.

Here is below the sketch I put together but does not function properly.

I grealty appreciate anyone's help in this matter and thanl you for your inputs.

#include <DateTime.h>
#include "DHT.h"
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
/*******************Demo for MG-811 Gas Sensor Module V1.1*****************************
Author:  Tiequan Shao: tiequan.shao@sandboxelectronics.com
        Peng Wei:     peng.wei@sandboxelectronics.com
         
Lisence: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)

Note:    This piece of source code is supposed to be used as a demostration ONLY. More
        sophisticated calibration is required for industrial field application. 
         
                                                   Sandbox Electronics    2012-05-31
************************************************************************************/

/************************Hardware Related Macros************************************/
#define         MG_PIN                       (5)     //define which analog input channel you are going to use
#define         BOOL_PIN                     (6)
#define         DC_GAIN                      (8.5)   //define the DC gain of amplifier
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302) 
/***********************Software Related Macros************************************/
#define         READ_SAMPLE_INTERVAL         (50)    //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_TIMES            (5)     //define the time interval(in milisecond) between each samples in 
                                                    //normal operation

/**********************Application Related Macros**********************************/
//These two values differ from sensor to sensor. user should derermine this value.
#define         ZERO_POINT_VOLTAGE           (0.368) //define the output of the sensor in volts when the concentration of CO2 is 400PPM
#define         REACTION_VOLTGAE             (0.0) //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2

/*****************************Globals***********************************************/
float           CO2Curve[3]  =  {2.602,ZERO_POINT_VOLTAGE,(REACTION_VOLTGAE/(2.602-3))};   
                                                    //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is
                                                    //"approximately equivalent" to the original curve.
                                                    //data format:{ x, y, slope}; point1: (lg400, 0.324), point2: (lg4000, 0.280) 
                                                    //slope = ( reaction voltage ) / (log400 –log1000) 
//define analog inputs to which we have connected our sensors
int moistureSensor1 = 0;
int moistureSensor2 = 1;
int moistureSensor3 = 2;
int moistureSensor4 = 3;
int lightSensor = 4;
int dht22 = 2;


//define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
//int waterPump = 24;
//int lightSwitch = 26;
//int tempLed = 28;
int evacuationconstON = 3;
int CO2 = 5;
int evacuationconstOFF = 6;

//define variables to store moisture, light, and temperature values
int moisture_val1;
int moisture_val2;
int moisture_val3;
int moisture_val4;
int light_val;
int temp_val;
int humidity_val;
int CO2_val;

////decide how many hours of light your plants should get daily
//float hours_light_daily_desired = 14;

//calculate desired hours of light total and supplemental daily based on above values
//float proportion_to_light = hours_light_daily_desired / 24;
//float seconds_light = 0;
//float proportion_lit;

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
 lcd.begin(16,2);
{
   Serial.begin(9600);                              //UART setup, baudrate = 9600bps
   pinMode(BOOL_PIN, INPUT);                        //set pin to input
   digitalWrite(BOOL_PIN, HIGH);                    //turn on pullup resistors
  Serial.print(" Hello Victor and welcome to your own personalized garden control system ");               
Serial.println(" DHT22 ");

dht.begin();

//set the water, light, and temperature pins as outputs that are turned off
//pinMode (waterPump, OUTPUT);
//pinMode (lightSwitch, OUTPUT);
//pinMode (tempLed, OUTPUT);
pinMode (dht22, OUTPUT);
pinMode (CO2, OUTPUT);
pinMode (evacuationconstON, OUTPUT);
pinMode (evacuationconstOFF, OUTPUT);
//digitalWrite (waterPump, LOW);
//digitalWrite (lightSwitch, LOW);
//digitalWrite (tempLed, LOW);
digitalWrite (dht22, LOW);
digitalWrite (CO2, HIGH);
digitalWrite (evacuationconstON, LOW);
digitalWrite (evacuationconstOFF, LOW);


//establish start time
start_time = DateTime.now();
seconds_elapsed_total = 0;

}
}
void loop()

  {
   int percentage;
   float volts;
    
   
   volts = MGRead(MG_PIN);
   Serial.print( "SEN-00007:" );
   Serial.print(volts); 
   Serial.print( "V           " );
 
   percentage = MGGetPercentage(volts,CO2Curve);
   Serial.print("CO2:       ");
   lcd.setCursor(0,1);
   lcd.print("CO2:       ");
       if (percentage == -1) {
       Serial.print( "<400" );
       lcd.print("<400");
           } else {
       Serial.print(percentage);
       lcd.print(percentage);
      
         {
  
     
 

 }
   }
 
   Serial.print( "PPM" );
   lcd.print( " PPM");    
   Serial.print( "       Time point:" );
   Serial.print(millis());     
   Serial.print("\n");       
    
   if (digitalRead(BOOL_PIN) ){
       Serial.println( "=====BOOL is HIGH======" );
   } else {
       Serial.println( "=====BOOL is LOW======" );
   }
           if (light_val>800 && CO2_val<1250  )
           {// Condition change!!!!!!!!!!!!!!!!if CO2 level is below 1250ppm and light is ON or light is high
     digitalWrite(CO2, LOW);// turn on CO2 pump.
     Serial.println(" CO2 turned ON ");//  write to screen that the CO2 pump is ON
     lcd.setCursor(0,0);
     lcd.print(" CO2 turned ON ");
          digitalWrite(evacuationconstOFF, HIGH);//Turn OFF Extraction.!!!!!!!!!!!!!!!!!!
     Serial.println(" Extraction1 OFF ");// Write to creen that the pump is off. 
     lcd.setCursor(0,1);
     lcd.print(" Extraction1 OFF ");
     digitalWrite(evacuationconstON, LOW);//Turn OFF the evacuation hood Constant ON.!!!!!!!!!!!!!!!!!!
     Serial.println(" Extraction2 OFF ");// Write to creen that the pump is off.   
     lcd.setCursor(0,0);
     lcd.print(" Extraction2 OFF ");
      
}   
else
        {
           digitalWrite(CO2, HIGH);// ELSE STATEMENT. In all other conditions turn OFF the CO2.
     Serial.println(" CO2 turned OFF ");// Write to screen: CO2 is turned OFF
     lcd.setCursor(0,0);
     lcd.print(" CO2 turned OFF ");
     Serial.println(" Extraction1 OFF "); //Write to screen: Extraction is OFF
     lcd.setCursor(0,1);
     lcd.print(" Extraction1 OFF ");
 delay(3000);
     
        }
           {
        delay (1000);// wait for one second.
      }
      
              if (CO2_val >= 2250){// Condition change!!!!!!!!!!!!!!!! if CO2 level is above 2250ppm.
     digitalWrite(CO2, HIGH);// Condition change!!!!!!!!!!!!!!!!turn off CO2 pump.      
     Serial.println(" CO2 LEVEL CRITICAL, CO2 turned OFF ");// Condition change!!!!!!!!!!!!!!!!if CO2 level is above 2250ppm write to screen that the CO2 pump has been turned off.
     lcd.setCursor(0,0);
     lcd.print(" CO2 CRITICAL, CO2 turned OFF ");
     lcd.setCursor(0,1);
     lcd.print(" CO2 turned OFF ");
     delay(3000);
     digitalWrite(evacuationconstOFF, LOW);//Turn on the evacuation hood.!!!!!!!!!!!!!!!!!!
     Serial.println(" Extraction1 ON ");// Write on the screen Extraction Const OFF is ON.
     digitalWrite(evacuationconstON, HIGH);//Turn OFF the evacuation hood Constant ON.!!!!!!!!!!!!!!!!!!
     Serial.println(" Extraction2 ON ");// Write to creen Extraction is ON!!!!!
   }

 while (percentage >= 2250) 

    
  
    
{
 // Wait a few seconds between measurements.  
 // DHT22 Section.  Check to see if all works correctly with other probes and relays!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 delay(2000);

 // Reading temperature or humidity takes about 250 milliseconds!
 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
 float h = dht.readHumidity();
 // Read temperature as Celsius
 float t = dht.readTemperature();
 // Read temperature as Fahrenheit
 float f = dht.readTemperature(true);
 
 if (t >= 36 ){// If Temp is above or equal to 36 degrees.
  digitalWrite(evacuationconstOFF, HIGH);// tun ON the evacuation hood Normally OFF.
  Serial.println( " Evacuation ON ");// print Hood ON.
  }
  else// otherwise.
  {
    digitalWrite(evacuationconstOFF, LOW); //turn OFF the Hood.
  }
 
 // Check if any reads failed and exit early (to try again).
 if (isnan(h) || isnan(t) || isnan(f)) {
   Serial.println("Failed to read from DHT sensor!");
   return;
  

  

{
digitalWrite(evacuationconstOFF, HIGH);//turn on the evacuation Hood.
Serial.println(" Temperature Alert, Air extraction ON ");
}
 }

 // Compute heat index 
 // Must send in temp Celcius
 float hi = dht.computeHeatIndex(f, h);

 Serial.print("Humidity: "); 
 Serial.print(h);
 Serial.print(" %\t");
 
   lcd.setCursor(0,0);
    lcd.print("Humidity: "); 
 lcd.print(" %\t");
 delay(1000);
 
 Serial.print("Temperature: "); 
 Serial.print(t);
 Serial.print(" *C ");
 Serial.println(" ");
{
  lcd.setCursor(0,1);
 lcd.print("Temperature: "); 
 lcd.print(t);
 lcd.print(" *C ");
 lcd.println(" ");
 delay(1000);
}
}   

// read the value from the moisture-sensing probes, print it to screen, and wait a second
moisture_val1 = analogRead(moistureSensor1);
Serial.print(" moisture sensor 1 reads ");
Serial.println( moisture_val1 );
{
 lcd.setCursor(0,0);
 lcd.print("Moisture 1  ");
   lcd.print( moisture_val1 );
 delay(1000);
}
moisture_val2 = analogRead(moistureSensor2);
Serial.print(" moisture sensor 2 reads ");
Serial.println( moisture_val2 );
{
 lcd.setCursor(0,1);
 lcd.print("Moisture 2   ");
   lcd.print( moisture_val2 );
 delay(1000);
}
moisture_val3 = analogRead(moistureSensor3);
Serial.print(" moisture sensor 3 reads ");
Serial.println( moisture_val3 );
{
 lcd.setCursor(0,0);
 lcd.print("Moisture 3   ");
   lcd.print( moisture_val3 );
 delay(1000);
}
moisture_val4 = analogRead(moistureSensor4);
Serial.print("moisture sensor 4 reads ");
Serial.println(moisture_val4 );
{
 lcd.setCursor(0,1);
 lcd.print("Moisture 4   ");
   lcd.print( moisture_val4 );
 delay(1000);
}
{
// read the value from the photosensor, print it to screen, and wait a second
light_val = analogRead(lightSensor);
Serial.print("light sensor reads ");
Serial.println( light_val );
  {
 lcd.setCursor(0,0);
 lcd.print("Light Sensor ");
   lcd.print( light_val );
 delay(1000);
}

// read the value from the temperature sensor, print it to screen, and wait a second
//temp_val = analogRead(tempSensor);
//Serial.print("temp sensor reads ");
//Serial.println( temp_val );
//   {
//  lcd.setCursor(0,1);
//  lcd.print("Temperature ");
//    lcd.print( temp_val );
//  delay(1000);
//}
// 
//Serial.print("seconds total = ");
//Serial.println( seconds_elapsed_total );
//Serial.print("seconds lit = ");
//Serial.println( seconds_light);
//Serial.print("proportion desired = ");
//Serial.println( proportion_to_light);
//Serial.print("proportion achieved = ");
//Serial.println( proportion_lit);

//turn water on when soil is dry, and delay until soil is wet
//if (moisture_val < 850)
{
//digitalWrite(waterPump, HIGH);
}

//while (moisture_val < 850)
{

}

//digitalWrite(waterPump, LOW);

//update time, and increment seconds_light if the lights are on
//seconds_for_this_cycle = DateTime.now() - seconds_elapsed_total;
//seconds_elapsed_total = DateTime.now() - start_time;
//if (light_val > 900)
//{
//seconds_light = seconds_light + seconds_for_this_cycle;
//}

//cloudy days that get sunny again: turn lights back off if light_val exceeds 900. this works b/c the supplemental lights aren't as bright as the sun:)
//if (light_val > 900)
//{
////digitalWrite (lightSwitch, LOW);
//}
//
////turn off lights if proportion_lit>proportion_to_light, and then wait 5 minutes
//if (proportion_lit > proportion_to_light)
//{
////digitalWrite (lightSwitch, LOW);
//delay (300000);
//}

//figure out what proportion of time lights have been on
//proportion_lit = seconds_light/seconds_elapsed_total;
//
////turn lights on if light_val is less than 900 and plants have light for less than desired proportion of time, then wait 10 seconds
//if (light_val < 900 and proportion_lit < proportion_to_light)
//{
////digitalWrite(lightSwitch, HIGH);
//delay(10000);
//}
//
////turn on temp alarm light if temp_val is less than 850 (approximately 50 degrees Fahrenheit)
//if (temp_val < 850)
{
//digitalWrite(tempLed, HIGH);
}


}

  }
/*****************************  MGRead *********************************************
Input:   mg_pin - analog channel
Output:  output of SEN-000007
Remarks: This function reads the output of SEN-000007
************************************************************************************/
float MGRead(int mg_pin)
{
   int i;
   float v=0;

   for (i=0;i<READ_SAMPLE_TIMES;i++) {
       v += analogRead(mg_pin);
       delay(READ_SAMPLE_INTERVAL);
   }
   v = (v/READ_SAMPLE_TIMES) *5/1024 ;
   return v;  
}

/*****************************  MQGetPercentage **********************************
Input:   volts   - SEN-000007 output measured in volts
        pcurve  - pointer to the curve of the target gas
Output:  ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm) 
        of the line could be derived if y(MG-811 output) is provided. As it is a 
        logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic 
        value.
************************************************************************************/
int  MGGetPercentage(float volts, float *pcurve)
{
  if ((volts/DC_GAIN )>=ZERO_POINT_VOLTAGE) {
     return -1;
  } else { 
     return pow(10, ((volts/DC_GAIN)-pcurve[1])/pcurve[2]+pcurve[0]);
  }
}

Please take off the scrolling feature!

Hello to all and thank you inadvance for the help you may bring to my project. I am new in the Arduino world and thank you for your patience as well.

I am putting together a guarduino automated guardening system and have problems with the sketch.

My project consists on the following:

-light sensor
-DHT 22 (temp and humidity sensor)
-CO2 sensor
-Soil humidity probes
-LCD screen. 2x16: LCD Keypad Shield for Arduino Duemilanove LCD1602
-Relays:
-1 relay for the CO2 injection. MG-811 sensor module.
CO2 Parameters. ON Only when lights are on (Light value > 800) and the PPM is below or equal to 1250PPM. Maintain within these parameter.
If PPM greater than 2250 PPM shut off CO2 and turn on evacuation hood and turn off CO2.
-1 relay for evacation hood constant ON
-1relay for evacuation hood constant Off.

Optionally once these issues are solved; I would like to add the following:

  • RTC DS3231 module in orde to control the lights.
    -be able to use the push buttons on the LCD shield in order to change the values and parameters.
    -add relays to control the watering system.

Here is below the sketch I put together but does not function properly.

I grealty appreciate anyone's help in this matter and thank you for your inputs, guidance, help and code corrections.

include <DateTime.h>
#include "DHT.h"
#include <LiquidCrystal.h>
#include <Wire.h>
 LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
/*******************Demo for MG-811 Gas Sensor Module V1.1*****************************
Author:  Tiequan Shao: tiequan.shao@sandboxelectronics.com
         Peng Wei:     peng.wei@sandboxelectronics.com
          
Lisence: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
 
Note:    This piece of source code is supposed to be used as a demostration ONLY. More
         sophisticated calibration is required for industrial field application. 
          
                                                    Sandbox Electronics    2012-05-31
************************************************************************************/
 
/************************Hardware Related Macros************************************/
#define         MG_PIN                       (5)     //define which analog input channel you are going to use
#define         BOOL_PIN                     (6)
#define         DC_GAIN                      (8.5)   //define the DC gain of amplifier
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302) 
/***********************Software Related Macros************************************/
#define         READ_SAMPLE_INTERVAL         (50)    //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_TIMES            (5)     //define the time interval(in milisecond) between each samples in 
                                                     //normal operation
 
/**********************Application Related Macros**********************************/
//These two values differ from sensor to sensor. user should derermine this value.
#define         ZERO_POINT_VOLTAGE           (0.368) //define the output of the sensor in volts when the concentration of CO2 is 400PPM
#define         REACTION_VOLTGAE             (0.0) //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2
 
/*****************************Globals***********************************************/
float           CO2Curve[3]  =  {2.602,ZERO_POINT_VOLTAGE,(REACTION_VOLTGAE/(2.602-3))};   
                                                     //two points are taken from the curve. 
                                                     //with these two points, a line is formed which is
                                                     //"approximately equivalent" to the original curve.
                                                     //data format:{ x, y, slope}; point1: (lg400, 0.324), point2: (lg4000, 0.280) 
                                                     //slope = ( reaction voltage ) / (log400 –log1000) 
 //define analog inputs to which we have connected our sensors
int moistureSensor1 = 0;
int moistureSensor2 = 1;
int moistureSensor3 = 2;
int moistureSensor4 = 3;
int lightSensor = 4;
int dht22 = 2;


//define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
//int waterPump = 24;
//int lightSwitch = 26;
//int tempLed = 28;
int evacuationconstON = 3;
int CO2 = 5;
int evacuationconstOFF = 6;

//define variables to store moisture, light, and temperature values
int moisture_val1;
int moisture_val2;
int moisture_val3;
int moisture_val4;
int light_val;
int temp_val;
int humidity_val;
int CO2_val;

////decide how many hours of light your plants should get daily
//float hours_light_daily_desired = 14;

//calculate desired hours of light total and supplemental daily based on above values
//float proportion_to_light = hours_light_daily_desired / 24;
//float seconds_light = 0;
//float proportion_lit;

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

DHT dht(DHTPIN, DHTTYPE);
 
void setup()
{
  lcd.begin(16,2);
{
    Serial.begin(9600);                              //UART setup, baudrate = 9600bps
    pinMode(BOOL_PIN, INPUT);                        //set pin to input
    digitalWrite(BOOL_PIN, HIGH);                    //turn on pullup resistors
   Serial.print(" Hello Victor and welcome to your own personalized garden control system ");               
 Serial.println(" DHT22 ");
 
 dht.begin();

//set the water, light, and temperature pins as outputs that are turned off
//pinMode (waterPump, OUTPUT);
//pinMode (lightSwitch, OUTPUT);
//pinMode (tempLed, OUTPUT);
pinMode (dht22, OUTPUT);
pinMode (CO2, OUTPUT);
pinMode (evacuationconstON, OUTPUT);
pinMode (evacuationconstOFF, OUTPUT);
//digitalWrite (waterPump, LOW);
//digitalWrite (lightSwitch, LOW);
//digitalWrite (tempLed, LOW);
digitalWrite (dht22, LOW);
digitalWrite (CO2, HIGH);
digitalWrite (evacuationconstON, LOW);
digitalWrite (evacuationconstOFF, LOW);


//establish start time
start_time = DateTime.now();
seconds_elapsed_total = 0;

}
}
void loop()

   {
    int percentage;
    float volts;
     
    
    volts = MGRead(MG_PIN);
    Serial.print( "SEN-00007:" );
    Serial.print(volts); 
    Serial.print( "V           " );
  
    percentage = MGGetPercentage(volts,CO2Curve);
    Serial.print("CO2:       ");
    lcd.setCursor(0,1);
    lcd.print("CO2:       ");
        if (percentage == -1) {
        Serial.print( "<400" );
        lcd.print("<400");
            } else {
        Serial.print(percentage);
        lcd.print(percentage);
       
          {
   
      
  

  }
    }
  
    Serial.print( "PPM" );
    lcd.print( " PPM");    
    Serial.print( "       Time point:" );
    Serial.print(millis());     
    Serial.print("\n");       
     
    if (digitalRead(BOOL_PIN) ){
        Serial.println( "=====BOOL is HIGH======" );
    } else {
        Serial.println( "=====BOOL is LOW======" );
    }
            if (light_val>800 && CO2_val<1250  )
            {// Condition change!!!!!!!!!!!!!!!!if CO2 level is below 1250ppm and light is ON or light is high
      digitalWrite(CO2, LOW);// turn on CO2 pump.
      Serial.println(" CO2 turned ON ");//  write to screen that the CO2 pump is ON
      lcd.setCursor(0,0);
      lcd.print(" CO2 turned ON ");
           digitalWrite(evacuationconstOFF, HIGH);//Turn OFF Extraction.!!!!!!!!!!!!!!!!!!
      Serial.println(" Extraction1 OFF ");// Write to creen that the pump is off. 
      lcd.setCursor(0,1);
      lcd.print(" Extraction1 OFF ");
      digitalWrite(evacuationconstON, LOW);//Turn OFF the evacuation hood Constant ON.!!!!!!!!!!!!!!!!!!
      Serial.println(" Extraction2 OFF ");// Write to creen that the pump is off.   
      lcd.setCursor(0,0);
      lcd.print(" Extraction2 OFF ");
       
}   
 else
         {
            digitalWrite(CO2, HIGH);// ELSE STATEMENT. In all other conditions turn OFF the CO2.
      Serial.println(" CO2 turned OFF ");// Write to screen: CO2 is turned OFF
      lcd.setCursor(0,0);
      lcd.print(" CO2 turned OFF ");
      Serial.println(" Extraction1 OFF "); //Write to screen: Extraction is OFF
      lcd.setCursor(0,1);
      lcd.print(" Extraction1 OFF ");
  delay(3000);
      
         }
            {
         delay (1000);// wait for one second.
       }
       
               if (CO2_val >= 2250){// Condition change!!!!!!!!!!!!!!!! if CO2 level is above 2250ppm.
      digitalWrite(CO2, HIGH);// Condition change!!!!!!!!!!!!!!!!turn off CO2 pump.      
      Serial.println(" CO2 LEVEL CRITICAL, CO2 turned OFF ");// Condition change!!!!!!!!!!!!!!!!if CO2 level is above 2250ppm write to screen that the CO2 pump has been turned off.
      lcd.setCursor(0,0);
      lcd.print(" CO2 CRITICAL, CO2 turned OFF ");
      lcd.setCursor(0,1);
      lcd.print(" CO2 turned OFF ");
      delay(3000);
      digitalWrite(evacuationconstOFF, LOW);//Turn on the evacuation hood.!!!!!!!!!!!!!!!!!!
      Serial.println(" Extraction1 ON ");// Write on the screen Extraction Const OFF is ON.
      digitalWrite(evacuationconstON, HIGH);//Turn OFF the evacuation hood Constant ON.!!!!!!!!!!!!!!!!!!
      Serial.println(" Extraction2 ON ");// Write to creen Extraction is ON!!!!!
    }

  while (percentage >= 2250) 

     
   
     
{
  // Wait a few seconds between measurements.  
  // DHT22 Section.  Check to see if all works correctly with other probes and relays!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  
  if (t >= 36 ){// If Temp is above or equal to 36 degrees.
   digitalWrite(evacuationconstOFF, HIGH);// tun ON the evacuation hood Normally OFF.
   Serial.println( " Evacuation ON ");// print Hood ON.
   }
   else// otherwise.
   {
     digitalWrite(evacuationconstOFF, LOW); //turn OFF the Hood.
   }
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
   

   

{
digitalWrite(evacuationconstOFF, HIGH);//turn on the evacuation Hood.
Serial.println(" Temperature Alert, Air extraction ON ");
}
  }

  // Compute heat index 
  // Must send in temp Celcius
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  
    lcd.setCursor(0,0);
     lcd.print("Humidity: "); 
  lcd.print(" %\t");
  delay(1000);
  
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.println(" ");
 {
   lcd.setCursor(0,1);
  lcd.print("Temperature: "); 
  lcd.print(t);
  lcd.print(" *C ");
  lcd.println(" ");
  delay(1000);
 }
}   

// read the value from the moisture-sensing probes, print it to screen, and wait a second
moisture_val1 = analogRead(moistureSensor1);
Serial.print(" moisture sensor 1 reads ");
Serial.println( moisture_val1 );
{
  lcd.setCursor(0,0);
  lcd.print("Moisture 1  ");
    lcd.print( moisture_val1 );
  delay(1000);
}
moisture_val2 = analogRead(moistureSensor2);
Serial.print(" moisture sensor 2 reads ");
Serial.println( moisture_val2 );
{
  lcd.setCursor(0,1);
  lcd.print("Moisture 2   ");
    lcd.print( moisture_val2 );
  delay(1000);
}
moisture_val3 = analogRead(moistureSensor3);
Serial.print(" moisture sensor 3 reads ");
Serial.println( moisture_val3 );
{
  lcd.setCursor(0,0);
  lcd.print("Moisture 3   ");
    lcd.print( moisture_val3 );
  delay(1000);
}
moisture_val4 = analogRead(moistureSensor4);
Serial.print("moisture sensor 4 reads ");
Serial.println(moisture_val4 );
{
  lcd.setCursor(0,1);
  lcd.print("Moisture 4   ");
    lcd.print( moisture_val4 );
  delay(1000);
}
{
// read the value from the photosensor, print it to screen, and wait a second
light_val = analogRead(lightSensor);
Serial.print("light sensor reads ");
Serial.println( light_val );
   {
  lcd.setCursor(0,0);
  lcd.print("Light Sensor ");
    lcd.print( light_val );
  delay(1000);
}

// read the value from the temperature sensor, print it to screen, and wait a second
//temp_val = analogRead(tempSensor);
//Serial.print("temp sensor reads ");
//Serial.println( temp_val );
//   {
//  lcd.setCursor(0,1);
//  lcd.print("Temperature ");
//    lcd.print( temp_val );
//  delay(1000);
//}
// 
//Serial.print("seconds total = ");
//Serial.println( seconds_elapsed_total );
//Serial.print("seconds lit = ");
//Serial.println( seconds_light);
//Serial.print("proportion desired = ");
//Serial.println( proportion_to_light);
//Serial.print("proportion achieved = ");
//Serial.println( proportion_lit);

//turn water on when soil is dry, and delay until soil is wet
//if (moisture_val < 850)
{
//digitalWrite(waterPump, HIGH);
}

//while (moisture_val < 850)
{

}

//digitalWrite(waterPump, LOW);

//update time, and increment seconds_light if the lights are on
//seconds_for_this_cycle = DateTime.now() - seconds_elapsed_total;
//seconds_elapsed_total = DateTime.now() - start_time;
//if (light_val > 900)
//{
//seconds_light = seconds_light + seconds_for_this_cycle;
//}

//cloudy days that get sunny again: turn lights back off if light_val exceeds 900. this works b/c the supplemental lights aren't as bright as the sun:)
//if (light_val > 900)
//{
////digitalWrite (lightSwitch, LOW);
//}
//
////turn off lights if proportion_lit>proportion_to_light, and then wait 5 minutes
//if (proportion_lit > proportion_to_light)
//{
////digitalWrite (lightSwitch, LOW);
//delay (300000);
//}

//figure out what proportion of time lights have been on
//proportion_lit = seconds_light/seconds_elapsed_total;
//
////turn lights on if light_val is less than 900 and plants have light for less than desired proportion of time, then wait 10 seconds
//if (light_val < 900 and proportion_lit < proportion_to_light)
//{
////digitalWrite(lightSwitch, HIGH);
//delay(10000);
//}
//
////turn on temp alarm light if temp_val is less than 850 (approximately 50 degrees Fahrenheit)
//if (temp_val < 850)
{
//digitalWrite(tempLed, HIGH);
}

 
}

   }
/*****************************  MGRead *********************************************
Input:   mg_pin - analog channel
Output:  output of SEN-000007
Remarks: This function reads the output of SEN-000007
************************************************************************************/
float MGRead(int mg_pin)
{
    int i;
    float v=0;
 
    for (i=0;i<READ_SAMPLE_TIMES;i++) {
        v += analogRead(mg_pin);
        delay(READ_SAMPLE_INTERVAL);
    }
    v = (v/READ_SAMPLE_TIMES) *5/1024 ;
    return v;  
}
 
/*****************************  MQGetPercentage **********************************
Input:   volts   - SEN-000007 output measured in volts
         pcurve  - pointer to the curve of the target gas
Output:  ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm) 
         of the line could be derived if y(MG-811 output) is provided. As it is a 
         logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic 
         value.
************************************************************************************/
int  MGGetPercentage(float volts, float *pcurve)
{
   if ((volts/DC_GAIN )>=ZERO_POINT_VOLTAGE) {
      return -1;
   } else { 
      return pow(10, ((volts/DC_GAIN)-pcurve[1])/pcurve[2]+pcurve[0]);
   }
}

You didn't need to make a new post, you just needed to fixed the one you had.

Here is below the sketch I put together but does not function properly.

So what is it doing? In your Arduino software, press CTRL + T, then replace this code with what you just changed.

No new post.

Thank you. Trying to get better at it :slight_smile:

Hope this is better and that the pictures do help a bit.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

How to use this forum

Ok Thank you. Did not mean to do so. It was a mistake. Thank you for your comprehension.

Here is below the sketch I put together but does not function properly.

The sketch by itself seems to work yet:

-The relays do not turn on and off correctly as the PPM rises or the temperature rises or the light sensor detects that there is no light.

In short, I am in dire need for someone to look at the mistakes that could cause such malfuncitons.

At first, the realys funciton.

Once the light value is implemented, the hoods are supposed to turn off but do not.
and once the PPM rises above the critical value, the hoods do not turn on and the CO2 does not turn off.

-The relays do not turn on and off correctly as the PPM rises or the temperature rises or the light sensor detects that there is no light.

You need to be specific. What do you expect to happen? What actually happens?

Here is below the sketch I put together but does not function properly.

That could mean anything.

Thank you for your reply.

During night time:

-Evacuation hood const ON = ON.
-Evacuation hood const OFF = OFF.
-CO2 = OFF

Daytime: When light sensor reads > 800:

-Evacuation hood const ON = OFF.
-Evacuation hood const OFF = OFF.
-CO2 = ON

Only if CO2 <= 1250 && Light Value >800.
Maitain CO2 level between 1250PPM and 1750PPM.
Maintain evacuation hoods off.

Critical parameters:

If CO2 level > 2250PPM:

-turn off CO2
-Evacuation hood const OFF = ON.
-Do so until the conditoins stabalize and come back to the parameters.

If Temp > 36 degrees celcius

-turn off CO2
-Evacuation hood const OFF = ON.
-Do so until the conditoins stabalize and come back to the parameters.
Write all these informaiton to LCD and to monitor.

At this point of time,

the evacuation hoods relays do not turn on and off with these parameters nor the CO2 relay.

meaning that the relays seem to turn on and off with a mind of their own due to my sketch issues.

Hope this helps.

Please let me know if there is additional information needed.

I can't see anything obvious, but it isn't easy to read. Can you please turn "loop" into a series of functions, each one doing a specific thing? It's just a wall of instructions.

And what's with this stuff?

 {
    delay (1000);// wait for one second.
  }

What do the braces achieve?

The problem is with your IF statements.

if (light_val>800 && CO2_val<1250 ) <- what happens if CO2_Val is 2250 here, it will still be true and so this if statements executes. Then once this is done, being that CO2_val is still 2250, it will then go here if (CO2_val >= 2250){/

What you need to do is check the light level and see if the CO2 level is between 1250 and 2249

Thank you for your reply once more.

From what I learned so far and from other examples I have seen and found; the "loop" can be used for different functions each doing specific tasks.

please find Below an example:
Source: Garduino: Geek Gardening with Arduino | Make:

#include 

//define analog inputs to which we have connected our sensors
int moistureSensor = 0;
int lightSensor = 1;
int tempSensor = 2;

//define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
int waterPump = 7;
int lightSwitch = 8;
int tempLed = 2;

//define variables to store moisture, light, and temperature values
int moisture_val;
int light_val;
int temp_val;

//decide how many hours of light your plants should get daily
float hours_light_daily_desired = 14;

//calculate desired hours of light total and supplemental daily based on above values
float proportion_to_light = hours_light_daily_desired / 24;
float seconds_light = 0;
float proportion_lit;

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

void setup() {
//open serial port
Serial.begin(9600);
//set the water, light, and temperature pins as outputs that are turned off
pinMode (waterPump, OUTPUT);
pinMode (lightSwitch, OUTPUT);
pinMode (tempLed, OUTPUT);
digitalWrite (waterPump, LOW);
digitalWrite (lightSwitch, LOW);
digitalWrite (tempLed, LOW);

//establish start time
start_time = DateTime.now();
seconds_elapsed_total = 0;

}
void loop() {
// read the value from the moisture-sensing probes, print it to screen, and wait a second
moisture_val = analogRead(moistureSensor);
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
delay(1000);
// read the value from the photosensor, print it to screen, and wait a second
light_val = analogRead(lightSensor);
Serial.print("light sensor reads ");
Serial.println( light_val );
delay(1000);
// read the value from the temperature sensor, print it to screen, and wait a second
temp_val = analogRead(tempSensor);
Serial.print("temp sensor reads ");
Serial.println( temp_val );
delay(1000);
Serial.print("seconds total = ");
Serial.println( seconds_elapsed_total );
delay(1000);
Serial.print("seconds lit = ");
Serial.println( seconds_light);
delay(1000);
Serial.print("proportion desired = ");
Serial.println( proportion_to_light);
delay(1000);
Serial.print("proportion achieved = ");
Serial.println( proportion_lit);
delay(1000);

//turn water on when soil is dry, and delay until soil is wet
if (moisture_val < 850)
{
digitalWrite(waterPump, HIGH);
}

while (moisture_val < 850)
{
delay(10000);
}

digitalWrite(waterPump, LOW);

//update time, and increment seconds_light if the lights are on
seconds_for_this_cycle = DateTime.now() - seconds_elapsed_total;
seconds_elapsed_total = DateTime.now() - start_time;
if (light_val > 900)
{
seconds_light = seconds_light + seconds_for_this_cycle;
}

//cloudy days that get sunny again: turn lights back off if light_val exceeds 900. this works b/c the supplemental lights aren't as bright as the sun:)
if (light_val > 900)
{
digitalWrite (lightSwitch, LOW);
}

//turn off lights if proportion_lit>proportion_to_light, and then wait 5 minutes
if (proportion_lit > proportion_to_light)
{
digitalWrite (lightSwitch, LOW);
delay (300000);
}

//figure out what proportion of time lights have been on
proportion_lit = seconds_light/seconds_elapsed_total;

//turn lights on if light_val is less than 900 and plants have light for less than desired proportion of time, then wait 10 seconds
if (light_val < 900 and proportion_lit < proportion_to_light)
{
digitalWrite(lightSwitch, HIGH);
delay(10000);
}

//turn on temp alarm light if temp_val is less than 850 (approximately 50 degrees Fahrenheit)
if (temp_val < 850)
{
digitalWrite(tempLed, HIGH);
}

}

NO. Do not do this without a way of breaking out of the while loop.

while (moisture_val < 850) 
{
delay(10000);
}

moisture_val will never change and so your code will never break from this while loop.

delay (300000);

Surely your code can do something more productive with 5 minutes then nothing at all.

You may be right. I have to check this out.

Also do you think that a while statement should follow the if statement in order to maintain the parameters between fields and then finish the IF statement with an else?

Could that do the trick?

Some of the code you find on the Internet is not without problems.

Anyway:

  while (percentage >= 2250) 




  {
    // Wait a few seconds between measurements.  
    // DHT22 Section.  Check to see if all works correctly with other probes and relays!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    delay(2000);

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit
    float f = dht.readTemperature(true);

    if (t >= 36 ){// If Temp is above or equal to 36 degrees.
      digitalWrite(evacuationconstOFF, HIGH);// tun ON the evacuation hood Normally OFF.
      Serial.println( " Evacuation ON ");// print Hood ON.
    }
    else// otherwise.
    {
      digitalWrite(evacuationconstOFF, LOW); //turn OFF the Hood.
    }

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      return;




      {
        digitalWrite(evacuationconstOFF, HIGH);//turn on the evacuation Hood.
        Serial.println(" Temperature Alert, Air extraction ON ");
      }
    }

    // Compute heat index 
    // Must send in temp Celcius
    float hi = dht.computeHeatIndex(f, h);

    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");

    lcd.setCursor(0,0);
    lcd.print("Humidity: "); 
    lcd.print(" %\t");
    delay(1000);

    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.print(" *C ");
    Serial.println(" ");
    {
      lcd.setCursor(0,1);
      lcd.print("Temperature: "); 
      lcd.print(t);
      lcd.print(" *C ");
      lcd.println(" ");
      delay(1000);
    }
  }

Assuming percentage is >= 2250 at this point, when does it ever go below that? In other words, how is this loop exited?

Plus what HazardsMind said about something very similar.

You have a lot of debugging displays. Good. How about sharing what they tell you?

If I am not making a mistake; you are refferring to the CO2 level here.

-The CO2 turns off when the light goes out.
-It needs to reach an ideal level of between 1250 PPM and 2250PPM.
-In case the CO2 goes beyond that point; the CO2 needs to turn OFF and the hood ON.

The CO2 can regulated perhaps with a while statement in order to remain between the 1250 PPM and 2250PPM as long as the lights are on.

The CO2 can regulated perhaps with a while statement in order to remain between the 1250 PPM and 2250PPM as long as the lights are on.

The CO2 is not regulated with a while statement. If you are going to use a while statement then you need to re-read the CO2.

Even then, what happens if it takes hours to regulate it? You aren't checking anything else while you are doing it.

You main loop should be short, then it will become more obvious what the structure is.