After a bit of help checking my code.
I am developing a temperature controller that will read from multiple DS18B20 and then activate a one of series of relays using the digital output from the arduino. Each DS18B20 is linked to a different relay. So it’s basically acting like multiple temp controllers. With the exception that the last relay is activated if any of other relays are on, and off when all the other relays are off.
I have tested it with 4 DS18B20 and the Serial Monitor and it seems to work fine. I understand my code is not brillante and a lot of the work could be better done using classes. However it works as far as I can tell. Can anyone spot any major floors in the code?
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS_PIN 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress Probe01 = {
0x28, 0x17, 0xD8, 0xEA, 0x05, 0x00, 0x00, 0xC7 }; //Blank DS18B20
DeviceAddress Probe02 = {
0x28, 0x19, 0x28, 0xEB, 0x05, 0x00, 0x00, 0xFD}; //DS18B20 with tape
//DeviceAddress Probe03 = {}; // ????
DeviceAddress Probe04 = {
0x28, 0x6B, 0xE3, 0x2A, 0x06, 0x00, 0x00, 0x0B }; //Black China
DeviceAddress Probe05 = {
0x28, 0xCF, 0xFC, 0x2A, 0x04, 0x00, 0x00, 0xD7 }; //Zac Wire with Wayki
//Define Temp Set Points
float SetPointTank1 = 21;
float SetPointTank2 = 21;
float SetPointTank3 = 21;
float SetPointTank4 = 21;
float SetPointTank5 = 21;
float SetPointFridge = 21;
//Asign Digital Pins to valves and SSR
int Valve1 = 7; //Tank1 - Bright Beer Tank
int Valve2 = 8; //Tank2 - 2000L Leaky Fermenter
int Valve3 = 9; //Tank3 - Short China Fermenter
int Valve4 = 10; //Tank4 - Black Clad Peruvian Fermenter
int Valve5 = 11; //Tank5 - 2000L New China Fermenter
int Valve0 = 12; //Fridge - Walk in cooler
int Pump = 13; //Recirc Pump on Chiller (SSR)
void setup(void)
{
// initialize the digital pins as an output.
pinMode(Valve0, OUTPUT);
pinMode(Valve1, OUTPUT);
pinMode(Valve2, OUTPUT);
pinMode(Valve3, OUTPUT);
pinMode(Valve4, OUTPUT);
pinMode(Valve5, OUTPUT);
pinMode(Pump, OUTPUT);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Probe01, 12);
sensors.setResolution(Probe02, 12);
// sensors.setResolution(Probe03, 12);
sensors.setResolution(Probe04, 12);
sensors.setResolution(Probe05, 12);
// start serial port
Serial.begin(9600);
}
void loop(void)
{
sensors.requestTemperatures(); // Send the command to get temperatures
float currentTempTank1;
float currentTempTank2;
float currentTempTank3;
float currentTempTank4;
float currentTempTank5;
float currentTempFridge;
currentTempTank1 = sensors.getTempC(Probe01);
currentTempTank2 = sensors.getTempC(Probe02);
// currentTempTank3 = sensors.getTempC(Probe03);
currentTempTank4 = sensors.getTempC(Probe04);
currentTempFridge = sensors.getTempC(Probe05);
/* Check Temp on Tank 1 */
if ( currentTempTank1 <= SetPointTank1 )
{
Serial.print("Tank 1 Temperature is too LOW! It is: ");
Serial.println(currentTempTank1);
digitalWrite(Valve1, LOW);
}
if ( currentTempTank1 > SetPointTank1 )
{
Serial.print("Tank 1 Temperature is too HOT! It is: ");
Serial.println(currentTempTank1);
digitalWrite(Valve1, HIGH);
}
/*Check Temp on Tank 2*/
if ( currentTempTank2 <= SetPointTank2 )
{
Serial.print("Tank 2 Temperature is too LOW! It is: ");
Serial.println(currentTempTank2);
digitalWrite(Valve2, LOW);
}
if ( currentTempTank2 > SetPointTank2 )
{
Serial.print("Tank 2 Temperature is too HOT! It is: ");
Serial.println(currentTempTank2);
digitalWrite(Valve2, HIGH);
}
/* Check Temp on Tank 3 NOT ACTIVE
if ( currentTempTank3 <= SetPointTank3 )
{
Serial.print("Tank 3 Temperature is too LOW! It is: ");
Serial.println(currentTempTank3);
digitalWrite(Valve3, LOW);
}
if ( currentTempTank3 > SetPointTank3 )
{
Serial.print("Tank 3 Temperature is too HOT! It is: ");
Serial.println(currentTempTank3);
digitalWrite(Valve3, HIGH);
} */
/* Check Temp on Tank 4 */
if ( currentTempTank4 <= SetPointTank4 )
{
Serial.print("Tank 4 Temperature is too LOW! It is: ");
Serial.println(currentTempTank4);
digitalWrite(Valve4, LOW);
}
if ( currentTempTank4 > SetPointTank4 )
{
Serial.print("Tank 4 Temperature is too HOT! It is: ");
Serial.println(currentTempTank4);
digitalWrite(Valve4, HIGH);
}
/* Check Temp on Tank 5 NOT ACTIVE
if ( currentTempTank5 <= SetPointTank5 )
{
Serial.print("Tank 5 Temperature is too LOW! It is: ");
Serial.println(currentTempTank5);
digitalWrite(Valve5, LOW);
}
if ( currentTempTank5 > SetPointTank5 )
{
Serial.print("Tank 5 Temperature is too HOT! It is: ");
Serial.println(currentTempTank5);
digitalWrite(Valve5, HIGH);
} */
/* Check Temp on Walk In Cooler */
if ( currentTempFridge <= SetPointFridge )
{
Serial.print("Fridge Temperature is too LOW! It is: ");
Serial.println(currentTempFridge);
digitalWrite(Valve0, LOW);
}
if ( currentTempFridge > SetPointFridge )
{
Serial.print("Fridge Temperature is too HOT! It is: ");
Serial.println(currentTempFridge);
digitalWrite(Valve0, HIGH);
}
/*Check if any valves are open. If yes, turn pump on, in No, pump off*/
if ( digitalRead(7) == LOW && digitalRead(8) == LOW && digitalRead(9) == LOW && digitalRead(10) == LOW && digitalRead(11) == LOW && digitalRead(12) == LOW )
{
Serial.println("Pump OFF");
digitalWrite(Pump, LOW);
}
if ( digitalRead(7) == HIGH || digitalRead(8) == HIGH || digitalRead(9) == HIGH || digitalRead(10) == HIGH || digitalRead(11) == HIGH || digitalRead(12) == HIGH )
{
Serial.println("Pump ON");
digitalWrite(Pump, HIGH);
}
// Wait 20 Secods and repeat loop
delay(2000);
}
The eventual aim is to connect the arduino to a Raspberry Pi and use that as a web server and control interface.