Dan,
Hope this works…
Regards,
BRIAN
/* Program Name: FPH_controller (Free Pool Heater)
- Rev 0.0
- Use: Controls pool related hardware
- Date: 11-Mar-2018
- Developers: Rob and Dad
-
- Time-of Day SEQUENCER. Supports the following MODES
-
MODE=VACUUM (1)
-
TIME: 00:00:00 to 01:59:59
-
PUMP at Speed-1 (RELAY_1)
-
VACUUM Valve OPEN (RELAY_3)
-
MODE=SKIM (2)
-
TIME: 00:02:00 to 03:59:59
-
PUMP at Speed-2 (RELAY_2)
-
SKIMMER Valve OPEN (RELAY_4)
-
MODE=FILTER (3)
-
TIME: 00:04:00 to 11:59:59
-
PUMP at Speed-1 (RELAY_1)
-
VACUUM and SKIMMER Valves CLOSED
-
MODE=IDLE (4)
-
TIME: 12:00:00 to 23:59:59
-
PUMP OFF
-
VACUUM and SKIMMER Valves CLOSED
-
- Hardware:
-
Arduino Uno
-
Smartthings Shield
-
Relay Shield (4 x 3A slave relays)
-
4 x 24V slave relays
-
5V power supply
-
24V transformer
-
2 x DS18S20 thermometers (-50C to 125C)
-
2 x Pressure/Vacuum transducers (+30 to -14.5 PSI)
*/
// Inluide LIBRARIES. Note DallasTemperature library, for DS18S20 thermometers, includes oneWire
#include <DallasTemperature.h>
#include <Wire.h>
#include <SD.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Use I/O pin 10 for temp sensor pull-up, 10Kohm
#define ONE_WIRE_BUS 10
// 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);
RTC_DS1307 RTC;
// set to ‘true’ to get debug info in serial monitor
boolean debug = true;
boolean toggle = false;
// set MODE depending on time. PreviousMode stores MODE for previous pass through the main loop
int mode = 0;
int previousMode = 0;
// define digital pins on UNO controlling the 5V slave relays on RELAY SHIELD
int RELAY_1 = 7; // PUMP Speed-1
int RELAY_2 = 6; // PUMP Speed-2
int RELAY_3 = 5; // VALVE-1 (VACUUM)
int RELAY_4 = 4; // VALVE-2 (SKIMMER)
// pressure sensor parameters, offset and calibration
int i;
int sensorValue0, sensorValue1 = 0;
int min_sensorValue0, min_sensorValue1 = 999;
int max_sensorValue0, max_sensorValue1 = -999;
float total_sensorValue0, total_sensorValue1 = 0;
float average_sensorValue0, average_sensorValue1;
float pressure0, pressure1;
float ADC_offset0 = 377; // ADC reading for 0.0PSI
float ADC_calibration0 = 0.0543; // PSI per ADC step;
float ADC_offset1 = 377; // ADC reading for 0.0PSI
float ADC_calibration1 = 0.0543; // PSI per ADC step;
// temperature sensor parameters
float temperature0, temperature1;
// ---------- setup ----------
void setup(void) {
Serial.begin(9600);
sensors.begin();
Wire.begin();
RTC.begin();
lcd.init(); lcd.init(); // initialize LCD (not sure whyt we need to do twice)
// check to see if RTC is running
if (!RTC.isrunning()) {
Serial.println(“RTC is NOT running”);
}
// set RTC from LAPTOP
//RTC.adjust (DateTime(DATE, TIME));
if (debug == true) {
Serial.print("INITIALIZING RTC @ ");
PrintDateTime();
}
// initialize the digital pins controlling the SLAVE RELAYS as outputs
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
pinMode(RELAY_4, OUTPUT);
// turn all realys off
digitalWrite(RELAY_1, LOW); // turn RELAY-1 OFF
digitalWrite(RELAY_2, LOW); // turn RELAY-2 OFF
digitalWrite(RELAY_3, LOW); // turn RELAY-3 OFF
digitalWrite(RELAY_4, LOW); // turn RELAY-4 OFF
// update display template
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“FPH Controller”);
lcd.setCursor(0,1);
lcd.print(“Mode=”);
lcd.setCursor(9,1);
lcd.print(“Time=”);
lcd.setCursor(16,1);
lcd.print(":");
lcd.setCursor(0,2);
lcd.print(“P0/P1:”);
lcd.setCursor(0,3);
lcd.print(“T0/T1:”);
lcd.setCursor(7,2);
lcd.print(pressure0,1);
lcd.setCursor(14,2);
lcd.print(pressure1,1);
lcd.setCursor(7,3);
lcd.print(temperature0,1);
lcd.setCursor(14,3);
lcd.print(temperature1,1);
}
// ---------- main program loop ----------
void loop(void) {
// check time to see which MODE we are int
DateTime now = RTC.now();
// set mode to suit time
if (now.hour() < 2) mode = 1;
if (now.hour() < 4 && now.hour() >= 2) mode = 2;
if (now.hour() < 12 && now.hour() >= 4) mode = 3;
if (now.hour() > 12) mode = 4;
if (debug == true) {
PrintDateTime();
Serial.print("CURRENT MODE = “);
Serial.print(mode);
Serial.print(”, PREVIOUS MODE = ");
Serial.println(previousMode);
}
// collect data from pressure sensors
total_sensorValue0 = 0;
total_sensorValue1 = 0;
// read the input on analog pin A1 10 times:
for (i = 0; i < 10; i++) {
sensorValue0 = analogRead(A0);
sensorValue1 = analogRead(A1);
if (sensorValue0 > max_sensorValue0) max_sensorValue0 = sensorValue0;
if (sensorValue0 < min_sensorValue0) min_sensorValue0 = sensorValue0;
if (sensorValue1 > max_sensorValue1) max_sensorValue1 = sensorValue1;
if (sensorValue1 < min_sensorValue1) min_sensorValue1 = sensorValue1;
total_sensorValue0 = total_sensorValue0 + sensorValue0;
total_sensorValue1 = total_sensorValue1 + sensorValue1;
delay(100);
}
average_sensorValue0 = total_sensorValue0/10;
average_sensorValue1 = total_sensorValue1/10;
if (debug == true) {
Serial.println(total_sensorValue0);
Serial.println(average_sensorValue0);
}
// convert ADC value to PSI
pressure0 = (average_sensorValue0 - ADC_offset0)* ADC_calibration0;
pressure1 = (average_sensorValue1 - ADC_offset1)* ADC_calibration1;
Serial.print("Pressure0 = ");
Serial.print(pressure0,1);
Serial.println(“PSI”);
// issue a global temperature request to all devices on the bus
sensors.requestTemperatures();
temperature0 = sensors.getTempFByIndex(0);
temperature1 = sensors.getTempFByIndex(1);
if (mode = 1) { // VACUUM
digitalWrite(RELAY_1, HIGH); // turn RELAY-1 on
digitalWrite(RELAY_2, LOW); // turn RELAY-2 off
digitalWrite(RELAY_3, HIGH); // turn RELAY-3 on
digitalWrite(RELAY_4, LOW); // turn RELAY-4 off
}
if (mode = 2) { // SKIM
digitalWrite(RELAY_1, LOW); // turn RELAY-1 off
digitalWrite(RELAY_2, HIGH); // turn RELAY-2 on
digitalWrite(RELAY_3, LOW); // turn RELAY-3 off
digitalWrite(RELAY_4, HIGH); // turn RELAY-4 on
}
if (mode = 3) { // FILTER
digitalWrite(RELAY_1, HIGH); // turn RELAY-1 on
digitalWrite(RELAY_2, LOW); // turn RELAY-2 off
digitalWrite(RELAY_3, LOW); // turn RELAY-3 off
digitalWrite(RELAY_4, LOW); // turn RELAY-4 off
}
if (mode = 4) { // IDLE
digitalWrite(RELAY_1, LOW); // turn RELAY-1 off
digitalWrite(RELAY_2, LOW); // turn RELAY-2 off
digitalWrite(RELAY_3, LOW); // turn RELAY-3 off
digitalWrite(RELAY_4, LOW); // turn RELAY-4 off
}
// update active part of display
lcd.setCursor(18,0);
if (toggle == true) {
lcd.print(" “);
}
else lcd.print(” ");
toggle = !toggle;
lcd.setCursor(5,1);
lcd.print(mode);
if (now.hour() <10) {
lcd.setCursor(14,1);
lcd.print(“0”);
lcd.setCursor(15,1);
lcd.print(now.hour());
}
else {
lcd.setCursor(14,1);
lcd.print(now.hour());
}
if (now.minute() <10) {
lcd.setCursor(17,1);
lcd.print(“0”);
lcd.setCursor(18,1);
lcd.print(now.minute());
}
else {
lcd.setCursor(17,1);
lcd.print(now.minute());
}
lcd.setCursor(7,2);
lcd.print(pressure0,1);
lcd.setCursor(14,2);
lcd.print(pressure1,1);
lcd.setCursor(7,3);
lcd.print(temperature0,1);
lcd.setCursor(14,3);
lcd.print(temperature1,1);
previousMode = mode;
}
// ---------- functions ----------
void PrintDateTime () {
// read date/time from RTC
DateTime now = RTC.now();
// write to serial monitor
Serial.print(“Time = “);
if (now.hour() < 10) {
Serial.print(“0”);
}
Serial.print(now.hour());
Serial.print(”:”);
if (now.minute() < 10) {
Serial.print(“0”);
}
Serial.print(now.minute());
Serial.print(":");
if (now.second() < 10) {
Serial.print(“0”);
}
Serial.print(now.second(),DEC);
Serial.print("\t");
Serial.print(“Date = “);
if (now.month() < 10) {
Serial.print(“0”);
}
Serial.print(now.month());
Serial.print(”/”);
if (now.day() < 10) {
Serial.print(“0”);
}
Serial.print(now.day());
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.println("");
}