Just can't quite get the code right

I am unable to turn off relays when my DSB120 reads a certain temperature. No matter how I change the code around I cant seem to get it right.

//

#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LCD.h>
#include <WProgram.h>
#include <Arduino.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TimeAlarms.h>
#include <avr/wdt.h>


//------------------------ Other Pins Used--------------------
int feedButton = 22;
int feedLed = 13;
int tempLed = 12;
float _currentTempF = 0.0;
bool  _tempReady      = false;

//---------Serial LCD--------------------------------------
#define txPin 24 // White wire from Serial LCD screen
const int LCDdelay = 10; // conservative, 2 actually works
SoftwareSerial LCD(0, txPin);

//-------------Dallas Temp------------------------------
#define ONE_WIRE_BUS 9    // NOTE: No ";" on #define 
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;

//--------------------------Relays--------------------------
#define sumpLights  53                        
#define skimmer  52                        
#define heater  51                        
#define heater2  50
#define sumpPump  49                        
#define atoRelay  48                        
#define atoPump  47                        
#define algaePump  46
#define TurnDeviceOn == LOW;
#define TurnDeviceOff == HIGH;


//--------------------------------------------------------------------------------
class Countdown
{
public:
	Countdown():m_zero(0){}
	void Set(unsigned long duration,
			 unsigned long currentTime)
	{
		m_zero = currentTime + duration;
	}
	bool Done(unsigned long currentTime)
	{
		return ((long)(currentTime-m_zero))>0;
	}
private:
	unsigned long m_zero;
};
Countdown _temperatureTimer;



//------------------------------LCD-----------------------------------

void lcdPosition(int row, int col) {
  LCD.write(0xFE); //command flag
  LCD.write((col + row*64 + 128)); //position
  delay(LCDdelay);
}

/*
void lcdPositionLine1() {
 LCD.write(0xFE); //command flag
 LCD.write(0x45);
 LCD.write(0x00);
 delay(LCDdelay);
 }
 */

void lcdPositionLine2() {
  LCD.write(0xFE); //command flag
  LCD.write(0x45);
  LCD.write(0x40);
  delay(LCDdelay);
}

void lcdPositionLine3() {
  LCD.write(0xFE); //command flag
  LCD.write(0x45);
  LCD.write(0x14);
  delay(LCDdelay);
}

void lcdPositionLine4() {
  LCD.write(0xFE); //command flag
  LCD.write(0x45);
  LCD.write(0x54);
  delay(LCDdelay);
}

void clearLCD(){
  LCD.write(0xFE); //command flag
  LCD.write(0x51); //clear command.
  delay(LCDdelay);
}

void serCommand(){ //a general function to call the command flag for issuing all other commands
  LCD.write(0xFE);
}

void setLCDContrast() {
  LCD.write(0xFE); //command flag
  LCD.write(0x52);
  LCD.write(40); //value 1 to 50 (50 is highest contrast)
  delay(LCDdelay);
}

void setLCDBrightness() {
  LCD.write(0xFE); //command flag
  LCD.write(0x53);
  LCD.write(5); //value 1 to 8
  delay(LCDdelay);
}

bool setupTempSensor()
{
	// locate devices on the bus
	Serial.print("Locating devices...");
	sensors.begin();
	Serial.print("Found ");

	int dc = sensors.getDeviceCount();
	Serial.print(dc, DEC);
	Serial.println(" devices.");
	if (dc==0) return false;

	// report parasite power requirements
	Serial.print("Parasite power is: "); 
	if (sensors.isParasitePowerMode()) Serial.println("ON");
	else Serial.println("OFF");

	if (!sensors.getAddress(insideThermometer, 0)) 
	{
		Serial.println("Unable to find address for Device 0"); 
		return false;
	}

	// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
	sensors.setResolution(insideThermometer, 12);

	Serial.print("Device 0 Resolution: ");
	Serial.print(sensors.getResolution(insideThermometer), DEC); 
	Serial.println();

	return true;  
}
// END---------------------------------------------------------------------------------------

