#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and size (0x27 for a common 16x2 display)
volatile unsigned long pulseCountPin2 = 0; // Initialize pulse count variable for pin 2 (P)
volatile unsigned long pulseCountPin3 = 0; // Initialize pulse count variable for pin 3 (S)
unsigned long previousMillis = 0; // Initialize previous time
unsigned long interval = 1000; // Interval to calculate pulses per second
float microlitersPerPulse = 452.0; // Microliters per pulse
float totalGallonsPin2 = 0.0; // Total gallons flowed for pin 2 (P)
float totalGallonsPin3 = 0.0; // Total gallons flowed for pin 3 (S)
float gallonsPerHourPin2 = 0.0; // Flow rate in GPH for pin 2 (P)
float gallonsPerHourPin3 = 0.0; // Flow rate in GPH for pin 3 (S)
float rollingAveragePin2[10] = {0}; // Array to store the last 10 flow rate values for pin 2 (P)
float rollingAveragePin3[10] = {0}; // Array to store the last 10 flow rate values for pin 3 (S)
int currentIndexPin2 = 0; // Index to keep track of the current value for pin 2 (P)
int currentIndexPin3 = 0; // Index to keep track of the current value for pin 3 (S)
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("P GPH: 0.00");
lcd.setCursor(0, 1);
lcd.print("S GPH: 0.00");
Serial.begin(9600); // Initialize serial communication
attachInterrupt(digitalPinToInterrupt(2), countPulsePin2, RISING); // Attach an interrupt to pin 2 (P)
attachInterrupt(digitalPinToInterrupt(3), countPulsePin3, RISING); // Attach an interrupt to pin 3 (S)
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// Calculate pulses per second for both pins
float pulsesPerSecondPin2 = (float)pulseCountPin2 / (float)interval * 1000.0;
float pulsesPerSecondPin3 = (float)pulseCountPin3 / (float)interval * 1000.0;
// Calculate gallons per hour for both pins
gallonsPerHourPin2 = (pulsesPerSecondPin2 * microlitersPerPulse * 0.000264172052) * 3600.0;
gallonsPerHourPin3 = (pulsesPerSecondPin3 * microlitersPerPulse * 0.000264172052) * 3600.0;
// Update the rolling average arrays for both pins
rollingAveragePin2[currentIndexPin2] = gallonsPerHourPin2;
rollingAveragePin3[currentIndexPin3] = gallonsPerHourPin3;
// Calculate the rolling averages for both pins
float sumPin2 = 0.0;
float sumPin3 = 0.0;
for (int i = 0; i < 10; i++) {
sumPin2 += rollingAveragePin2[i];
sumPin3 += rollingAveragePin3[i];
}
float averageGPHPin2 = sumPin2 / 10.0;
float averageGPHPin3 = sumPin3 / 10.0;
// Update the total gallons for both pins
totalGallonsPin2 += (gallonsPerHourPin2 / 3600.0);
totalGallonsPin3 += (gallonsPerHourPin3 / 3600.0);
// Display on the LCD
lcd.setCursor(0, 0);
lcd.print("P GPH: "); // Clear the previous value
lcd.setCursor(7, 0);
lcd.print(averageGPHPin2, 2); // Display with 2 decimal places
lcd.setCursor(0, 1);
lcd.print("S GPH: "); // Clear the previous value
lcd.setCursor(7, 1);
lcd.print(averageGPHPin3, 2); // Display with 2 decimal places
// Reset pulse counts and update the previous time
noInterrupts();
pulseCountPin2 = 0;
pulseCountPin3 = 0;
interrupts();
previousMillis = currentMillis;
// Update the current indices for the rolling average arrays
currentIndexPin2 = (currentIndexPin2 + 1) % 10;
currentIndexPin3 = (currentIndexPin3 + 1) % 10;
}
}
void countPulsePin2() {
// This function is called whenever a pulse is detected on pin 2 (P)
pulseCountPin2++;
}
void countPulsePin3() {
// This function is called whenever a pulse is detected on pin 3 (S)
pulseCountPin3++;
}
assuming the code is right it should be calibrated, these flow sensors came out of a dialysis machine so i would imagine the sticker on the back is right
im not too worried about air in the system because it will be filled 24/7 and i have not sanity checked with a timed measure yet but i will after i eat dinner
After some fine-tuning and testing, I got it all working. I know P and S GPH are off, but they fluctuate a bit, and I'm fine with that.
I ordered an Arduino Nano and a proto board. I'm going to make an enclosure and test it on an old truck I have. I'll make sure the sensor is gas-rated 1000% before I put it in the boat.
If anyone sees anything wrong, let me know; I'm still kind of winging it.
more testing and tweaking and now im waiting on that proto board, the only problem i have is if the GPH's go under 10 they have a small floating point error or something but not too big of a deal