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);
}
}