void setup()   /****** SETUP: RUNS ONCE ******/
{
  //------- Initialize the Temperature measurement library--------------
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // locate devices on the bus
  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");

  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 


  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(insideThermometer, 9);

  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC); 
  Serial.println();
  //---------------- Initialize the lcd ------------------------------------------------
  pinMode(txPin, OUTPUT);
  LCD.begin(9600);

  //------------lcdPosition(0,0);---------------------------------------------------------
  clearLCD();
  setLCDContrast();
  setLCDBrightness();
  LCD.print("   FOGLEMAN REEF");
  
  //------- start timers-------------------------------------------------------
	unsigned long t = millis();
	if (_tempReady) _temperatureTimer.Set(5UL*1000UL,t); // 5s
      
  // -------------------------------INITIALIZE DEVICE STATES------------------------

  pinMode(sumpLights, OUTPUT);      //LOW IS ON FOR RELAYS     
  pinMode(skimmer, OUTPUT);	    //LOW IS ON FOR RELAYS 
  pinMode(heater, OUTPUT);          //LOW IS ON FOR RELAYS 
  pinMode(heater2, OUTPUT);	    //LOW IS ON FOR RELAYS 
  pinMode(sumpPump, OUTPUT); 	    //LOW IS ON FOR RELAYS       
  pinMode(atoRelay, OUTPUT);	    //LOW IS ON FOR RELAYS 
  pinMode(atoPump, OUTPUT);	    //LOW IS ON FOR RELAYS 
  pinMode(algaePump, OUTPUT);	    //LOW IS ON FOR RELAYS 
  pinMode(feedButton, INPUT);       //ONLY HIGH IF WE PUSH
  pinMode(tempLed, OUTPUT);         //ONLY HIGH TO TELL US TEMP IS HIGH AND HEATERS OFF
  pinMode(feedLed, OUTPUT);         //ONLY HIGH TO TELL US TEMP IS HIGH AND HEATERS OFF
}    
//--------------------------------------------------------------------------------
void UpdateHeating()
{
	// Heater
	if (_currentTempF <  78.7) digitalWrite(heater, LOW);
	if (_currentTempF >= 79.0) digitalWrite(heater && tempLed, HIGH);
}
//--------------------------------------------------------------------------------
void UpdateTemperature(unsigned long t)
{
  if (_tempReady && _temperatureTimer.Done(t))
  {
    sensors.requestTemperatures(); // Send the command to get temperatures
    float tempC = sensors.getTempC(insideThermometer);
    _currentTempF = DallasTemperature::toFahrenheit(tempC); // Converts tempC to Fahrenheit
		delay(250); // allow processor current levels to recover
    UpdateHeating();
    _temperatureTimer.Set(30UL*1000UL, t);
  }
}
//--(end setup )---------------------------------------------------


void loop()
{
   unsigned long t = millis();
  
    //BITRelays(t);  
  UpdateTemperature(t); // this will delay the processor for a few seconds
	t = millis();

  //---------------------------TEMP LOOP-------------------------------------------
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcdPositionLine3();  
  LCD.print("Reef Temp= ");
  displayTemperature(insideThermometer);  

  Alarm.delay(2000);

}

/*-----( Declare User-written Functions )-----*/
void displayTemperature(DeviceAddress deviceAddress)
{

  float tempC = sensors.getTempC(deviceAddress);

  if (tempC == -127.00) // Measurement failed or no device found
  {
    LCD.print("Temperature Error");
  } 
  else
  {
    LCD.print(DallasTemperature::toFahrenheit(tempC)); // Convert to F
  }


  //----------------------------RTC LOOP--------------------------------
  {
    tmElements_t tm;

    if (RTC.read(tm)) {
      lcdPositionLine2();
      LCD.print(tm.Hour);
      LCD.write(':');
      LCD.print(tm.Minute);
      LCD.write(':');
      LCD.print(tm.Second);
      LCD.print("  ");
      LCD.print(tm.Month);
      LCD.write('/');
      LCD.print(tm.Day);
      LCD.write('/');
      LCD.print(tmYearToCalendar(tm.Year));
    }
    else {
      if (RTC.chipPresent()) {
        Serial.println("The DS1307 is stopped.  Please run the SetTime");
        Serial.println("example to initialize the time and begin running.");
        Serial.println();
      } 
      else {
        Serial.println("DS1307 read error!  Please check the circuitry.");
        Serial.println();
      }
      Alarm.delay(9000);
    }
    Alarm.delay(1000);
  }
}

