Set greenhouse temp or humidity with an arduino

hello

I want to set the greenhouse temp and humidity for an arduino and dht21 module.

I set up the module and displayed temp and humidity on the LCD 2*16.

But I want to use the three buttons for adjustment temp and humidity with two relays.

I want to define one of the buttons as “set” and define the other button as “up” and the latest button as “down”.

so by pushing the “set button” we could set the temp by using two buttons of “up” and “down” and again by pushing the “set button” we could set the humidity by “up” and “down”.

But I couldn’t program this. :confused:

This is the code that I wrote here.

#include <MenuBackend.h>
#include <Bounce2.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

#define setBUTTON 13
#define upBUTTON 12
#define downBUTTON 11
#define DHTPIN 2
#define DHTTYPE DHT21

Bounce set;
Bounce up;
Bounce down;

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);

const int temprelay = 10, humidityrelay = 9;

float temp,hum;
const float i=temp, j=hum;


void setup() {
  
  lcd.begin(16, 2);
  lcd.clear();

  dht.begin();

    set.attach (setBUTTON);
    set.interval(50);
    
    up.attach (upBUTTON);
    up.interval(50);

    down.attach (downBUTTON);
    down.interval(50);

    
      pinMode (setBUTTON, INPUT_PULLUP);
      pinMode (upBUTTON, INPUT_PULLUP);
      pinMode (downBUTTON, INPUT_PULLUP);

}

void loop() {


  set.update();
  up.update();
  down.update();

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("Failed to read ");
    lcd.setCursor(0,1);
    lcd.print("from DHT sensor!");
    delay(2000);
    lcd.clear();
    return;
  }

      

  if ((t) >= i){
  digitalWrite (temprelay, HIGH);
  }
       else if ((t) <= i-5) 
       {
       digitalWrite (temprelay, LOW);
       }
  
       if ((h) >= j){
       digitalWrite (humidityrelay, LOW);
       }
       else if ((h) <=j-12)
       {
       digitalWrite (humidityrelay, HIGH);
       }

  
   lcd.print("Humidity:");
   lcd.setCursor(10,0);
   lcd.print(h);
   lcd.setCursor(15,0);
   lcd.print("%");
   lcd.setCursor(1,1);
   lcd.print("Temp:");
   lcd.setCursor(7,1);
   lcd.print(t);
   lcd.setCursor(12,1);
   lcd.print(" *C ");
}
float temp,hum;
const float i=temp, j=hum;

It does not make much sense to name global variables with one letter names. It does not make much sense to make constant “variables” that are initialized to 0.

  if ((t) >= i){
  digitalWrite (temprelay, HIGH);
  }
       else if ((t) <= i-5)
       {

Your indenting and inconsistent curly brace placement sucks.

Where you you ever care what happens to the switches? What, EXACTLY, are the switches supposed to increase, decrease or set?

Why do you need a library to do something as simple as reading a switch?

Thanks for your reply. :slight_smile:

OK. I removed the useless library. and it is supposed to set a special temperature and humidity by hand and then if the temp and humidity are upper than the defined value, relay will be activated automatically to turn on the ventilator.

it is supposed to set a special temperature and humidity by hand

So, press the up switch and the temperature AND humidity set points are increased. Press the down switch and the temperature AND humidity set points are decreased. Press the select switch and ?

then if the temp and humidity are upper than the defined value, relay will be activated automatically to turn on the ventilator.

You won't be able to change the values of (the uselessly named) i and j variables. So, they are not the set point variables. temp and hum are never actually used, so they are not the set point variables. So, just what variables ARE supposed to the be the set point variables?

Press the select switch and ?

press the select switch and it would be possible to modify the temp ( by pressing up and down buttons) and by pressing the select switch again ,modify humidity (by pressing up and down buttons). by pressing the select switch in the third stage , it can save and apply the settings.

cjmj:
But I want to use the three buttons for adjustment temp and humidity with two relays.

I want to define one of the buttons as "set" and define the other button as "up" and the latest button as "down".

so by pushing the "set button" we could set the temp by using two buttons of "up" and "down" and again by pushing the "set button" we could set the humidity by "up" and "down".

Push SET button.
Person wants to set temperature, so check the UP and DOWN buttons and also the SET button.
Push SET button.
Person now wants to set the humidity, so the check UP and DOWN and also the SET button.
Push SET button.
Do nothing until the next time SET button is pushed.

When you press the select switch you need something to advise the user whether s/he will be changing temperature or humidity.

If you add that facility to the program you will pretty much automatically take care of the problem you describe.

In slightly more technical jargon, the select switch should change the state of the program from (say) adjustState = 'T' (for temperature) to adjustState = 'H' (for humidity).

Then your code could do different things depending on the value in the variable adjustState

...R

so plz help me to complete this program.

what and how write the codes??

I am newbie in programming.

cjmj:
so plz help me to complete this program.

what and how write the codes??

I am newbie in programming.

I would breadboard a simple circuit with three tact pushbutton switches - SET, UP, DOWN and three resistors. https://www.arduino.cc/en/Tutorial/Button

If you bought one of those Arduino starter kits, it should come with some some tact pushbutton switches and 10Kohm resistors.

Use the Serial Monitor to help debug your program.
Use Serial.println("Temperature"); and Serial.println("Humidity"); to indicate state when the SET button is pressed.
Use Serial.println("Up"); and Serial.println("Down"); to indicate Up/Down button presses.

this isn't a program its a section that I removed from a different program. Its might give you a idea of how it can be done

//global
float humiditySetpoint = 50;
float tempSetpoint = 50;
unsigned long prevMillis = 0;
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
buttons();

//if (temp >  tempSetpoint){
  //do something
  //}
}

