I hate to bother everyone, but I have tinkered with the code and hardware for 3 days now and am about ready to pull my hair out.
Background: The project is a temperature control unit. When the temp reaches the set point +differential the unit should output a signal to an SSR, which in turn turns on the 110V mains. The freezer cools and when the sensor reaches set point-differential the output should turn off.
Problem: 1. Output "A4" is doing the opposite of what I would expect it to do. When the temp is low, the output is on. When the temp is high the output is off. I have simulated the SSR with a led and get the same behavior. I have tried different pins, same behavior. I have a led on pin "9" that indicates freezer is "On". This output is behaving correctly. 2. The buttons function poorly. By poorly I mean I may have to press the buttons multiple times to increase/decrease 1 unit. If I disconnect the temp sensor, the buttons perform much better. I am positive this is due to shoddy programming.
Any help would be greatly appreciated. Problem 1 is my main focus, but any input on problem 2 would be great. Don't go easy on me, I know I am a weak programmer, as this is a hobby for me.
Thanks,
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 4
#define TEMPERATURE_PRECISION 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
uint8_t therm1[8], therm2[8];
int heatcoolswitch = 8;
int freezer = A4;
int setpoint = 65;
int diff = 2;
int setpointbutton = 13;
int diffbutton = 12;
int upbutton = 10;
int downbutton = 11;
int led = 9;
int valsp = 0;
int valdiff = 0;
int valup = 0;
int valdown = 0;
int prevvalsp = 0;
int prevvaldiff = 0;
int prevvalup = 0;
int prevvaldown = 0;
unsigned long lastBtnUp = 0;
unsigned long lastBtnDwn = 0;
unsigned long lastBtnSP = 0;
unsigned long lastBtnDiff = 0;
int transInt = 30;
int Turnon = 0;
int Turnoff = 0;
void setup(void)
{
pinMode(freezer, OUTPUT);
digitalWrite(freezer, LOW);
pinMode(setpointbutton, INPUT);
digitalWrite(setpointbutton, HIGH);
pinMode(diffbutton, INPUT);
digitalWrite(diffbutton, HIGH);
pinMode(upbutton, INPUT);
digitalWrite(upbutton, HIGH);
pinMode(downbutton, INPUT);
digitalWrite(downbutton, HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
Serial.print("?c0");
sensors.begin();
sensors.getDeviceCount();
sensors.getAddress(therm1, 0);
sensors.setResolution(therm1, 12);
Serial.print("?f");
Serial.print("?x00");
Serial.print("?y0");
Serial.print("?C0 Temperature ");
delay(200);
Serial.print("?C1Monitoring Unit ");
delay(200);
Serial.print("?S2");
delay(1000);
Serial.print("?f");
}
void spset()
{
valup = digitalRead(upbutton);
valdown = digitalRead(downbutton);
{
if (valup == HIGH && prevvalup == LOW)
{if (millis() - lastBtnUp > transInt)
{
++setpoint;
}
lastBtnUp = millis();}
prevvalup = valup;
if (valdown == HIGH && prevvaldown == LOW)
{if (millis() - lastBtnDwn > transInt)
{
--setpoint;
}
lastBtnDwn = millis();}
prevvaldown = valdown;
}
}
void diffset()
{
valup = digitalRead(upbutton);
valdown = digitalRead(downbutton);
{
if (valup == HIGH && prevvalup == LOW)
{if (millis() - lastBtnUp > transInt)
{
++diff;
}
lastBtnUp = millis();}
prevvalup = valup;
if (valdown == HIGH && prevvaldown == LOW)
{if (millis() - lastBtnDwn > transInt)
{
--diff;
}
lastBtnDwn = millis();}
prevvaldown = valdown;
}
}
void printTemperature(uint8_t deviceAddress[])
{
float tempF = sensors.getTempF(deviceAddress);
Serial.print("?x09");
Serial.print("?y0");
Serial.print(tempF);
Serial.print("F");
Serial.print("?x00");
Serial.print("?y1");
Serial.print("SP=");
Serial.print(setpoint);
Serial.print(" ");
Serial.print("?x06");
Serial.print("?y1");
Serial.print("Diff=");
Serial.print(diff);
Serial.print(" ");
}
void tempcontrol(uint8_t deviceAddress[])
{
float tempF = sensors.getTempF(deviceAddress);
Turnon = setpoint + diff;
Turnoff = setpoint - diff;
if (tempF > Turnon)
{
digitalWrite(freezer, HIGH);
digitalWrite(led, HIGH);
}
if (tempF < Turnoff)
{
digitalWrite(freezer, LOW);
digitalWrite(led, LOW);
}
}
void loop(void)
{
sensors.requestTemperatures();
Serial.print("?x00");
Serial.print("?y0");
Serial.print("Sensor 1");
printTemperature(therm1);
tempcontrol(therm1);
valsp = digitalRead(setpointbutton);
if (valsp == HIGH)
{if (millis() - lastBtnSP > transInt)
{
prevvalsp = valsp;
spset();
}
lastBtnSP = millis();}
valdiff = digitalRead(diffbutton);
if (valdiff == HIGH)
{if (millis() - lastBtnDiff > transInt)
{
prevvaldiff = valdiff;
diffset();
}
lastBtnDiff = millis();}
}