Its in the void UpdateHeating()
Thank you for looking

I didn't read your whole code. It's long. I point to the following line and ask you what it means?

if (_currentTempF >= 79.0) digitalWrite(heater && tempLed, HIGH);

The double ampersand means a logic AND. It will be TRUE if both heater and tempLed are TRUE or non-zero. It will be FALSE if one of them is FALSE or zero. Is that what you intended to do or do you want to turn on both heater and tempLed to HIGH, instead? You need two separate lines if you want two pins turned HIGH.

digitalWrite(heater && tempLed, HIGH);

If you want to turn both the heater and tempLed pin high, you need two separate calls to digitalWrite.

# define TEMP_LOW  78.7
# define TEMP_HIGH 79.0
...

void UpdateHeating()
{
  if (_currentTempF <  TEMP_LOW  ) heaterOn();
  else if (_currentTempF >= TEMP_HIGH ) heaterOff();
}

// inline will be optimized by compiler
inline void heaterOn() { digitalWrite(heater, LOW); }
inline void heaterOff() { digitalWrite(heater, HIGH); }

You should connect the tempLED to the heaterpin in hardware so it really indicates the state of the heater(pin)!

Furthermore I would advise to have more "distance" between the two temperatures, at least 1 degree to prevent too frequent on/off cycles. (but in the end it is a choice

SoftwareSerial LCD(0, txPin);

Do NOT use a hardware serial pin for SoftwareSerial!

PaulS:

SoftwareSerial LCD(0, txPin);

Do NOT use a hardware serial pin for SoftwareSerial!

Good eye!

PaulS:

SoftwareSerial LCD(0, txPin);

Do NOT use a hardware serial pin for SoftwareSerial!

Why not?
is there anything that forbids this? (except common sense maybe as a HW serial is more powerful than SW serial)

that asked, well noticed !

What the OP probably meant was

SoftwareSerial LCD(-1, txPin);

so no buffers are claimed (at least this used to be in SW Serial). Dive into the code to see if this still is (no time to do it now)

@robtillaart - I took your advice and removed my code with your and added the definitions at the top off main code with other definitions and replaced the other part of the code with yours. However its still not turning off relays like it should. I am totally stuck. Thank you for your help so far.

PaulS - I am using a 20x4 LCD that can be used as serial GND, 5v, and digital line 24. If I remove that line of code you said, I throw errors. Any ideas? To be honest I am not even sure what that line actually means. I am C++ blind. I've built 2 websites and still want to slam my head in the door when trying to understand all the ins and outs of the code. You guys know your stuff thats for sure.

Thanks to all that have replied.

SoftwareSerial LCD(-1, txPin);
I just implemented this with no errors so I will just use this, thank you. Forgot to mention instead of turning off relays with the temp, I modified the code to just trun on an LED when temp got too high, still nothing, so Im lost and officially suck.

@robtillaart - I took your advice and removed my code with your and added the definitions at the top off main code with other definitions and replaced the other part of the code with yours. However its still not turning off relays like it should. I am totally stuck. Thank you for your help so far.

How do you have the relays connected to the Arduino?
The Arduino cannot deliver enough current (amperes) to drive relays!!

google images "Arduino relay" and you find things like

GrundelGravy:
SoftwareSerial LCD(-1, txPin);
I just implemented this with no errors so I will just use this, thank you. Forgot to mention instead of turning off relays with the temp, I modified the code to just trun on an LED when temp got too high, still nothing, so Im lost and officially suck.

what is the output you get?
Can you minimize the code ?
make a copy and strip off all that is not essential, just back to one temp sensor and one heater relay
replace all LCD.print just with Serial.print
// yes that is a bit of work

If I look at : void displayTemperature(DeviceAddress deviceAddress)
it does much more than displaying the temperature.
This makes it is difficult to get the mental model right when to understand the code in detail

robtillaart-- I am using a sainsmart 8channel 5v relay board. I have it powered with the arduino 5v power adapter. I can just make a sketch to turn on off relays so I know they work. I think it has to do with my sketch seeing the temp and combining that with the relays.
What do you mean outputs? I will take the code all down to one core function which will be temp probe and relay and see.
Thanks

Could it be the float _currentTempF = 0.0; in the top?

I have removed everything I thought was now essential. For some reason I am not seeing the temperature in the serial com
heres the code

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>

//-------------defines------------------
int tempLed = 12;
float _currentTempF   = 0.0;
bool  _tempReady      = false;
# define TEMP_LOW  78.7
# define TEMP_HIGH 79.0
//--------------------------Relays--------------------------
#define RELAY1  53                        
#define RELAY2  52                        
#define RELAY3  51                        
#define RELAY4  50
#define RELAY5  49                        
#define RELAY6  48                        
#define RELAY7  47                        
#define RELAY8  46

//-------------Dallas Temp------------------------------
#define ONE_WIRE_BUS 9    // NOTE: No ";" on #define 
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;
//--------------------------------------------------------------------------------
class Countdown
{
public:
	Countdown():m_zero(0){}
	void Set(unsigned long duration,
			 unsigned long currentTime)
	{
		m_zero = currentTime + duration;
	}
	bool Done(unsigned long currentTime)
	{
		return ((long)(currentTime-m_zero))>0;
	}
private:
	unsigned long m_zero;
};
Countdown _temperatureTimer;

//--------------------------------------------------------------------------------
bool setupTempSensor()
{
	// locate devices on the bus
	Serial.print("Locating devices...");
	sensors.begin();
	Serial.print("Found ");

	int dc = sensors.getDeviceCount();
	Serial.print(dc, DEC);
	Serial.println(" devices.");
	if (dc==0) return false;

	// report parasite power requirements
	Serial.print("Parasite power is: "); 
	if (sensors.isParasitePowerMode()) Serial.println("ON");
	else Serial.println("OFF");

	if (!sensors.getAddress(insideThermometer, 0)) 
	{
		Serial.println("Unable to find address for Device 0"); 
		return false;
	}

	// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
	sensors.setResolution(insideThermometer, 12);

	Serial.print("Device 0 Resolution: ");
	Serial.print(sensors.getResolution(insideThermometer), DEC); 
	Serial.println();

	return true;  
}
//-------------------------------------------SETUP--------------------------------------------

void setup(){
// start serial port
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);       
  pinMode(RELAY6, OUTPUT);
  pinMode(RELAY7, OUTPUT);
  pinMode(RELAY8, OUTPUT);
  pinMode(tempLed, OUTPUT);

  Serial.begin(9600);
  Serial.println("Hello");
 // find our temp sensor
	_tempReady = setupTempSensor();
// start timers
	unsigned long t = millis();
	if (_tempReady) _temperatureTimer.Set(5UL*1000UL,t); // 5s
}
//-------------------------------------------HEATING--------------------------------------------
void UpdateHeating()
{
  if (_currentTempF <  TEMP_LOW  ) tempLedOn();
  else if (_currentTempF >= TEMP_HIGH ) tempLedOff();
}

