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

Spent some more time trying to get the thermistor lib used in the sketches above working.
For whatever reason it doesn't work in an Arduino simulator so unless anyone has any objections i'm going to ditch the lib and use more conventional thermistor code.


Steve

Code:
/*Ormston GPHPC Rev02 
D00 - (N/A)
D01 - (N/A)
D02 - Flow Sensor (indoor)
D03 - Flow Sensor (outdoor)
D04 - Call For Heat (+5V)
D05 - Compressor ON
D06 - Pump ON (indoor)
D07 - Pump ON (outdoor)
D08 - Simulate flow sensors(sets flow to 10L/m for testing)
D09 - (N/A)
D10 - (N/A)
D11 - (N/A)
D12 - (N/A)
D13 - LED

A00 - Thermistor (evaporator temperature)
A01 - Thermistor (condenser temperature)
A02 - (N/A)
A03 - (N/A)
A04 - (N/A)
A05 - (N/A)
*/
#include <math.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
byte simulateFlow = false;
float pad = 9850;                       // balance/pad resistor value, set this to
                                        // the measured resistance of your pad resistor
float thermr = 10000;                   // thermistor nominal resistance



// ********************Temperature measurement starts here**************
float Thermistor(int RawADC) {
  long Resistance;  
  float Temp;  // Dual-Purpose variable to save space.

  Resistance=((1024 * pad / RawADC) - pad); 
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;  // Convert Kelvin to Celsius                      
  //temp = (Temp * 9.0)/ 5.0 + 32.0;                  // Convert to Fahrenheit
  return Temp;                                      // Return the Temperature
}

void readTemps() { //read all temperatures from thermistors
  evaporator = Thermistor(analogRead(A0));       // read ADC and  convert it to Celsius
  condensor = Thermistor(analogRead(A1));       // read ADC and  convert it to Celsius
  //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 (09-09-14)