View Single Post
Old 08-14-14, 07:07 AM   #189
Ormston
Apprentice EcoRenovator
 
Join Date: Mar 2013
Location: UK
Posts: 131
Thanks: 13
Thanked 35 Times in 32 Posts
Default

Try this V0.00001
Made a couple of corrections and added input D8 to simulate flow sensors reading 10L/m for testing.
Run code on simulator and seems OKish except the thermistor code, can't get additional lib's to work in the simulator for some reason.

Code:
#include <Thermistor.h>



double evaporator, condensor;  //Variables for temperatures from thermistors
float HpLPM;                   //Flow rate from flow meter in L/m
float HpLPM1;                   //Flow rate from flow meter in L/m
int minLPM = 3;                //Minimum flowrate to run HP
int minEv = 2;                //Minimum evaporator temp
int maxCon = 65;              //Maximum condensor temp
byte runHP = false;
byte runCompressor = false;
byte runOutPump = false;       // pump to circulate water to house/tank
byte runInPump = false;        //fan in case of ASHP
const int demandPin = 4;       // input connected to external thermostat to request heat
byte demand = false;               // variable for reading if heat required from thermostat
unsigned long compLR;          // Time compressor was last run
unsigned long hpStarted;        // Time HP cycle started
unsigned long hpStopped;        // Time HP cycle stopped
volatile int NbTopsFan; //measuring the rising edges of the signal
volatile int NbTopsFan1; //measuring the rising edges of the signal
int hallsensor = 2; //The pin location of the sensor
int hallsensor1 = 3; //The pin location of the sensor
byte led = 13;
byte ledState = false;
byte comp = 5;
byte inPump = 6;
byte outPump = 7;
unsigned long prevMillis = 0;        //used for led blinking
Thermistor temp(0);
Thermistor temp1(1);
byte simulateFlow = false;

// ********************Temperature measurement starts here**************
void readTemps() { //read all temperatures from thermistors
  evaporator = temp.getTemp();
  condensor = temp1.getTemp();
  //evaporator = 20;
  //condensor = 20;
  }

// ********************Temperature measurement ends here**************

// ********************Flow meter reading starts here**************

void hpFlow () //This is the function that the interupt calls
{
  NbTopsFan++;  //This function measures the rising and falling edge of the hall effect sensors signal
}

void hpFlow1 () //This is the function that the interupt calls
{
  NbTopsFan1++;  //This function measures the rising and falling edge of the hall effect sensors signal
}

void readFlows()
{
  NbTopsFan = 0; //Set NbTops to 0 ready for calculations
  NbTopsFan1 = 0; //Set NbTops to 0 ready for calculations
  interrupts(); //Enables interrupts
  delay (1000); //Wait 1 second
  noInterrupts(); //Disable interrupts
  HpLPM = (NbTopsFan / 2.75);
  HpLPM1 = (NbTopsFan1 / 2.75);
  simulateFlow = digitalRead(8);
  if (simulateFlow == true) {
    HpLPM = 10;
    HpLPM1 = 10;
  }
}
// ********************Flow meter reading ends here**************


void setup() {
  pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
  pinMode(hallsensor1, INPUT); //initializes digital pin 3 as an input
  attachInterrupt(0, hpFlow, RISING); //and the interrupt is attached (1 for Leonardo 0 for mega/uno)
  attachInterrupt(1, hpFlow1, RISING); //and the interrupt is attached (0 for Leonardo 1 for mega/uno)
  pinMode(led, OUTPUT);
  pinMode(comp, OUTPUT);
  pinMode(inPump, OUTPUT);
  pinMode(outPump, OUTPUT);
  pinMode(demandPin, INPUT); 
  compLR = millis();
  pinMode(8, INPUT); //disable flow sensors for testing, high simulates 10L/m

}

void loop() {
  readTemps();
  readFlows();
  demand = digitalRead(demandPin);

  unsigned long tm = millis();
  if ((demand == true) && (compLR <= (tm - 120000))) {
    if (runHP == false) hpStarted = tm;
    runHP = true;
  }
  else {
    if (runHP == true) hpStopped = tm;
    runHP = false;
  }

  //*****HP startup cycle********
  if (runHP == true) runInPump = true;
  if ((runHP == true) && (hpStarted <= (tm - 30000) && (HpLPM > minLPM))) runCompressor = true; //Delay compressor start 30s after starting in pump
  if ((runHP == true) && (hpStarted <= (tm - 60000))) {
    runOutPump = true; //Delay out pump start 60s after starting in pump
    ledState = true; ///turn on pin 13 led after startup of HP
  }
  
  //*****HP shutdown cycle********
  if (runHP == false) runCompressor = false;
  if ((runHP == false) && (hpStopped <= (tm - 120000))) runInPump = false;
  if ((runHP == false) && (hpStopped <= (tm - 300000))) {
    if (runOutPump == true && ledState == true) compLR = tm;
    runOutPump = false;
    ledState = false; ///turn off pin 13 led after shutdown of HP
  }
  
  //***Check flow rates above min***
  if ((runOutPump == true) && HpLPM < minLPM || HpLPM1 < minLPM) {
    runCompressor = false;
    if (runHP == true) {
    hpStopped = tm;
    compLR = (tm + 600000); //prevent cycle restart for 10m
    runHP = false;
    }
  }

 //***Check evaporator/condensor above/below limits*** not intended for defrost purposes, halts system for 30m
  if (evaporator < minEv || condensor > maxCon) {
    runCompressor = false;
    if (runHP == true) hpStopped = tm;
    compLR = (tm + 1800000); //prevent cycle restart for 30m
    runHP = false;
  }
    
  
  digitalWrite(led, ledState);
  digitalWrite(comp, runCompressor);
  digitalWrite(inPump, runInPump);
  digitalWrite(outPump, runOutPump);


}
Ormston is offline   Reply With Quote
The Following User Says Thank You to Ormston For This Useful Post:
buffalobillpatrick (08-14-14)