HI guys. I'm building a freezer temp controller for a friend's home brew system....
I have it all on a bread board and its working almost like it should. I'm having an issue with // int relayPin;
(its not staying HIGH as commanded, its only staying HIGH for a short time)
you will see in my code that if there is a temp difference of X then turn on relay pin and nested in that "IF" statement is another if statement to turn it off at a temp of Y. (line 76 and 77)
The code as is compiles fine and everything else works like it should.
I attempted to comment out line 76 and 77 to see if there was some type of issue with the value settings on that "IF" statement....
when i commented out the lines, it no longer compiled. It told me that neither of my functions have been declared in this scope.... the functions have nothing to do with what i commented out (at least to my knowlege)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int SensorPin = A1;
int SetPin = A3 ;
float sensorVal;
int setVal;
float sensorTemp;
int setTemp;
int tempDiffPos;
int tempDiffNeg;
int count;
int LED_B = 9; // PWM capable
int LED_G = 10; // PWM capable
int LED_R = 11; // PWM capable
int relayPin = 3; // PWM capable
const byte relaySensor = 4;
const byte button = 2;
void setup() {
pinMode(LED_B, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_R, OUTPUT);
pinMode(SetPin, INPUT);
pinMode(SensorPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(relaySensor, INPUT);
pinMode(button, INPUT);
count = 0;
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" ! Mmmmmmmm ! ");
lcd.setCursor(0, 1);
lcd.print(" KOEL'D BEER ");
delay(2000);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
sensorVal = analogRead(SensorPin);
sensorTemp = map (sensorVal, 410.00, 754.00, 28.00, 80.00); // sensor
tempDiffPos = (sensorTemp - setTemp);
tempDiffNeg = (setTemp - sensorTemp);
while (digitalRead(button) == HIGH) {
SetTempVal();
}
lcd.setCursor(0, 0);
lcd.print("SENSOR ");
lcd.setCursor(10, 0);
lcd.print(sensorTemp);
lcd.setCursor(0, 1);
lcd.print(" SET ");
lcd.setCursor(10, 1);
lcd.print(setTemp);
//************************************************** BLUE FUNCTION **********************************************
// turn cooling on and check cooling circuit in the mains power. if an issue is found in the mains power, log the fault and display fault
if (sensorTemp - setTemp > 1.3) {
digitalWrite(LED_G, LOW);
digitalWrite(LED_R, LOW);
digitalWrite(relayPin, HIGH);
delay(100);
if (tempDiffNeg = 1) { // issue when commented out
digitalWrite(relayPin, LOW); //
}
if (digitalRead(relaySensor) == LOW) {
count++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" SYSTEM ");
lcd.setCursor(0, 1);
lcd.print(" FAULT! ");
for (int i = 0; i <= count; i++) { // opperate pulse per count value
countDisplay(); // pulse LED
}
delay(1250);
}
while (digitalRead(relaySensor) == HIGH ) {
for (int b = 15; b <= 255; b++) {
analogWrite(LED_B, b);
delay(5);
}
for (int b = 255; b >= 15; b--) {
analogWrite(LED_B, b);
delay(5);
}
}
}
//************************************************ GREEN FUNCTION **********************************************
// if statement uses digital read so that green function doesnt clash with blue function. also display fault log if applicable
if (((tempDiffPos <= 1.35) && digitalRead(relaySensor) == LOW) || ((tempDiffNeg >= 1.35) && digitalRead(relaySensor) == LOW)) {
digitalWrite(LED_B, LOW);
digitalWrite(LED_R, LOW);
for (int g = 15; g <= 255; g++) {
analogWrite(LED_G, g);
delay(5);
}
for (int g = 255; g >= 15; g--) {
analogWrite(LED_G, g);
delay(5);
}
if (count > 0) {
for (int i = 0; i <= count; i++) { // opperate pulse per count value
countDisplay(); // pulse LED
}
delay(1250);
}
}
//********************************************* RED FUNCTION ********************************************
// display fault log if applicable and show there is no cooling action taking place
if (tempDiffNeg >= 1.36) {
digitalWrite(LED_B, LOW);
digitalWrite(LED_G, LOW);
if (count > 0) {
for (int i = 0; i <= count; i++) { // opperate pulse per count value
countDisplay(); // pulse LED
}
delay(1250);
}
for (int r = 15; r <= 255; r++) {
analogWrite(LED_R, r);
delay(5);
}
for (int r = 255; r >= 15; r--) {
analogWrite(LED_R, r);
delay(5);
}
}
// **************************************** DEFAULT TO OFF ****************************************
else {
digitalWrite(relayPin, LOW);
digitalWrite(LED_B, LOW);
digitalWrite(LED_G, LOW);
digitalWrite(LED_R, LOW);
}
}
// ***************************************** FAULT COUNTER FUNCTION ***********************************
void countDisplay() {
digitalWrite(LED_R, HIGH);
delay(350);
digitalWrite(LED_R, LOW);
delay(350);
}
int SetTempVal() {
// lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" KOEL'D BEER ");
lcd.setCursor(0, 1);
lcd.print("SET ");
setVal = analogRead(SetPin);
setTemp = map (setVal, 11.0, 880.0, 28.0, 80.0); // pot
lcd.setCursor(12, 1);
lcd.print(setTemp);
return setTemp;
}