Hello,
I'm trying to measure the flowrate of two different fluids using an arduino uno.
I'm using two common flow sensors, one is wired to digital pin 2 and the other to digital pin 3 (interrupts 0 and 1). Additionally , I'm using an LCD to display the results.
I've browsed the forum for similar projects and I've found some advices to incorporate in my code. The problem is, I'm getting unstable flow results. Do you have any suggestions as to why?
Im attaching my code and the serial monitor plot.
//#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
//#include <LiquidCrystal.h>
//LiquidCrystal lcd//LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int liquidflowPin = 2; //This is the input pin on the Arduino
int airflowPin = 3; //This is the input pin on the Arduino
double airflowRate, liquidflowRate; //This is the value we intend to calculate.
volatile int aircount = 0;
volatile int liquidcount = 0 ; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
unsigned long oldTime;
void setup() {
// put your setup code here, to run once:
pinMode(airflowPin, INPUT); //Sets the pin as an input
pinMode(liquidflowPin, INPUT); //Sets the pin as an input
attachInterrupt(0, liquidFlow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
attachInterrupt(1, airFlow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
Serial.begin(9600); //Start Serial
oldTime = 0;
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Snipe Flow Meter");
lcd.setCursor(0, 1);
lcd.print("STARTING ...");
}
void loop() {
if ((millis() - oldTime) > 1000) // Only process counters once per second
{
detachInterrupt(0);
detachInterrupt(1);
oldTime = millis();
//Start the math
airflowRate = (aircount * 4.5); //Take counted pulses in the last second and multiply by 2.25mL
airflowRate = airflowRate * 60; //Convert seconds to minutes, giving you mL / Minute
airflowRate = airflowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
liquidflowRate = (liquidcount * 3.2); //Take counted pulses in the last second and multiply by 2.25mL
liquidflowRate = liquidflowRate * 60; //Convert seconds to minutes, giving you mL / Minute
liquidflowRate = liquidflowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
unsigned int frac;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("AIR: ");
lcd.print(airflowRate);
lcd.print(" mL/mi");
lcd.setCursor(0, 1);
lcd.print("BLOOD");
lcd.print(liquidflowRate);
lcd.print("mL/mi");
/*
Serial.print("AIR");
Serial.println(airflowRate); //Print the variable flowRate to Serial
Serial.print("FLO");
Serial.println(liquidflowRate); //Print the variable flowRate to Serial
*/
aircount = 0; // Reset the counter so we start counting from 0 again
liquidcount = 0; // Reset the counter so we start counting from 0 again
attachInterrupt(0, liquidFlow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
attachInterrupt(1, airFlow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
}
}
void airFlow()
{
aircount++; //Every time this function is called, increment "count" by 1
}
void liquidFlow()
{
liquidcount++; //Every time this function is called, increment "count" by 1
}
thanks
Ben