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]);
}
}