This is what I got before I stopped messing with it, this should be for a 16x2 display but I bought a bigger one
#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 = 454.0; // Microliters per pulse
float gallonsPerHourPin2 = 0.0; // Flow rate in GPH for pin 2 (P)
float gallonsPerHourPin3 = 0.0; // Flow rate in GPH 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;
float pulsesPerSecondPin3 = (float)pulseCountPin3 / (float)interval;
// Calculate gallons per hour for both pins
gallonsPerHourPin2 = (pulsesPerSecondPin2 * microlitersPerPulse) / 1000.0 * 3600.0; // Convert microliters to gallons
gallonsPerHourPin3 = (pulsesPerSecondPin3 * microlitersPerPulse) / 1000.0 * 3600.0; // Convert microliters to gallons
// Display on the LCD
lcd.setCursor(7, 0);
lcd.print(" "); // Clear the previous value
lcd.setCursor(7, 0);
lcd.print(gallonsPerHourPin2, 2); // Display with 2 decimal places
lcd.setCursor(7, 1);
lcd.print(" "); // Clear the previous value
lcd.setCursor(7, 1);
lcd.print(gallonsPerHourPin3, 2); // Display with 2 decimal places
// Reset pulse counts and update the previous time
pulseCountPin2 = 0;
pulseCountPin3 = 0;
previousMillis = currentMillis;
}
}
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++;
}