View Single Post
Old 08-15-14, 01:15 PM   #202
buffalobillpatrick
Master EcoRenovator
 
Join Date: Mar 2014
Location: Florissant, Colorado
Posts: 599
Thanks: 814
Thanked 59 Times in 55 Posts
Default

Added: Read_10K_NTC_C
guessed at A0 & A1 for evap & cond input pins, change as required

Ormston_Rev03
Code:
                                    //Got tired of counting zeros
#define  One_Sec              1000
#define  Half_Min            30000
#define  One_Min             60000
#define  Two_Min            120000
#define  Five_Min           300000
#define  Ten_Min            600000
#define  Thirty_Min        1800000

      
#define evap_Sensor           A0   //???
#define cond_Sensor           A1 


int evap_Temp, cond_Temp;  //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 blocking_Time;          // Delay Time
unsigned long hpStart_Time;        // Time HP cycle started
unsigned long hpStop_Time;        // 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


// ********************Temperature measurement starts here**************
void readTemps()
{ //read all temperatures from thermistors in C

  evap_Temp   =  (Read_10K_NTC_C (evap_Sensor)); 
  cond_Temp   =  (Read_10K_NTC_C (cond_Sensor));
}

// ********************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 (One_Sec); //Wait 1 second
  noInterrupts(); //Disable interrupts
  HpLPM = (NbTopsFan / 2.75);
  HpLPM = (NbTopsFan1 / 2.75);
}
// ********************Flow meter reading ends here**************




int Read_10K_NTC_C (int which_sensor)
{   
  #define sample_cnt 30
  
  float alpha =   0.9;     // factor to tune
  float average = 0.0;
  float steinhart;
  
  int I;
 
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000

// TEMP. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25

// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3892

// the value of the series resistor
#define SERIESRESISTOR 10000
  
  for (I=0; I< sample_cnt; I++) 
  {  
  average =  alpha * analogRead(which_sensor) + (1-alpha) * average;
  delay(10);
  }
 
  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C
  
//  return (round(steinhart * 1.8) + 32);       // round to whole number 
                                              // & convert to F            

return (round(steinhart));         // round to whole number, return int

}  // end of Read_10K_NTC





void setup() {
  pinMode(evap_Sensor, INPUT); //init
  pinMode(cond_Sensor, INPUT); //init
  
  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); 
  blocking_Time = millis(); 

}

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

  unsigned long current_Time = millis();
  
  if ((demand == true) && (blocking_Time >= (current_Time - Two_Min))) 
  //Wait 2 min.
  {
  if (runHP == false) hpStart_Time = current_Time;
  
  runHP = true;
  }
  else 
  {
  if (runHP == true) hpStop_Time = current_Time;
  
  runHP = false;
  }



  //*****HP startup cycle********
  if (runHP == true) runInPump = true;
  
  if ((runHP == true) 
     && (hpStart_Time >= (current_Time - Half_Min)  //Delay compressor start 30s
     && (HpLPM > minLPM)))                          //after starting in pump
  runCompressor = true;
     
  if ((runHP == true) 
     && (hpStart_Time >= (current_Time - One_Min))) 
  {
  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) 
     && (hpStop_Time >= (current_Time - Two_Min))) 
  runInPump = false;
     
  if ((runHP == false) 
     && (hpStop_Time >= (current_Time - Five_Min)))   //delay 5 min.
  {
  runOutPump = false;
  blocking_Time = current_Time;
  ledState = false;               //turn off pin 13 led after shutdown of HP
  }
 
 
  
  //***Check flow rates above min***
  if (HpLPM < minLPM || HpLPM1 < minLPM) 
  {
  runCompressor = false;
  
  if (runHP == true) hpStop_Time = current_Time;
  
  blocking_Time = (current_Time + Ten_Min); //prevent cycle restart for 10m
  runHP = false;
  }



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


}

Last edited by buffalobillpatrick; 08-15-14 at 01:23 PM..
buffalobillpatrick is offline   Reply With Quote