"If/else" code does not work on relay

Basically, this project aims to control a relay with a temperature. What I would like to do is when the temperature is below 38°C, the relay turns on and when the temperature exceeds 38°C, it will turn off. But, what happens is that the relay doesn't turn off even if the temperature exceeds 38°C.

For reference, I am using DHT11 for the temperature sensor. The setup is working just fine, it's just the condition where I'm having problems

Here's the code:

#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,  16, 2);

#define DHTPIN 7
#define DHTTYPE DHT11

int relay1 = 3;
int relay2 = 4;
int temp = 7;

DHT dht(DHTPIN, DHTTYPE);

void setup() {

Serial.begin(9600);

pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);

dht.begin();

lcd.init();
lcd.clear();
lcd.backlight();

}

void loop() {

  one();
//relay and motion connection
  delay(2000);
  }
  void one() {
    float dhtHumidity = dht.readHumidity();
    float dhtTemperature = dht.readTemperature();

    delay(1000);

  lcd.setCursor(0,0);

  lcd.print("Temp: ");
  lcd.print(dhtTemperature);
  lcd.print(" C");

  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  lcd.print(dhtHumidity);
  lcd.print(" H");
  }

  void two() {
    temp = dht.readTemperature();
      if (temp < 38) {
      digitalWrite(relay1, LOW);
      }
      else {
      digitalWrite(relay1, HIGH);
      }
     }

Note:

  1. The LCD Display is working just fine as well.
  2. I accidentally bought a 2 channel relay, but I will not use the 2nd channel.

Maybe because the code to trigger the relay is under function "two()" which is never called?

Hi! I'm kind of new to Arduino language (I'm doing it just for a project) so I kind of don't get what you meant by "never called"

Based on the code you posted

#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,  16, 2);

#define DHTPIN 7
#define DHTTYPE DHT11

int relay1 = 3;
int relay2 = 4;
int temp = 7;

DHT dht(DHTPIN, DHTTYPE);

void setup() {

Serial.begin(9600);

pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);

dht.begin();

lcd.init();
lcd.clear();
lcd.backlight();

}

void loop() {

  one();  //Function controlling LCD is called here
  two(); //Function switching the relay is called here
  delay(2000);
  }
  void one() {
    float dhtHumidity = dht.readHumidity();
    float dhtTemperature = dht.readTemperature();

    delay(1000);

  lcd.setCursor(0,0);

  lcd.print("Temp: ");
  lcd.print(dhtTemperature);
  lcd.print(" C");

  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  lcd.print(dhtHumidity);
  lcd.print(" H");
  }

  void two() {
    temp = dht.readTemperature();
      if (temp < 38) {
      digitalWrite(relay1, LOW);
      }
      else {
      digitalWrite(relay1, HIGH);
      }
     }

Thanks! It solved my problem, I know understand that I also have to declare "two()" after void loop

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