#include <OneWire.h>
#include <DallasTemperature.h>
#include <AutoPID.h>
//WiFI
double Temp_desired;
//Hall-effect
byte sensorInterrupt0 = 0; // 0 = digital pin 2
byte sensorInterrupt1 = 1; // 1 = digital pin 3
byte sensorPin2 = 2;
byte sensorPin3 = 3;
float calibrationFactor = 4.5;
volatile byte pulseCount_HW;
volatile byte pulseCount_CW;
unsigned long oldTime;
//Temperature Sensors
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int resT = 12;
//////////////////////////////////////////////////////////////////////////
// Change This to the address specific to your sensor
DeviceAddress Temp_CW ={0x28, 0xC9, 0x21, 0x97, 0x94, 0x08, 0x03, 0x67};
DeviceAddress Temp_Out={0x28, 0xEA, 0x1D, 0x97, 0x94, 0x12, 0x03, 0x26};
DeviceAddress Temp_HW ={0x28, 0x87, 0x3A, 0x97, 0x94, 0x04, 0x03, 0x40};
//////////////////////////////////////////////////////////////////////////
unsigned long time;
//block valve
byte blockPin10 = 10;
//PID
#define OUTPUT_MIN 0
#define OUTPUT_MAX 255
#define KP .12
#define KI .0003
#define KD 0
double temperature, setPoint, outputVal;
AutoPID myPID(&temperature, &setPoint, &outputVal, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);
unsigned long lastTempUpdate;
byte controlPin7 = 7;
double temp_out;
void setup()
{
// Initialize a serial connection for reporting values to the host
Serial.begin(9600);
//Setup for hall-effect flowmeters
pinMode(sensorPin2, INPUT);
digitalWrite(sensorPin2, HIGH);
pinMode(sensorPin3, INPUT);
digitalWrite(sensorPin3, HIGH);
pulseCount_HW = 0;
pulseCount_CW = 0;
oldTime = 0;
attachInterrupt(sensorInterrupt0, pulseCounter_HW, FALLING);
attachInterrupt(sensorInterrupt1, pulseCounter_CW, FALLING);
//Setup for Temperature Sensors
sensors.begin();
sensors.setResolution(Temp_CW, resT);
sensors.setResolution(Temp_Out, resT);
sensors.setResolution(Temp_HW, resT);
//block valve
pinMode(blockPin10, OUTPUT);
digitalWrite(blockPin10, LOW);
//PID
pinMode(controlPin7, OUTPUT);
}
void loop()
{
setPoint = 97;
temperature = 95;
sensors.requestTemperatures();
delay(3000);
pulseCount_HW = 10;
pulseCount_CW = 0;
if(pulseCount_HW > 5 && pulseCount_CW < 5)
{
digitalWrite(blockPin10, HIGH);
}
else
{
digitalWrite(blockPin10, LOW);
}
myPID.run();
analogWrite(controlPin7, outputVal);
}
void fm_hw_loop()
{
// put your main code here, to run repeatedly:
if(millis() - oldTime > 1000)
{detachInterrupt(sensorInterrupt0);
oldTime = millis();
unsigned int frac; //I don't know if this is needed!!
Serial.print("pulseCount_HW is:");
Serial.print("\t");
Serial.print(pulseCount_HW);
Serial.print("/t");
pulseCount_HW = 0;
attachInterrupt(sensorInterrupt0, pulseCount_HW, FALLING);
}
}
void fm_cw_loop()
{
// put your main code here, to run repeatedly:
if(millis() - oldTime > 1000)
{detachInterrupt(sensorInterrupt1);
oldTime = millis();
unsigned int frac; //I don't know if this is needed!!
Serial.print("pulseCount_CW is:");
Serial.print("\t");
Serial.print(pulseCount_CW);
Serial.print("/t");
pulseCount_CW = 0;
attachInterrupt(sensorInterrupt1, pulseCount_CW, FALLING);
}
}
void pulseCounter_HW()
{
pulseCount_HW++;
}
void pulseCounter_CW()
{
pulseCount_CW++;
}
Loading
ds18b20
ds18b20