// inline will be optimized by compiler
inline void tempLedOn() { digitalWrite(RELAY1, LOW); }
inline void tempLedOff() { digitalWrite(RELAY1, HIGH); }

//--------------------------------------------------------------------------------
void UpdateTemperature(unsigned long t)
{
  if (_tempReady && _temperatureTimer.Done(t))
  {
    sensors.requestTemperatures(); // Send the command to get temperatures
    float tempC = sensors.getTempC(insideThermometer);
    _currentTempF = DallasTemperature::toFahrenheit(tempC); // Converts tempC to Fahrenheit
      Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
		delay(250); // allow processor current levels to recover
    UpdateHeating();
    _temperatureTimer.Set(30UL*1000UL, t);
  }
}

//----------------------------------------------LOOP----------------------------------------

void loop(){
   UpdateTemperature; delay(1000); // this will delay the processor for a few seconds
	
}

Why not?
is there anything that forbids this? (except common sense maybe as a HW serial is more powerful than SW serial)

that asked, well noticed !

Well, there is one small problem with using the hardware serial pin for software serial:

  Serial.begin(9600);

Paul, is the -1 instead of 0 ok?

GrundelGravy:
Paul, is the -1 instead of 0 ok?

Yes. It maps to pin 255 which doesn't exist, so it won't interfere with another pin.

Thanks bud. I ended up scrapping this sketch and started fresh. Most everything works now. I just need to figure a better delay instead of using delay()

I just need to figure a better delay instead of using delay()

All Together Now "BlinkWithoutDelay"