Need help in coding an if else statment

Hello. For my project, I would like to make a heat pad that turns on automatically by sensing a presence, and when it reaches a specific temperature, the heat pad will turn off. For this project, I'm using an Arduino Uno, a DHT 11 for the temperature sensor, an i2c display (so it can display the temperature), a relay, and an HC-SR04 ultrasonic sensor (for automation purposes).

The condition wherein it will turn off after reaching a specific temperature works fine, but the ultrasonic sensor part doesn't work and I'm kind of stuck.

The step-by-step of what my project should be like:

  1. The ultrasonic sensor will sense a presence, and it will turn the whole system on
  2. The DHT11 will sense if the temperature is below or above 40 degrees Celsius. If it's below, the heat pad will turn on, else, it will turn off.
  3. When the presence is gone, the whole system will also turn off.

Here's the schematic diagram:
image

Here's the coding:

#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;
int dist = 9;

const int trigPin = 8;
const int echoPin = 9;

DHT dht(DHTPIN, DHTTYPE);

float duration, distance;

void setup() {

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

  Serial.begin(9600);

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


dht.begin();

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

}

void loop() {

  one();
  two();
  three();

  delay(2000);
  }
  void one() {
    if (dist > 50.0) {
      digitalWrite(relay1, HIGH);
    }
    else {
      digitalWrite(relay1, LOW);
    }
  }
  void two() {
    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 three() {
    temp = dht.readTemperature();
      if (temp < 40) {
      digitalWrite(relay1, LOW);
      }
      else {
      digitalWrite(relay1, HIGH);
      }
     }

  void four() {
    digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  delay(100);
  }

Any replies will be highly appreciated!

Note: I'm a high school STEM student who doesn't have any background in coding. I'm just doing this for a capstone project

your never called the function four() it reads the value of ultrasonic sensors

In your code you never call function four( ) which has the distance measurement.

Update:

The ultrasonic-relay connection now works. However, the temperature condition doesn't work. Even if the heat pad exceeds 40 degrees Celsius, it's still on.

To be clear, its goal is to turn off when it exceeds 40 degrees Celsius and turn on when it's below 40 degrees Celcius. It must turn off even with presence from the ultrasonic sensor

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

const int trigPin = 8;
const int echoPin = 9;

int relay2 = 4;
int temp = 7;

DHT dht(DHTPIN, DHTTYPE);

float duration, distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(relay2, OUTPUT);

  Serial.begin(9600);
  
  dht.begin();

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

void loop() {

  one();
  two();
  three();
  four();
}
  void one() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
  }

  void two() {
   if (distance > 50.0) {
      digitalWrite(relay2, HIGH);
    }
    else {
      digitalWrite(relay2, LOW);
    }
  }

  void three() {
    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 four() {
    temp = dht.readTemperature();
      if (temp < 40) {
      digitalWrite(relay2, LOW);
      }
      }


Well, now "four" doesn't have the "turn off if too hot" part:

void four() {
    temp = dht.readTemperature();
      if (temp < 40) {
      digitalWrite(relay2, LOW);
      }
      }

Also, to help properly format your code and make it more readable for you and others, do this before posting:

image

And, I suspect that this is not going to do what you want:

  void two() {
   if (distance > 50.0) {
      digitalWrite(relay2, HIGH);
    }
    else {
      digitalWrite(relay2, LOW);
    }
  }

two() and four() may be fighting each other if the distance is <= 50
.
.
.
PS: interesting choice of projects considering that...

...capstone project is a multifaceted academic experience typically required for students during the final year of an academic program. It is a comprehensive and interdisciplinary project that often requires students to apply the knowledge and skills acquired throughout their academic careers....

That makes sense. How can I stop the contradiction between two() and four() when the distance is less than or equal to 50? I can't seem to think of a solution for it.

Also, my capstone project was assigned to me by the teacher so I don't have a choice but to do it even if it's challenging for me.

Call two() from four() only if temperature is below 40 degC.
And consider to give your functions a sensible name....

How would I do calling two() from four()? I'm sorry I don't know Arduino language. To be clear, what I should do is make an if/else statement for two() and four()?

I also gave my functions from what I think is a sensible name:
void one() = void ultrasonic()
void two() = void ultrarelay()
void three () = void tempreading()
void four() = tempcondition()

You really should do a course on C++ basics.
Look here (or somewhere else):

In four:

void four() {
    if (t<40) {
         do_whatever_needs_done;
         two();  //call function two()
     }
     else {
         blablabla;
     }
}

And remove two() from loop()

thanks!!

Update with this one, the condition still only follows the ultrasonic sensor command. I tried to undo the names of the functions since it made it more confusing to me.

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

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define DHTPIN 7
#define DHTTYPE DHT11

const int trigPin = 8;
const int echoPin = 9;

int relay2 = 4;
int temp = 7;

DHT dht(DHTPIN, DHTTYPE);

float duration, distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(relay2, OUTPUT);

  Serial.begin(9600);

  dht.begin();

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

void loop() {

  one();
  three();
  four();
}
void one() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration * .0343) / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
}

