refrigerator

Greetings fellow forummers. I just finished constructing a refrigerator where I can grow plants in it. Below are my coding

I intend to switch on my light from 10am - 2am and switch it off from 2am - 10am. Why it didnt work as intended?

Other than that the code seems okay.

How can i reduce the use of if to make it more tidy?

/* YourDuino.com Example Software Sketch
   DHT11 Humidity and Temperature Sensor test
   Displayed on I2C LCD Display
   Credits: Rob Tillaart
   http://arduino-info.wikispaces.com/PROJECT-Temp-Humidity-Display
   terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <dht.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

#include "RTClib.h"

RTC_DS1307 rtc;

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // twelve servo objects can be created on most boards
Servo myservo3;
Servo myservo4;

int pos1 = 0;  //mini- servo pos
int pos2 = 90;

/*---- Relay Channel---*/
#define humiRelay 10
#define lightRelay 11
#define acRelay 12

#define I2C_ADDR    0x3F  // Define I2C Address where the PCF8574A is
// Address can be changed by soldering A0, A1, or A2
// Default is 0x27

// map the pin configuration of LCD backpack for the LiquidCristal class
#define BACKLIGHT_PIN 3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C lcd(I2C_ADDR,
                      En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin,
                      BACKLIGHT_PIN, POSITIVE);
dht DHT;

int co; //declare carbon object


/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11_PIN 8

/*--Squirell Fan--*/
#define blower 9

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  //Serial.begin(9600); //(Remove all 'Serial' commands if not needed)
  lcd.begin(20, 4);        // initialize the lcd for 20 chars 4 lines, turn on backlight
  lcd.setBacklight(HIGH);
  // Print a message to the LCD.
  lcd.setCursor(0, 1);
  lcd.print("DHT11 Humid :");
  lcd.setCursor(0, 2);
  lcd.print("DHT11 Temp  : ");
  lcd.setCursor(0, 3);
  lcd.print("Carbon Dioxide:");

  myservo1.attach(7); // attaches the servo on pin 9 to the servo object
  myservo2.attach(6);
  myservo3.attach(5);
  myservo4.attach(4);

  pinMode(10, OUTPUT); //relay channels
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);

  rtc.begin();

}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  DateTime now = rtc.now();

  // READ DATA
  //Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
      //  Serial.print("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      //  Serial.print("Checksum error,\t");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      //  Serial.print("Time out error,\t");
      break;
    case DHTLIB_ERROR_CONNECT:
      // Serial.print("Connect error,\t");
      break;
    case DHTLIB_ERROR_ACK_L:
      //  Serial.print("Ack Low error,\t");
      break;
    case DHTLIB_ERROR_ACK_H:
      // Serial.print("Ack High error,\t");
      break;
    default:
      // Serial.print("Unknown error,\t");
      break;
  }
  // DISPLAY DATA
  //Serial.print(DHT.humidity, 1);
  // Serial.print(",\t");
  lcd.setCursor(15, 1);
  lcd.print((float)DHT.humidity, 0);
  lcd.print("%");
  // Serial.println(DHT.temperature, 1);
  //Serial.print(",\t");
  lcd.setCursor(15, 2);
  lcd.print((float)DHT.temperature, 1);
  lcd.print((char)223);
  co = analogRead(0); //Read Gas value from analog 0
  lcd.setCursor(15, 3);
  lcd.print(co, DEC);
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.print(' ');
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  // lcd.print();


  /*  NORMAL OPERATION

  if temp sensor reads between 17-25:
  servo 1 closed 0
  servo 2 open 90
  servo 3 closed 0
  AC relay is off
  */

  myservo1.write(pos1);
  myservo2.write(pos2);
  myservo3.write(pos1);

  if ( DHT.temperature > 16 &&  DHT.temperature < 26 )
  {
    digitalWrite(acRelay, LOW);
  }

  /**CHANGE OPERATION
  if temp sensor reads above 25:
  AC relay is on until temp sensor reads 16, then AC relay turns off.
  */

  else if (DHT.temperature > 25)
  {
    digitalWrite(acRelay, HIGH);
  }
  /*NORMAL OPERATION
  if humidity sensor reads same and above 70%:
  humidifier relay is off
  */
  if ( DHT.humidity >= 70 )
  {
    digitalWrite(humiRelay, LOW);
  }

  /*CHANGE OPERATION
  if humidity sensor reads blow 70%:
  humidifier relay is turned on until humidity sensor reads 80%, then humidity relay turns off.
  */

  else if ( DHT.humidity < 70)
  {
    digitalWrite(humiRelay, LOW);
  }

  /*
  /*NORMAL OPERATION
  if Co2 sensor reads between 1000-1500:
  servo 4 is closed 0
*/

  if (co >= 1000 && co <= 1500)
  {
    myservo4.write(pos1);
  }
   

  /*CHANGE OPERATION
  if Co2 sensor reads below 350:
  servo 1 opens 90
  servo 2 closed 0
  servo 3 opens 90
  servo 4 opens 90
  AC relay is off
  */

  if (co < 350 )
  {
    myservo1.write(pos2);
    myservo2.write(pos1);
    myservo3.write(pos2);
    myservo4.write(pos2);
  }

  /*
  once Co2 sensor reads 400:
  servo1 closed 0
  servo 2 closed 90
  servo 3 closed 0
  AC relay is on
  servo 4 open 90 until Co2 sensor reads 1500 then servo 4 turns off
  */
  else if ( co > 400)
  {
    myservo1.write(pos1);
    myservo2.write(pos2);
    myservo3.write(pos1);
    digitalWrite(acRelay, HIGH);
    if (co > 1500)
    {
      myservo4.write(pos1);
    }
  }


  /**NORMAL OPERATION
  if 10 am to 2am:
  LED relay is on
  */
  if (now.hour() > 10 && now.hour() < 02)
  {
    digitalWrite(lightRelay, HIGH);
  }

  /*CHANGE OPERATION
      if 2 am to 10am:
      LED relay is off
  */
  if (now.hour() > 02 && now.hour() < 10)
  {
    digitalWrite(lightRelay, LOW);
  }

  /*blower is on   */

  digitalWrite(9, 255); //change the fan speed here
  delay(200);

}/* --(end main loop )-- */

Have a better look :wink:

  if (now.hour() > 10 && now.hour() < 02)

How can something be higher then 10 and at the same time be lower then 2? Never going to happen :wink:

Btw, you can skip the second if, just replace it with an else. If you don't want them on you automatically want them off ;)

Why it didnt work as intended?

We don't know what it actually did. We don't know what you intended. So, how can we explain why "it didn't work as intended"?