PoolController Code v0.7

/*
this is a pool control program for dual speed motors and gas heater no direct
control of the heater implemented in this version. The system tests in and out
water temperature for the heater and accordingly adjusts the pump speed, by 
selecting the appropriate speed circuit on the motor

version 0.7 changes
	Separated lcd updates into a separate subroutine
	
version 0.6 changes
	re-added sensor update line which had been dropped in a previous version
	Updated error message on start fail
	Updated heater-on logic to wait 3 secs. Still needs to be reviewed.

version 0.5 changes
	changed motoroff logic to activate when not the current expected state
	changed heater-on logic to simply wait for 30 seconds before going to high speed

version 0.4 changes
	added delay when changing from one speed to another
	added logic to let heater to run longer (up to 10C higher) before
		activating high speed to prevent hysterisis
	removed redundant commentary

version 0.3 changes
	removed all serial service. now rely only on lcd for output/data
	reduced temp delta from 2 to 1 deg C
	cleaned out abandoned code
	updated comments
*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define ONE_WIRE_BUS 7 //pin 7 for onewire bus
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress intakeTemp, outputTemp;
LiquidCrystal lcd(12,11,10,9,8,6);

// First setup controls

int speedpin = 2; //switch input, high is max speed, low is auto set
int maintpin = 3; //switch input, low is run mode (all off), high is maintenance mode
int MotSpeed = 4; //output, low is low speed, high is high speed
int MotRun = 5; //output, low is motor off, high is motor active
int motState = 0; // 0=off, 1=low speed, 2=high speed; to identify current configured motor speed
String SpeedStatus;
String ModeInf;
float ColdTemp;
float HotTemp;

void setup() {
  lcd.begin(16,2);
  pinMode(speedpin, INPUT);
  pinMode(maintpin, INPUT);
  pinMode(MotSpeed, OUTPUT);
  digitalWrite(MotSpeed, LOW);
  pinMode(MotRun, OUTPUT);
  digitalWrite(MotRun, LOW);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("PoolMan v0.7 ");
  lcd.setCursor(0,1);
  lcd.print("Copyright 2014");
  delay(3000);
  lcd.setCursor(0,0);
  lcd.print("Elmars Ositis");
  delay(4000); 
  sensors.begin();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Locating devices...");
  lcd.setCursor(0,1);
  lcd.print("Found ");
  lcd.setCursor(7,1);
  lcd.print(sensors.getDeviceCount(), DEC);
   delay(2000);
  if (!sensors.getAddress(intakeTemp, 0)) {
    lcd.setCursor(0,1);
    lcd.print("Miss Cold Therm");
    delay(2000);
    SetupFail();
  }
  if (!sensors.getAddress(outputTemp, 1)) {
    lcd.setCursor(0,1);
    lcd.print("Miss Hot Therm");
    delay(2000);
    SetupFail();
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Intake therm OK");
  lcd.setCursor(0,1);
  lcd.print("Output therm OK");
  delay(4000);
  lcd.clear();
}

void loop() {
  sensors.requestTemperatures(); // restored in rev 0.6
  ColdTemp = sensors.getTempC(intakeTemp);
  HotTemp = sensors.getTempC(outputTemp);
  boolean maintstate = digitalRead(maintpin);
  boolean speedstate = digitalRead(speedpin);
  if (maintstate == HIGH) {
    motoroff();
	motState = 0;
	ModeInf="Maintenance";
  }
  if (speedstate == HIGH && maintstate == LOW) {
    maxspeed();
	ModeInf="Max Perform";
    }
  if (speedstate == LOW && maintstate == LOW) {
    automode();
    }
  LCDupdate();  
  delay(500);
}

// turns off all motor function
void motoroff() {
  digitalWrite(MotRun, LOW);
  SpeedStatus="OFF";
  LCDupdate();
  delay(500);
}

// enables motor
void motoron() {
  digitalWrite(MotRun, HIGH);
}
// In order to allow relays to change state in a timely manner,
// it is necessary to implement an all-stop between speeds. This
// helps prevent both motor windings being active at the same time.

// sets motor speed to high
void maxspeed() {
	if (motState != 2) {
		motoroff();
		}
  digitalWrite(MotSpeed, HIGH);
//  lcd.setCursor(4,1);
//  lcd.print("HIGH");
  SpeedStatus="HIGH";  
  delay(200);
  motoron();
  motState = 2;
}

// sets motor speed to low
void ecospeed() {
	if (motState != 1) {
		motoroff();
		}
  digitalWrite(MotSpeed, LOW);
  SpeedStatus="ECO";  
  delay(200);
  motoron();
  motState = 1;
}

// run pump in most economic mode. low when heater off
// high when heater on.
void automode() {
  float tempCdelta = (sensors.getTempC(outputTemp) - sensors.getTempC(intakeTemp));
  if (tempCdelta > 1) {
	ModeInf="Heater ON";
	delay(3000); // 30 secs way too long here reduced to 3secs rev0.6
	maxspeed();
    }
    else { 
    ecospeed();
	ModeInf="ECO Mode";
  }
}

void LCDupdate() {
	lcd.clear();
	lcd.setCursor(0,0);
	lcd.print(ModeInf);
	lcd.setCursor(13,0);
	lcd.print(ColdTemp);
	lcd.setCursor(15,0);
	lcd.print("C");
	lcd.setCursor(0,1);
	lcd.print("SPD:");
	lcd.print(SpeedStatus);
	lcd.setCursor(13,1);
	lcd.print(HotTemp);
	lcd.setCursor(15,1);
	lcd.print("C");
}
	
// go into this loop when the startup config is wrong
void SetupFail() {
  lcd.clear();
  int TheEnd = 0;
  lcd.setCursor(0,0);
  lcd.print("Failed to boot");
  lcd.setCursor(0,1);
  lcd.print("Check Sensors");
  while(TheEnd < 1) {
    delay(5000);
  }
}

Advertisements

One thought on “PoolController Code v0.7

  1. Pingback: Pool Pump Controller – Update 2 | Elmars Tinkering Again

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s