void buttons() {
  unsigned long currentmillis = millis();
  static byte prevminusButton;
  static byte prevplusButton;
  static byte preveditButton;
  static byte  prevEditMode;
  byte editButton = digitalRead(buttonedit);
  byte plusButton = digitalRead(buttonplus);
  byte minusButton = digitalRead(buttonminus);

  if ( editMode != 0) {//switch back to normal if left in edit for 30000 ms
    if (currentmillis - prevMillis >= 30000UL) {
      editMode = 0;
      prevMillis = currentmillis;
    }
  }
  if (editButton != preveditButton) {
    if (editButton == LOW) {
      editMode++;
    }
    preveditButton = editButton;
  }
  if (editMode >= 3) {
    editMode = 0;
  }

  if (editMode != prevEditMode) {
    lcd.clear();//clear lcd if screen info changes
    prevEditMode = editMode;
  }

  switch (editMode) {
    case 0://normal screen
      lcd.print("Humidity:");
      lcd.setCursor(10, 0);
      lcd.print(h);
      lcd.setCursor(15, 0);
      lcd.print("%");
      lcd.setCursor(1, 1);
      lcd.print("Temp:");
      lcd.setCursor(7, 1);
      lcd.print(t);
      lcd.setCursor(12, 1);
      lcd.print(" *C ");
      prevMillis = currentmillis;
      break;
      
    case 1://edit temp
      editDigits = 1;
      if (plusButton != prevplusButton) {
        if (plusButton == LOW) {
          tempSetpoint = tempSetpoint + 0.1;
        }
        prevplusButton = plusButton;
      }
      if (minusButton != prevminusButton) {
        if (minusButton == LOW) {
          tempSetpoint = tempSetpoint - 0.1;
        }
        prevminusButton = minusButton;
      }
      lcd.setCursor(0, 0)
      lcd.print("temp setpoint");
      lcd.setCursor(5, 1);
      lcd.print(tempSetpoint,2);
 break;
 
    case 2://edit humidity
      numberToDigits(1);
      editDigits = 2;
      if (plusButton != prevplusButton) {
        if (plusButton == LOW) {
          humiditySetpoint = humiditySetpoint + 0.1;
        }
        prevplusButton = plusButton;
      }
      if (minusButton != prevminusButton) {
        if (minusButton == LOW) {
          humiditySetpoint = humiditySetpoint - 0.1;
        }
        prevminusButton = minusButton;
      }
      lcd.setCursor(0, 0)
      lcd.print("humd setpoint");
      lcd.setCursor(5, 1);
      lcd.print(humiditySetpoint,2);
      break;
  }
}

P.s The code was not being used with a lcd so I just added something that looks correct. You would require code to add spaces when editing the setpoints or the digits will move on the screen

cjmj:
so plz help me to complete this program.

what and how write the codes??

I am newbie in programming.

Have you you modified your original program taking account of the advice you have already been given?

Assuming you have then post the latest version of your progam and tell us what it does and what you want it to do differently.

