Hi,
Very new to Arduino and trying to make this flowmeter work.
Everything works but the flowrate (L/min) is cumulative where I want it to be dynamic i.e rising and falling as it reads the pulses from the flowmeter
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address (0x27) based on your I2C module
const int flowSensorPin = 2; // Flow sensor signal pin
volatile int pulseCount = 0;
float calibrationFactor = 7.5; // Adjust this based on your sensor's specifications
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Flow: ");
lcd.setCursor(0, 1);
lcd.print("Total: ");
pinMode(flowSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
}
void loop() {
static unsigned long lastTime = 0;
unsigned long currentTime = millis();
if (currentTime - lastTime >= 1000) {
float flowRate = pulseCount / calibrationFactor;
float totalVolume = pulseCount / (calibrationFactor * 60); // in liters
lcd.setCursor(6, 0); // Position cursor for flow rate
lcd.print(" "); // Clear previous value
lcd.setCursor(6, 0); // Position cursor again
lcd.print(flowRate, 1);
lcd.print(" L/min");
lcd.setCursor(8, 1); // Position cursor for total volume
lcd.print(" "); // Clear previous value
lcd.setCursor(8, 1); // Position cursor again
lcd.print(totalVolume, 1);
lcd.print(" L");
lastTime = currentTime;
}
}
void pulseCounter() {
pulseCount++;
}
Any guidance greatly appreciated. As I stated I am very new to this.
If you read as @hammy suggests, you will find we like to look at code that hasn't been mangled.
An easy way to do this is to
First use the autoformat tool in the IDE to put your code into a standard format.
Then use the copyforforum tool in the IDE, and come back and just paste into a new sketch, or edit your post - select all the code in it and paste the formatted and for forum code you just got from the IDE.
Ppl will read mangled code, many more ppl will read code that looks like code
Your flowrate and totalvolume calculation need some revision as both are calculated based on pulsecount ( which is incremental and never reset ) and one is the scaled value of the other ( which can't be )
Something like this seems better:
noInterrupts();
int p = pulseCount;
pulseCount = 0;
interrupts();
float flowRate = (p / calibrationFactor);
float totalVolume += p / (calibrationFactor * 60); // in liters
This does not take in consideration that each period is not exactly 1 second ( but avoids divisions )
P.S.
Consider that for few counts per second this is not the best approach
I'm in transit. At a glance, your sketch is orientated to produce a result and publish it periodically.
To get more dynamic, you could reduce the period of the update, with appropriate changes to the calculations to reflect the reduced observation time.
I can't but it might be possible with things you are already doing to measure the time between two successive impulses from the flow meter, which is the extreme and most rapid possible way to see dynamic changes.
Instead of waiting one second or whatever and checking for how many, you cou,d time a certain small number like 1 or 15 pulses.
Instead of X pulses per second, you'd be using X seconds per pulse (or 5 or ten).
What is the frequency of the pulses at the maximum expected flow rate?
You have the flowRate and totalVolume calculations crossed. The interrupt routine counts pulses for one second. You multiply the pulse count by the calibration factor which I presume converts the counts to liters that have been measured during that second. That value should be added to the flow total. It is not the flow rate.
The flow rate in liters per minute is calculated by multiplying the volume you measured in one second by 60.
One thing you should consider is that by multiplying the pulses every second and then adding to the total for your display you are also adding up the accumulated rounding error every second. If you simply keep track of the total pulses counted and then multiply that total count by the cal. factor when you write to the display you will have a more accurate total.