Hi,
below is the code I am using for measuring temperature and depending of the temperature controlling a relay. It works fine when the temperature is over zero degrees but it wont show negative values when the temperature is under zero degrees. It will show some readings but not the actual temperature.
Another problem is that when the relay is pulled on(it is starting a frequensy controller) it will give inteference(for about a half second when contoller is starting) to the temperature measurement and sometimes set the relay off, would a short delay after digitalWrite(RelayPin, RelayState); do it.
I am supplying power, 9v, to the arduino via the barrel plug, could it help to supply power via the usb port, to give the temperature probs more voltage? I somewhere read that it could help as the voltage drop is bigger in the barrel plug?
Anyone have toughts about this?
#include <RunningAverage.h> //Rob Tillaart
#include <OneWire.h>
#include <LiquidCrystal_I2C.h> //NewLiquidCrystal_I2C made by F:Malpartida
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
RunningAverage myRAIN(20);
RunningAverage myRAOUT(20);
float tempC = 0;
const int RelayPin = 10; // Output pin that contols relay
const int ClearPin = 11; // Clears Min and Max
boolean ClearSwitchState = LOW;
boolean lastClearSwitchState = LOW;
const int spPins[] = {14, 15, 16, 17};
const int pinCount = 4;
float insideTemp = 0; // value read from the insidetemp
float setPointIn = 3; // Initial value of inside setPoint
float outsideTemp = 0; // value read from the outsidetemp
float setPointOut = 0.25; // Initial value of outside setPoint
boolean SW1state = LOW; // Store state of SW1
boolean SW2state = LOW; // Store state of SW2
boolean SW3state = LOW; // Store state of SW3
boolean SW4state = LOW; // Store state of SW4
boolean lastSW1state = LOW; // Store laststate of SW1
boolean lastSW2state = LOW; // Store laststate of SW2
boolean lastSW3state = LOW; // Store laststate of SW3
boolean lastSW4state = LOW; // Store laststate of SW4
int RelayState = 0; // Store state of Relay
const int menuswitchPin = 9; // momentary switch
int Display = 0;
boolean MenuSwitchState = LOW;
boolean lastMenuSwitchState = LOW;
const int tempInterval = 2000;
unsigned long lastTempTimeIn = 0;
unsigned long lastTempTimeOut = 0;
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
DeviceAddress outsideThermometer = { 0x28, 0xFF, 0x33, 0x77, 0x01, 0x17, 0x03, 0x67 }; // Marked Temp 2
DeviceAddress insideThermometer = { 0x28, 0xFF, 0x32, 0x8F, 0x01, 0x17, 0x03, 0xC7 }; // Marked Temp 1
void setup()
{
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(spPins[thisPin], INPUT_PULLUP);
}
pinMode(menuswitchPin, INPUT_PULLUP);
pinMode(RelayPin, OUTPUT);
pinMode(ClearPin, INPUT_PULLUP);
myRAIN.clear(); // explicitly start clean
myRAOUT.clear();
lcd.begin(20, 4);
lcd.clear();
/*-(start serial port to see results )-*/
delay(10);
Serial.begin(115200);
Serial.println("Temperature Program");
Serial.println("Temperature Sensor: DS18B20");
delay(10);
/*-( Start up the DallasTemperature library )-*/
sensors.begin();
sensors.setWaitForConversion(false); // requestTemperatures() will not block current thread
// set the resolution to 10 bit
sensors.setResolution(insideThermometer, 10);
sensors.setResolution(outsideThermometer, 10);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
lcd.print("Error");
}
else
{
lcd.print(tempC);
}
}
void loop(void)
{
// Check states of pushbuttons, if pressed change setpoint up or down
SW1state = digitalRead(spPins[0]);
if (lastSW1state == LOW && SW1state == HIGH)
{
setPointIn += 0.25;
}
lastSW1state = SW1state;
SW2state = digitalRead(spPins[1]);
if (lastSW2state == LOW && SW2state == HIGH)
{
setPointIn -= 0.25;
}
lastSW2state = SW2state;
// Check states of pushbuttons, if pressed change setpoint up or down
SW3state = digitalRead(spPins[2]);
if (lastSW3state == LOW && SW3state == HIGH)
{
setPointOut += 0.25;
}
lastSW3state = SW3state;
SW4state = digitalRead(spPins[3]);
if (lastSW4state == LOW && SW4state == HIGH)
{
setPointOut -= 0.25;
}
lastSW4state = SW4state;
sensors.requestTemperatures();
// Average for Inside Temp
if (millis() - lastTempTimeIn >= tempInterval)
{
float rnIN = sensors.getTempC(insideThermometer);
myRAIN.addValue(rnIN);
Serial.print("Running Average IN: ");
Serial.println(myRAIN.getAverage(), 2);
lastTempTimeIn = millis();
}
delay(20);
// Average for Outside Temp
if (millis() - lastTempTimeOut >= tempInterval)
{
float rnOUT = sensors.getTempC(outsideThermometer);
myRAOUT.addValue(rnOUT);
Serial.print("Running Average OUT: ");
Serial.println(myRAOUT.getAverage(), 2);
lastTempTimeOut = millis();
}
delay(20);
// read the sensor value
insideTemp = myRAIN.getAverage();
outsideTemp = myRAOUT.getAverage();
#define OFFSET 0.5
// Check sensor value against setpoint
if
((insideTemp > (setPointIn + OFFSET)) && (outsideTemp > (setPointOut + OFFSET)) && (insideTemp > (outsideTemp + OFFSET)))
RelayState = 0;
else if
(( insideTemp <= setPointIn ) || ( outsideTemp <= setPointOut) || (insideTemp <= outsideTemp))
RelayState = 1;
digitalWrite(RelayPin, RelayState);
ClearSwitchState = digitalRead(ClearPin);
if (ClearSwitchState == HIGH && lastClearSwitchState == LOW)
{
delay(20); // delay to debounce switch
{
myRAIN.clear();
myRAOUT.clear();
}
}
lastClearSwitchState = ClearSwitchState;
MenuSwitchState = digitalRead(menuswitchPin);
if (MenuSwitchState == HIGH && lastMenuSwitchState == LOW)
{
delay(20); // delay to debounce switch
Display ++;
if (Display > 2)
{
lcd.clear();
Display = 1;
}
}
if (Display == 1)
{
lcd.setCursor(5, 0);
lcd.print("Temperatur");
lcd.setCursor(0, 1);
lcd.print("IN:");
lcd.print(" ");
lcd.setCursor(4, 1);
printTemperature(insideThermometer);
lcd.setCursor(11, 1);
lcd.print("UT:");
lcd.print(" ");
lcd.setCursor(15, 1);
printTemperature(outsideThermometer);
lcd.setCursor(5, 2);
lcd.print("Setpoint ");
lcd.setCursor(0, 3);
lcd.print("IN:");
lcd.print(" ");
lcd.setCursor(4, 3);
lcd.print(setPointIn);
lcd.setCursor(11, 3);
lcd.print("UT:");
lcd.print(" ");
lcd.setCursor(15, 3);
lcd.print(setPointOut);
}
else if (Display == 2)
{
lcd.setCursor(5, 0);
lcd.print("Temperatur");
lcd.setCursor(0, 1);
lcd.print("MIN ");
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print("MAX ");
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("IN:");
lcd.print(" ");
lcd.setCursor(4, 2);
lcd.print(myRAIN.getMin(), 2);
lcd.setCursor(11, 2);
lcd.print("IN:");
lcd.print(" ");
lcd.setCursor(15, 2);
lcd.print(myRAIN.getMax(), 2);
lcd.setCursor(0, 3);
lcd.print("UT:");
lcd.print(" ");
lcd.setCursor(4, 3);
lcd.print(myRAOUT.getMin(), 2);
lcd.setCursor(11, 3);
lcd.print("UT:");
lcd.print(" ");
lcd.setCursor(15, 3);
lcd.print(myRAOUT.getMax(), 2);
}
lastMenuSwitchState = MenuSwitchState;
}