...R

this isn’t a program its a section that I removed from a different program. Its might give you a idea of how it can be done

thank you very much. I could make it.

#include <EEPROM.h>
#include <Bounce2.h>
#include <DHT.h>
#include <LiquidCrystal.h>

#define setBUTTON 11
#define upBUTTON 12
#define downBUTTON 13
#define DHTPIN 2
#define DHTTYPE DHT21

Bounce set;
Bounce up;
Bounce down;

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);

  const byte temprelay = 10, humidityrelay = 9;
  byte humiditySetpoint = 85;
  byte tempSetpoint = 20;
  unsigned long prevMillis = 0;
  unsigned long currentmillis = millis();
  static byte prevdownButton;
  static byte prevupButton;
  static byte prevsetButton;
  static byte  prevSetMode;
  byte setMode;
  
void setup() {

  lcd.begin(16, 2);
  lcd.clear();

  dht.begin();

    set.attach (setBUTTON);
    set.interval(30);
    up.attach (upBUTTON);
    up.interval(30);
    down.attach (downBUTTON);
    down.interval(30);

      pinMode (setBUTTON, INPUT_PULLUP);
      pinMode (upBUTTON, INPUT_PULLUP);
      pinMode (downBUTTON, INPUT_PULLUP);

  humiditySetpoint = EEPROM.read(0);
  tempSetpoint = EEPROM.read(1);
}

void loop() {
  
  set.update();
  up.update();
  down.update();

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("Failed to read ");
    lcd.setCursor(0,1);
    lcd.print("from DHT sensor!");
    delay(2000);
    lcd.clear();
    return;
  }

  if ((t) >= tempSetpoint){
  digitalWrite (temprelay, HIGH);
  }
       else if ((t) <= tempSetpoint-5) 
       {
       digitalWrite (temprelay, LOW);
       }
  
   if ((h) >= humiditySetpoint){
   digitalWrite (humidityrelay, LOW);
   }
       else if ((h) <=humiditySetpoint-10)
       {
       digitalWrite (humidityrelay, HIGH);
       }

  if ( setMode != 0) {
    if (currentmillis - prevMillis >= 10000UL) {
      setMode = 0;
      prevMillis = currentmillis;
    }
  }

    if (set.fell()) {
      setMode++;
    }
    prevsetButton = setBUTTON;
  
  if (setMode >= 3) {
    setMode = 0;
  }
  if (setMode != prevSetMode) {
    lcd.clear();
    prevSetMode = setMode;
  }

  switch (setMode) {
      case 0://normal
   lcd.print("Humidity:");
   lcd.setCursor(10,0);
   lcd.print(h);
   lcd.setCursor(15,0);
   lcd.print("%");
   lcd.setCursor(1,1);
   lcd.print("Temp:");
   lcd.setCursor(7,1);
   lcd.print(t);
   lcd.setCursor(12,1);
   lcd.print(" *C ");
   prevMillis = currentmillis;
      break;
    
    case 1://edit temp
      if ('upButton' != prevupButton) {
        if (up.fell()) {
          tempSetpoint = tempSetpoint + 1;
        }
        prevupButton = 'upButton';
      }
      if ('downButton' != prevdownButton) {
        if (down.fell()) {
          tempSetpoint = tempSetpoint - 1;
        }
        prevdownButton = 'downButton';
      }
      lcd.setCursor(0, 0);
      lcd.print("Set temp:");
      lcd.setCursor(7, 1);
      lcd.print(tempSetpoint);
      lcd.setCursor(9,1);
      lcd.print("*C");
      EEPROM.write(1, tempSetpoint);
      break;
 
    case 2://edit humidity
      if ('upButton' != prevupButton) {
        if (up.fell()) {
          humiditySetpoint = humiditySetpoint + 1;
        }
        prevupButton = 'upButton';
      }
      if ('downButton' != prevdownButton) {
        if (down.fell()) {
          humiditySetpoint = humiditySetpoint - 1;
        }
        prevdownButton = 'downButton';
      }
      lcd.setCursor(0, 0);
      lcd.print("Set Humidity:");
      lcd.setCursor(7, 1);
      lcd.print(humiditySetpoint);
      lcd.setCursor(9,1);
      lcd.print("%");
      EEPROM.write(0, humiditySetpoint);
      break;
  }}