My first code. - go easy please

I'm trying to regulate the temperature in a greenhouse.

I have a 4 relay board (controlling 4 solar fans) and a Nano3 running. The thresholds are:
60f - Fan 1 on
75f- Fans 1 and 2 on
80f- All 4 on

The Temps are off right now (testing variables for now).

Here's my code:

#include <DHT.h>;
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);

int chk;
float hum;
float temp;
//-----------------------------------------------------------------------------
void setup()
{
  Serial.begin(9600);
  dht.begin();
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);  
  pinMode(5, OUTPUT);
  digitalWrite(5,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(2,HIGH);
}
//--------------------------------------_---------------------------------------

  
void loop() {
  hum = dht.readHumidity();
  temp = (int)round(1.8 *  dht.readTemperature() + 30);
  
  if (temp > 60) {
    digitalWrite(5,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(2,HIGH);
  }
  if (temp > 70) {
    
    digitalWrite(5,LOW);
    digitalWrite(4,LOW);
    digitalWrite(3,LOW);
    digitalWrite(2,LOW);
  }
  else {
    digitalWrite(5, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(2, HIGH);
  }

  delay(6000);
  Serial.print("Hum: ");
  Serial.print(hum);
  Serial.print(" %");
  
  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print(" Degrees");
  Serial.println();
}

What am I missing to get the first temp block to trigger?

what is the first temp block ?

you should probably start the test with > 70, else test with >60 else go for the default

if (temp > 70) {
  // what to do if it's more than 70
  ...
} else  if (temp > 60) {
  // what to do if T° is between ]60, 70]
  ...
} else {
  // what to do if you are below or equal to 60
  ...
}
1 Like

When writing code try and read it like the microcontroller would. In loop that means 1 line at a time and then back to start. When writing a conditional statement do a calculation in your head as to whether it will be true or not. You have multiple conditional statements that will run true at the same time.
Either use the if else structure advised above or use a switch case. You are getting close. It is also wise to work on each component and the simplest block of code at any one time. Don’t over complicate. Often a single block of code can be tidied up into a function. You start progressing towards OOP (object oriented programming) which becomes easier to read, recycle and tweek to suit any new projects

if ((temp > 60) && (temp< 70) ) might be useful or not.

You are missing an 'else' between the first 'if' and the second 'if'. The second 'if' will be performed regardless of the first 'if' and will either turn all the fans on or all the fans off. That overrides whatever the first 'if' did.

You should give the pin numbers names and use the names to make the code easier to understand:

#include <DHT.h>;
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);

int chk;
float hum;
float temp;

const byte Fan1RelayPin = 2;
const byte Fan2RelayPin = 3;
const byte Fan3RelayPin = 4;
const byte Fan4RelayPin = 5;

//-----------------------------------------------------------------------------
void setup()
{
  Serial.begin(9600);
  dht.begin();
  pinMode(Fan1RelayPin, OUTPUT);
  pinMode(Fan2RelayPin, OUTPUT);
  pinMode(Fan3RelayPin, OUTPUT);
  pinMode(Fan4RelayPin, OUTPUT);

  // Initially, all fans off
  digitalWrite(Fan1RelayPin, HIGH);
  digitalWrite(Fan2RelayPin, HIGH);
  digitalWrite(Fan3RelayPin, HIGH);
  digitalWrite(Fan4RelayPin, HIGH);
}
//--------------------------------------_---------------------------------------


void loop()
{
  hum = dht.readHumidity();
  temp = (int)round(1.8 *  dht.readTemperature() + 30);

  if (temp <= 60)
  {
    // < 60, All fans off
    digitalWrite(Fan1RelayPin, HIGH);
    digitalWrite(Fan2RelayPin, HIGH);
    digitalWrite(Fan3RelayPin, HIGH);
    digitalWrite(Fan4RelayPin, HIGH);
  }
  else  if (temp < 70)
  {
    // 60-70, One fan on
    digitalWrite(Fan1RelayPin, LOW);
    digitalWrite(Fan2RelayPin, HIGH);
    digitalWrite(Fan3RelayPin, HIGH);
    digitalWrite(Fan4RelayPin, HIGH);
  }
  if (temp < 80)
  {
    // 70-80, Two fans on
    digitalWrite(Fan1RelayPin, LOW);
    digitalWrite(Fan2RelayPin, LOW);
    digitalWrite(Fan3RelayPin, HIGH);
    digitalWrite(Fan4RelayPin, HIGH);
  }
  else // >= 80  All fans on
  {
    digitalWrite(Fan1RelayPin, LOW);
    digitalWrite(Fan2RelayPin, LOW);
    digitalWrite(Fan3RelayPin, LOW);
    digitalWrite(Fan4RelayPin, LOW);
  }

  delay(6000);

  Serial.print("Hum: ");
  Serial.print(hum);
  Serial.print(" %");

  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print(" Degrees");
  Serial.println();
}

I know this is programming question, just curious how exactly fans control temperature?

Solar energy heats the air in the greenhouse. If the air inside the greenhouse gets too hot, blow the hot air out, causing cooler air to come in from outside. (The outside air is presumed cooler because it is not subject to the "greenhouse effect" (visible light being converted to infrared radiation that then gets trapped inside the greenhouse by reflecting off the glass).

80f is 26 celsius, this is normal for summer, it can get 35 like this summer, what then? I’m asking this because I have a feeling OP might think that because you could cool down a person with a fan it is the same for green house

Thank you for all the replies.

I have it going now!

The purpose since people asked.

Our greenhouse got significantly above air temp even with the doors open, some crops won't ripen if it's too hot. Tomato's won't ripen if it's too warm.

We are hoping the fans will help lower the temperature of the greenhouse.

Thank you again.

When it's dark.

It is. Fans cool green houses well enough to regulate for most plants.

The Adafruit DHT Humidity & Temperature Sensor Library will return temperature as Fahrenheit with:

// Read temperature as Fahrenheit (isFahrenheit = true)
  float temp = dht.readTemperature(true);

Which is good since the conversion formula is wrong.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.