void three() {
  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 four() {
  temp = dht.readTemperature();
  if (temp > 40) {
    digitalWrite(relay2, HIGH);
  }
  one();
  {
    if (distance > 50.0) {
      digitalWrite(relay2, HIGH);
    } else {
      digitalWrite(relay2, LOW);
    }
  }
}

The good thing is that the heat pad doesn't turn on when the distance is < 50, but the bad thing is that it only turns off when the distance is < 50 despite the temperature exceeding 40 degree Celcius

This should probably be inside the if block.
Move it inside the curly braces...
{}

And you really need to learn the basics...

It's been 2 hours of trying to find the right code. Putting the one() inside the if block turned the relay on even when the distance is < 50. In one code that I have tried like this one:

void four() {
  temp = dht.readTemperature();
  //If distance is less than 50, ON
  if (distance > 50.0) {
    digitalWrite(relay2, HIGH);
//if relay is ON
    if (relay2 == HIGH) {
//if relay is ON and temp is < 40, OFF
      if (temp < 40) {
        digitalWrite(relay2, LOW);
      }
    }
//if distance is < 50, OFF
  } else {
    digitalWrite(relay2, LOW);
  }
}

The same thing happens, the ultrasonic sensor condition is the only one working.
I kind of understand the principle of an if-else statement, I just can't find what part I'm still doing wrong.

if (relay2 == HIGH) {

relay2 holds the pin number, not the state of the relay...

Also, if you set the relay high, it is of no use to test if it is still high the very next line...

My head hurts already lol. I tried to make a "much simpler" code. This doesn't work but this is how I would like it to work:

void four() {
  temp = dht.readTemperature();
  if ((distance < 50) && (temp < 40)) {
    digitalWrite(relay2, LOW);
  }
  if ((distance < 50) && (temp > 40)) {
    digitalWrite(relay2, LOW);
  }
  if ((distance > 50) && (temp < 40)) {
    digitalWrite(relay2, LOW);
  }
  if ((distance) < 50 && (temp > 40)) {
    digitalWrite(relay2, HIGH);
  }
}

With this code, it stays on no matter what

Simplify:

Try this code:

#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 7
#define DHTTYPE DHT11
#define trigPin  8
#define echoPin  9
#define relay2  4
DHT dht(DHTPIN, DHTTYPE);
int temp = 7;
float duration, distance;
//--------------------------------------------------------------------
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(relay2, OUTPUT);
  Serial.begin(9600);
  dht.begin();
  lcd.init();
  lcd.clear();
  lcd.backlight();
}
//--------------------------------------------------------------------
void loop() {
  one();
  three();
  four();
}
//--------------------------------------------------------------------
void one() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * .0343) / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
}
//--------------------------------------------------------------------
void three() {
  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 four() {
  temp = dht.readTemperature();
  if (distance < 50.0 and temp < 40) {            
    digitalWrite(relay2, HIGH);
  }
  if (distance >= 50.0 or temp >= 40) {           
    digitalWrite(relay2, LOW);
  }
}

You still want it on, with nothing present? I guess I misunderstood you.

I want it off when nothing is present

Then why do you turn it on when the distance is >50?

OMG! This one worked! I just swapped where the HIGH and LOW conditions were. Thank you for all of those who replied to me, I appreciate you guys so much.