Help with malfunctioning sketch - arduino keeps looping same "if"

Hey guys, lately ive decided to pull out the arduino and start growing vegetables again. After connecting everything and reprogramming the arduino to accomodate a 2 speed fan, it started going crazy and not as planned.
Im a novice, but the sketch did work a few months ago.
Take a look:

#include <DHT22.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <EthernetUdp.h>
#include <KSduino.h>

unsigned int deviceID = 3404;
unsigned int devicePwd = 1397; // MUST be a 4-digit number
unsigned int port = 58833; // Local UDP port number
byte serverIp = {
178,63,53,233 }; // KSduino Server address & port
unsigned int serverPort = 9930; // Server UDP port number
char message_buf[32];

KSduino ksd (deviceID, devicePwd, serverIp, serverPort);

byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip = {
192,168,1,177};
byte gateway = {
192, 168, 1, 1};
byte subnet = {
255, 255, 255, 0};

#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);

#define DHT22A_PIN 2
DHT22 DHT22A(DHT22A_PIN);
#define DHT22B_PIN 3
DHT22 DHT22B(DHT22B_PIN);

unsigned int auxFan = 6;
unsigned int mainFan = 7;
unsigned int lightPin = 8;
boolean auxFanOn = false;
boolean mainFanOn = false;
boolean lightsOn = true;
boolean TempSensorError = false;

DallasTemperature sensors(&oneWire);
DeviceAddress Temp1 = {
0x28, 0xAE, 0x23, 0x3C, 0x05, 0x00, 0x00, 0x88};
DeviceAddress Temp2 = {
0x28, 0xCC, 0x5F, 0x3C, 0x05, 0x00, 0x00, 0x69};
DeviceAddress Temp3 = {
0x28, 0xF2, 0x2A, 0x35, 0x05, 0x00, 0x00, 0xBB};
DeviceAddress Temp4 = {
0x28, 0xA7, 0x63, 0x3C, 0x05, 0x00, 0x00, 0xF7};

void setup()
{
Serial.begin(115200);
sensors.begin();
sensors.setResolution(10);
pinMode(auxFan, OUTPUT);
digitalWrite(auxFan, HIGH);
pinMode(mainFan, OUTPUT);
digitalWrite(mainFan, HIGH);
pinMode(lightPin, OUTPUT);
digitalWrite(lightPin, LOW);
ksd.begin (mac, ip, port);
}

void loop()
{

DHT22_ERROR_t errorCode;
delay(2000);
errorCode = DHT22A.readData();
errorCode = DHT22B.readData();

Serial.print(F("DHT22A Humidity (%): "));
int DHT22AHum = DHT22A.getHumidity();
Serial.println(DHT22AHum);

Serial.print(F("DHT22B Humidity (%): "));
int DHT22BHum = DHT22B.getHumidity();
Serial.println(DHT22BHum);

Serial.print(F("\nOneWire temperatures…\n"));
sensors.requestTemperatures();

Serial.print(F("Temp 1 = "));
float TempC1 = sensors.getTempC(Temp1);
Serial.println(TempC1);

Serial.print(F("Temp 2 = "));
float TempC2 = sensors.getTempC(Temp2);
Serial.println(TempC2);

float TempC3 = sensors.getTempC(Temp3);

Serial.print(F("Temp 4 = "));
float TempC4 = sensors.getTempC(Temp4);
Serial.println(TempC4);

float avgTempC = (TempC1 + TempC2 + TempC4)/3;
Serial.print("\n");
Serial.print(avgTempC);

if (avgTempC <= 10 || avgTempC >= 50)
{
digitalWrite(lightPin, HIGH);
lightsOn = false;
TempSensorError = true;
Serial.print(“Temps out of range - Check temp sensors!”);
}

else if(avgTempC < 23 && auxFanOn == true && TempSensorError == false || avgTempC <23 && mainFanOn == true && TempSensorError == false)
{
digitalWrite(auxFan, HIGH);
digitalWrite(mainFan, HIGH);
auxFanOn = false;
mainFanOn = false;
Serial.print (“Auxilary & Main Fan Off”);
}

else if(avgTempC > 23 && avgTempC <=24 && auxFanOn == false && TempSensorError == false)
{
digitalWrite(mainFan, HIGH);
delay(500);
digitalWrite(auxFan, LOW);
Serial.print ("\n Auxilary Fan On");
auxFanOn = true;
mainFanOn = false;
}

else if(avgTempC >= 25 && mainFanOn == false && TempSensorError == false)
{
digitalWrite(auxFan, HIGH);
delay(500);
digitalWrite(mainFan, LOW);
Serial.print ("\n Main Fan ON");
auxFanOn = false;
mainFanOn = true;
}

Serial.print("\n Temp sensor error:");
Serial.print(TempSensorError);
Serial.print("\n Lights:");
Serial.print(lightsOn);
Serial.print("\n");
Serial.print(F(“Auxilary & Main Fans status:”));
Serial.print("\n");
Serial.print(auxFanOn);
Serial.print("\n");
Serial.print(mainFanOn);
Serial.print("\n");

Serial.print("\n\r");

/------( BUILD THE PACKET TO SEND TO KSduino )------------------------------/
// Send parameters to KSduino server
ksd.beginPacket ();
ksd.addParameter (“f1”, TempC1);
ksd.addParameter (“f2”, TempC2);
ksd.addParameter (“f3”, TempC3);
ksd.addParameter (“f4”, TempC4);
ksd.addParameter (“f5”, avgTempC);
ksd.addParameter (“i1”, DHT22AHum);
ksd.addParameter (“i2”, DHT22BHum);
ksd.addParameter (“i3”, auxFanOn);

ksd.endPacket ();
delay(1000);
ksd.update ();
delay (7000);
}

