Else if issues

Hi everyone,

I am new to arduino but am trying to learn as i go. I am having issues with an else if statement I kind of hacked together to try to get a couple relays to turn on based off the temperature. At the moment I am able to get the relay to turn on when the temperature gets over the max number and will then shut off when it gets to a good area. I cant seem to get the other relay to turn on if the temperature drops below the min though? I have tested all my hardware to make sure everything is working and it all points me back to my code right now. Any help would be greatly appreciated.

Sorry I am also new to using these forums for help so I pasted the code below but also tried to attach a pic, not sure if the attachment worked?

if (event.temperature <= 18) {
digitalWrite(hpinOut, HIGH);
delay(delayMS);
digitalWrite(hpinOut, LOW);
}
else {
digitalWrite(hpinOut, LOW);
}

if (event.temperature >= 25) {
digitalWrite(pinOut, HIGH);
delay(delayMS);
digitalWrite(pinOut, LOW);
}
else {
digitalWrite(pinOut, LOW);
}

code-help.JPG

Sorry I am also new to using these forums

forum rules and all the "read this before posting a question" should ring a bell..

in your first IF the else part means anything > 18
in your second IF the else part means anything < 25

the action of the first if basically is superseded by the second one.

try to write in plain English what you want to achieve when the t° is XXX

Please post the entire sketch using code tags (go into the Arduino IDE, Edit>Select All, Edit>Copy for Forum and paste that onto here).

The code looks ok, but we have no idea how hpinOut is defined, if it is properly set to output, or how the relay is wired.

Looks like the second IF block is overriding what you were trying to do in the first block.

You might try:

if (less than condition) {
    do stuff
} else if  (greater than condition) {
    do stuff
} else {
    do stuff for in the middle
}

Note that the digitalWrite's in the two IF statements are referring to different output pins.

@david_2018 Yep, totally missed that.

Thank you for the help @david_2018 here is the code.

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN A0     // Analog pin connected to the DHT sensor 

int pinOut = 7;       // Digital pin connected to relay
int hpinOut = 8;      // Digital pin connected to relay

#define DHTTYPE    DHT11     // DHT 11

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  
  Serial.begin(9600);
  
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(pinOut, LOW);
  digitalWrite(hpinOut, LOW);
  
  // Initialize device.
  
  dht.begin();

  sensor_t sensor;
  
  delayMS = sensor.min_delay / 1000;

}

void loop() {
  
  // Delay between measurements.
  
  delay(delayMS);
    
  sensors_event_t event;
  dht.temperature().getEvent(&event);

  // My if else to trigger relay depending on temperature
  
  if (event.temperature <= 18) {        
    digitalWrite(hpinOut, HIGH);
    delay(delayMS);              
    digitalWrite(hpinOut, LOW);
  }
  else {
    digitalWrite(hpinOut, LOW);       
  }
  
  if (event.temperature >= 25) {       
    digitalWrite(pinOut, HIGH);
    delay(delayMS);              
    digitalWrite(pinOut, LOW);
  }
  else {                                
   digitalWrite(pinOut, LOW);
  }
  
}

honeys:
could anyone tell me the code which can convert frequency into hertz. my max req is from 1 to 30Hz. i have tried with pulsein() function but it bug the whole code

Please start a new thread instead of hijacking one that has absolutely nothing to do with your question.

Before you do that, please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

david_2018:
Note that the digitalWrite's in the two IF statements are referring to different output pins.

good point - missed that as well!

At the moment I am able to get the relay to turn on when the temperature gets over the max number and will then shut off when it gets to a good area. I cant seem to get the other relay to turn on if the temperature drops below the min though?

I would suggest that you use a Serial print statement to verify that event.temperature is returning the value you think it is, and are testing for.

Explain more about the relays you are using.

What is the pulsed output all about. Are the relays active HIGH or LOW? delayMS is a value used in the sensor reading, why are you using it here for a pulse delay?

if (event.temperature <= 18) {       
    digitalWrite(hpinOut, HIGH);
    delay(delayMS);             
    digitalWrite(hpinOut, LOW);
  }
  else {
    digitalWrite(hpinOut, LOW);       
  }
 
  if (event.temperature >= 25) {       
    digitalWrite(pinOut, HIGH);
    delay(delayMS);             
    digitalWrite(pinOut, LOW);
  }
  else {                               
   digitalWrite(pinOut, LOW);
  }

I'm a bit surprised that any of the delays are working, you declared sensor in Setup(), but never gave it any value, so delayMS ends up with a value of 0.

You need the following in Setup() to populate sensor with data:

  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  delayMS = sensor.min_delay / 1000;

@cattledog i did mentioned earlier i kind of hacked this together from other code and am trying to learn as i go. maybe that is my issue? i am really new at this and just like messing with technology and things.

i did have serial print with this code and it showed me the temperature and humidity going up and down properly when i was debugging. i removed it and it actually helped part of my code work better (ie. both relays would then shut off when the temperature was in the middle).

they relays i have are "HONG WEI 10A 250VAC 10A 30VDC; 10A 125VAC 10A 28VDC". cheap ones off amazon.. which i believe i have set up as active HIGH?

the pulse with the relay was just me trying to make sure it was going through parts of my code as again i am new to arduino and the coding for it.

i am used to excel vba editor where you get to walk through and see where your code goes wrong. is there anything like that for arduino by chance?

@david_2018 i actually did have that in my code when i started hacking, i will add that back but i was still having the issue with that info there.

Can you post a picture of how the relays are connected to the arduino?

Have you tried swapping the relay connections to pins 7 & 8 to verify both relays actually work?

they relays i have are "HONG WEI 10A 250VAC 10A 30VDC; 10A 125VAC 10A 28VDC". cheap ones off amazon.. which i believe i have set up as active HIGH?

Most of these photo isolated relays are active LOW. Are you confusing NO and NC with what the input signal needs to be to activate them?

In any case, the pulse code is not correct. Figure out what turns the relay on and off. Is there an indicator light for status?

Getting the relays to work properly with the temperature limits will be very straighforward, once you figure out how the relays actually work, and you make sure they are both wired the same.

@david_2018 i attached a jpeg of how i wired everything up to the arduino. i tried both switching the wires and switching the pinOut in the code and both relays seem to work fine.

I really dont understand your delays...nowhere do you state how long the delay should be.

Proietti:
I really dont understand your delays...nowhere do you state how long the delay should be.

For some reason, he is using the delays defined in the DHT library for the minimum delay between samples. For a DHT11, that is 1000000uS, giving a 1000mS delay in the code. Probably just a carryover from the example code from the library, where delayMS is used to delay between samples from the sensor.