Hi all,
I'm currently working on a mini coolant system model project. A little background; the model consists of 3 temperature sensors (one at a secondary pump on a parallel line, a second at a radiator inlet, and a third at a radiator outlet) and two flow meters (one on the parallel line, another on the main line).
The temperature sensors are DS18B20s and the flow meters are these guys: G1/2 Water Flow sensor. I'm working with an Arduino Uno.
Posted below is my current test code that reads from two DS18B20s and two flow meters (tested with one flow meter though). Its a combination of the temperature sensor code from here and flow sensor code I modified a little bit from this post here (credits to dlloyd).
#include <OneWire.h>
#include <DallasTemperature.h>
// constants
#define ONE_WIRE_BUS 6 // Data bus pin for temperature sensor reading
const byte inputs[] = {2, 3}; // flow sensor input pins
const double pulseConstant[] = {7.5, 7.5}; // flow sensor pulse constants
const byte qty = sizeof(inputs);
OneWire oneWire(ONE_WIRE_BUS); // initialize OneWire to read for reading temperature sensors
DallasTemperature sensors(&oneWire); // directing the temperature sensors' location to the sensor library
// variables
long previousMillis = 0;
long previousMillis2 = 0;
long startTime[qty];
byte inputsState[qty];
byte inputsPrevious[qty];
double pulsePeriod[qty];
double pulseFrequency[qty];
double pulseMultiplier[qty];
double flowRate[qty];
void setup() {
Serial.begin(9600);
Serial.println("Temperature and Flow Sensor Read Out Testing");
sensors.begin(); // initialize the temperature sensor library
for (int i = 0; i < qty; i++) {
pinMode(inputs[i], INPUT_PULLUP); // flow sensor pin locations declared as inputs
pulseMultiplier[i] = 1.0 / (pulseConstant[i]); // multiplier to convert Hall effect sensor frequency into flow rate
}
clr(); // initialize arrays for flow rate calculations
}
void loop()
{
if (millis() - previousMillis2 > 1000) {
tempMeasure(); // read from DS18B20 sensors and print results
previousMillis2 = millis();
startTime[0] = micros();
}
else {
flowMeasure(); // run flow tests, calculations and print results
}
}
// functions -----------------------------------------------------
void flowMeasure() {
for (int i = 0; i < qty; i++) {
inputsState[i] = digitalRead(inputs[i]); // read the inputs
if ((inputsState[i] == 1) && (inputsPrevious[i] == 0)) { // if rising
pulsePeriod[i] = (micros() - startTime[i]) * 0.000001; // test duration (sec)
pulseFrequency[i] = 1 / pulsePeriod[i]; // input frequency (Hz)
flowRate[i] = pulseFrequency[i] * pulseMultiplier[i]; // 1.0 / pulseConstant[i] (L/min)
if (millis() - previousMillis > 250) { // update interval (milliseconds)
Serial.print(i); Serial.print(": ");
Serial.print(pulsePeriod[i], 6); Serial.print(" sec, ");
Serial.print(pulseFrequency[i], 3); Serial.print(" Hz, ");
Serial.print(flowRate[i], 3); Serial.print(" L/min, ");
Serial.println();
previousMillis = millis();
}
startTime[i] = micros();
}
inputsPrevious[i] = inputsState[i];
}
}
void clr() {
for (int i = 0; i < qty; i++) {
startTime[i] = micros();
inputsState[i] = 0;
inputsPrevious[i] = 1;
pulsePeriod[i] = 0;
pulseFrequency[i] = 0;
flowRate[i] = 0;
}
}
void tempMeasure() {
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures from all sensors on the bus
//Serial.println("DONE");
Serial.print("Temperature for Sensor 1 is: ");
Serial.println(sensors.getTempCByIndex(0)); // first sensor on the bus
Serial.print("Temperature for Sensor 2 is: ");
Serial.println(sensors.getTempCByIndex(1)); // second sensor on the bus
}
The problem I'm having is with flow meter output after reading from the temperature sensors. The time it takes to output the temperature readings throws off the pulse period calculation for the flow meter afterwords, which is dependent on the micros() function. I've tried resetting the start time of the pulse period calculation after reading from the temperature sensors but it's thrown out of wack (this little part below:).
if (millis() - previousMillis2 > 1000) {
tempMeasure(); // read from DS18B20 sensors and print results
previousMillis2 = millis();
*****startTime[0] = micros();
}
Without resetting the startTime, you get a really low flow rate reading because the pulse period is long (includes the time to output the temperature readings). Here's a sample output of the code as posted:
Temperature and Flow Sensor Read Out Testing
0: 0.435332 sec, 2.297 Hz, 0.306 L/min,
0: 0.034788 sec, 28.746 Hz, 3.833 L/min,
Temperature for Sensor 1 is: 23.00
Temperature for Sensor 2 is: 23.06
0: 0.000688 sec, 1453.488 Hz, 193.798 L/min,
0: 0.017900 sec, 55.866 Hz, 7.449 L/min,
0: 0.017824 sec, 56.104 Hz, 7.481 L/min,
0: 0.019008 sec, 52.609 Hz, 7.015 L/min,
Temperature for Sensor 1 is: 23.00
Temperature for Sensor 2 is: 23.06
0: 0.007060 sec, 141.643 Hz, 18.886 L/min,
0: 0.023296 sec, 42.926 Hz, 5.723 L/min,
0: 0.023380 sec, 42.772 Hz, 5.703 L/min,
0: 0.023768 sec, 42.073 Hz, 5.610 L/min
Anyone have any idea how I could sort this out? I haven't been able to think of a way around this, or I'm not seeing the logic. Thanks for reading!