Any suggestions to keep the code cleaner or more efficient would be highly appreciated.

The arduino is connected to a 8 relay module, with 2 Line wires for the fan and 1 for the lights. Everything is connected good and works perfectly as to wiring goes. I do have an ethernet shield on it too w5100 i think it was…

What seems to be happening is the arduino will measure the temps, put out the avgTemp and then initiate the correct IF statement, but it will turn the fan on for just a sec, then off and then rerun the code, again, initiating the right IF statement and so forth indefinitely.
Now, if you’ve noticed i have a auxFanOn and mainFanOn booleans which should be switched from false to true whenever an IF is met and one of the fans is turned on, then it should switch the boolean and the IF’s shouldnt be met until a temperature change, which obviously doesnt happen.
For some reason it seems like it doesnt happen and the arduino just reruns the whole code, gets to the right IF and still recognizes it as if the booleans were false… although after the previous run the right boolean should be on true and the code should skip that IF.
Now, if i put a 10 sec delay after switching the right pin to LOW, it does stay on, for exactly 10 secs, then reruns the code and back to the same IF, then it runs for 10 more secs and so forth.

Any advice would be appreciated, im stuck…

What does your serial output look like?

Those if statements have a lot of terms with && and || between them. I would use some parenthesis (even if they're not strictly needed) to break those lines up into groups to make sure they're being evaluated the way you want them to be.

wildbill:
What does your serial output look like?

Normal, just says the Serial print line in whatever IF statement it executes.
for example:
Temp1=23.00
temp2
temp3

23.00 (avgTempC)

Main Fan ON!
Auxilary & main fans:
0
1

something like that, if per se the temp was above 25C

something like that

Much nicer to have the actual output you're seeing.

Wildbill, thanks for the help.
Heres the actual serial output.

DHT22A Humidity (%): 50
DHT22B Humidity (%): 47

OneWire temperatures...
Temp 1 = 26.50
Temp 2 = 26.75
Temp 4 = 27.25

26.83
Auxilary fan OFF & Main ON!
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
1

DHT22A Humidity (%): 50
DHT22B Humidity (%): 47

OneWire temperatures...
Temp 1 = 26.50
Temp 2 = 26.75
Temp 4 = 27.25

26.83
Auxilary fan OFF & Main ON!
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
1

DHT22A Humidity (%): 50
DHT22B Humidity (%): 47

OneWire temperatures...
Temp 1 = 26.50
Temp 2 = 26.75
Temp 4 = 27.25

26.83
Auxilary fan OFF & Main ON!
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
1

DHT22A Humidity (%): 50
DHT22B Humidity (%): 47

OneWire temperatures...
Temp 1 = 26.50
Temp 2 = 26.75
Temp 4 = 27.25

26.83
Auxilary fan OFF & Main ON!
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
1

DHT22A Humidity (%): 50
DHT22B Humidity (%): 47

OneWire temperatures...
Temp 1 = 26.50
Temp 2 = 26.75
Temp 4 = 27.25

26.83
Auxilary fan OFF & Main ON!
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
1

DHT22A Humidity (%): 50
DHT22B Humidity (%): 47

OneWire temperatures...
Temp 1 = 26.50
Temp 2 = 26.75
Temp 4 = 27.25

26.83
Auxilary fan OFF & Main ON!
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
1

DHT22A Humidity (%): 50
DHT22B Humidity (%): 47

OneWire temperatures...
Temp 1 = 26.50
Temp 2 = 26.75
Temp 4 = 27.25

26.83
Auxilary fan OFF & Main ON!
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
1

These were taken 3-4 secs apart, which doesnt make sense because i have at least 10 secs of delays throughout the code.
BTW, the ethernet shield blinks off whenever this happens, can it have any relation to this?

Please modify your posts to use code tags not the quote. The code tag is the # next to the quote.

Put a print statement in the setup function to see if the arduino is being reset by interference.

I expect grumpy_mike has guessed it - it looks like you have a wiring issue such that when you switch on the main fan, the power drawn is sufficient to restart the arduino. You might also print millis in loop as a more evidence.

It's hard to say though, because it's evident that that serial output came from a modified version of your sketch, so any conclusions based on it without the updated sketch are suspect.

Thanks Grumpy mike, i will put a print in the setup and millis in the loop section too.
But, i do supply the arduino with a 9v transformer, is it still possible that the power drawn by 2 relays is enough?
BTW, i did mention that it works perfectly if i add a delay in the execution section of the IF.
I really dont think its electrical overloading, but im keeping an open mind anyway.

EDIT:
Heres the updated version, with a print in setup and a call for millis at the end.
Hope ill get this diagnosed now.
Thanks again, everyone.

#include <DHT22.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <EthernetUdp.h>
#include <KSduino.h>

// device ID & Password (received from "add New Device" at http://ksduino.org/?devices )
unsigned int deviceID   = 3451;
unsigned int devicePwd  = 2129;  // MUST be a 4-digit number 
unsigned int port       = 58833;              // Local UDP port number
byte         serverIp[] = { 
  178,63,53,233 };  // KSduino Server address & port
unsigned int serverPort = 9930;               // Server UDP port number
char   message_buf[32];

KSduino ksd (deviceID, devicePwd, serverIp, serverPort); // Create KSduino class

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {
  192,168,1,177};
byte gateway[] = {
  192, 168, 1, 1}; 
byte subnet[] = {
  255, 255, 255, 0}; 


#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);

#define DHT22A_PIN 2
DHT22 DHT22A(DHT22A_PIN);
#define DHT22B_PIN 3
DHT22 DHT22B(DHT22B_PIN);

unsigned int auxFan = 6;
unsigned int mainFan = 7;
unsigned int lightPin = 8;
unsigned long time;
boolean auxFanOn = false;
boolean mainFanOn = false;
boolean lightsOn = true;
boolean TempSensorError = false;

DallasTemperature sensors(&oneWire);
DeviceAddress Temp1 = { 
  0x28, 0xAE, 0x23, 0x3C, 0x05, 0x00, 0x00, 0x88};
DeviceAddress Temp2 = { 
  0x28, 0xCC, 0x5F, 0x3C, 0x05, 0x00, 0x00, 0x69};
DeviceAddress Temp3 = { 
  0x28, 0xF2, 0x2A, 0x35, 0x05, 0x00, 0x00, 0xBB};
DeviceAddress Temp4 = { 
  0x28, 0xA7, 0x63, 0x3C, 0x05, 0x00, 0x00, 0xF7};


void setup()
{
  Serial.begin(115200);
  Serial.print("Startup Initiated");
  sensors.begin();
  sensors.setResolution(10);
  pinMode(auxFan, OUTPUT);
  digitalWrite(auxFan, HIGH);
  pinMode(mainFan, OUTPUT);
  digitalWrite(mainFan, HIGH);
  pinMode(lightPin, OUTPUT);
  digitalWrite(lightPin, LOW);
  ksd.begin (mac, ip, port);
}


void loop() 
{

  DHT22_ERROR_t errorCode;
  delay(2000);
  errorCode = DHT22A.readData();
  errorCode = DHT22B.readData();


  Serial.print(F("DHT22A Humidity (%): "));
  int DHT22AHum = DHT22A.getHumidity();
  Serial.println(DHT22AHum);

  Serial.print(F("DHT22B Humidity (%): "));
  int DHT22BHum = DHT22B.getHumidity();
  Serial.println(DHT22BHum);

  Serial.print(F("\nOneWire temperatures...\n"));
  sensors.requestTemperatures();

  Serial.print(F("Temp 1 = "));
  float TempC1 = sensors.getTempC(Temp1);
  Serial.println(TempC1);


  Serial.print(F("Temp 2 = "));    
  float TempC2 = sensors.getTempC(Temp2);
  Serial.println(TempC2);
  
  float TempC3 = sensors.getTempC(Temp3);

  Serial.print(F("Temp 4 = "));
  float TempC4 = sensors.getTempC(Temp4);
  Serial.println(TempC4);

  float avgTempC = (TempC1 + TempC2  + TempC4)/3;
  Serial.print("\n");
  Serial.print(avgTempC);
  
  if (avgTempC <= 10 || avgTempC >= 50)
  {
    digitalWrite(lightPin, HIGH);
    lightsOn = false;
    TempSensorError = true;
    Serial.print("You have a defective temp sensor!");
  }
  
   else if(avgTempC < 23 && auxFanOn == true && TempSensorError == false)
  {
    digitalWrite(auxFan, HIGH);
    auxFanOn = false;
    Serial.print ("Auxilary  Fan  Off -  Temps Low... ");
  }

  else if(avgTempC > 23 && avgTempC <=24 && auxFanOn == false && mainFanOn == false && TempSensorError == false)
{
    digitalWrite(auxFan, LOW);
    Serial.print ("\nAuxilary Fan On");
    auxFanOn = true;
  }
  
  else if(avgTempC >= 25 && mainFanOn == false && TempSensorError == false)
 {
    digitalWrite(auxFan, HIGH);   
    digitalWrite(mainFan, LOW);
    Serial.print ("\nAuxilary fan OFF & Main ON  -  Temps Too High!");
    auxFanOn = false;
    mainFanOn = true;  
  }
 
Serial.print("\n Temp sensor error:");
  Serial.print(TempSensorError);
  Serial.print("\n Lights:");
  Serial.print(lightsOn);
  Serial.print("\n");
  Serial.print(F("Auxilary & Main Fans:"));
   Serial.print("\n");
  Serial.print(auxFanOn);
  Serial.print("\n");
  Serial.print(mainFanOn);
  Serial.print("\n");

  Serial.print("\n\r");


  /*------( BUILD THE PACKET TO SEND TO KSduino )------------------------------*/
  // Send parameters to KSduino server
  ksd.beginPacket ();
  ksd.addParameter ("f1",  TempC1);  
  ksd.addParameter ("f2",  TempC2); 
  ksd.addParameter ("f3",  TempC3); 
  ksd.addParameter ("f4",  TempC4); 
  ksd.addParameter ("f5",  avgTempC); 
  ksd.addParameter ("i1",  DHT22AHum); 
  ksd.addParameter ("i2",  DHT22BHum); 
  ksd.addParameter ("i3",  auxFanOn); 
  ksd.endPacket ();
  delay(1000);
  ksd.update ();  

  delay (7000);

time = millis ();
Serial.println(time);

}

Heres the updated version, with a print in setup and a call for millis at the end.

Fell free to compile, link, upload, and actually run that code, and post the serial output.

So, i uploaded it, aaaand... the problem is with all the IF section...

First things first, this is the output:

Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.25
Temp 4 = 20.25

20.25
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.00
Temp 4 = 20.25

20.17
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.00
Temp 4 = 20.25

20.17
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.00
Temp 4 = 20.25

20.17
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.00
Temp 4 = 20.25

20.17
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.00
Temp 4 = 20.25

20.17
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.00
Temp 4 = 20.25

20.17
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.00
Temp 4 = 20.25

20.17
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup InitiatedDHT22A Humidity (%): -99
DHT22B Humidity (%): -99

OneWire temperatures...
Temp 1 = 20.25
Temp 2 = 20.00
Temp 4 = 20.25

20.17
 Temp sensor error:0
 Lights:1
Auxilary & Main Fans:
0
0


Startup Initiated

It wont stop restarting.
Now, i deleted the KSDuino section at the end and the IF section in the middle, and it stopped doing that.
I have no idea what the problem might be, as i stated im a newbie...

Maybe i should change that to a switch case code thingy? Might that help?

Any advice anyone? :slight_smile:

So, i uploaded it, aaaand... the problem is with all the IF section...

How did you reach that (erroneous) conclusion? Something is making the Arduino restart AFTER the if statements.

Now, i deleted the KSDuino section at the end and the IF section in the middle, and it stopped doing that.

So, you still think that the problem is the if statements that execute successfully? Put the if statements back. They are NOT the problem.

What can it be then?
The ksduino part worked before as it is.
Seems weird to me, but im no pro so... ill take your advice.
im gonna try it right away.
Thanks Paul! :slight_smile:

EDIT:

HOT DAMN! You were right, it was the KSDUino part!

Thanks a lot Paul, youre the best! :smiley:

SOLVED (for now...)