#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int DT = 2; // GPIO #4-DT on encoder (Output B)
int CLK = 3; // GPIO #5-CLK on encoder (Output A)
float distanceTraveled = 0;
float deltaFlow = 0;
float bentFlow = 0;
float D_machine = 4.041;
float A_machine = PI * D_machine * D_machine / 4.0;
float D_pipe = 3.996;
float AOC = PI * (D_machine * D_machine - D_pipe * D_pipe) / 4.0;
int aState;
int aLastState;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 75;
unsigned long previousMillis = 0;
// Define LCD dimensions and address
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address and the LCD dimensions (20x4)
void setup() {
Serial.begin(9600);
pinMode(CLK, INPUT_PULLUP);
pinMode(DT, INPUT_PULLUP);
aLastState = digitalRead(CLK);
// Initialize LCD
lcd.init();
lcd.backlight();
// Display static text on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("mm/min:");
lcd.setCursor(0, 1);
lcd.print("m:");
lcd.setCursor(0, 2);
lcd.print("m^3/hr:");
lcd.setCursor(0, 3);
lcd.print("L/min:");
}
void loop() {
aState = digitalRead(CLK);
if (aState != aLastState) {
if (millis() - lastDebounceTime > debounceDelay) {
if (digitalRead(DT) != aState) {
distanceTraveled += 00.5236;
} else {
distanceTraveled -= 0.5236;
}
unsigned long currentMillis = millis();
unsigned long timeDiff = currentMillis - previousMillis;
float distancePerPulse = 0.5;
float speed_mm_min = (distancePerPulse / timeDiff) * 60000;
float speed_m_hr = speed_mm_min * 60 / 1000.0;
deltaFlow = speed_m_hr * A_machine;
bentFlow = speed_m_hr * AOC;
float bentFlow_L_min = bentFlow * 1000 / 60;
lastDebounceTime = millis();
previousMillis = currentMillis;
// Print to serial
Serial.print("Speed (mm/min): ");
Serial.println(speed_mm_min);
Serial.print("Dist (m): ");
Serial.println(distanceTraveled / 1000);
Serial.print("DeltaF (m³/hr): ");
Serial.println(deltaFlow);
Serial.print("BentF (L/min): ");
Serial.println(bentFlow_L_min);
// Update LCD with new values
lcd.setCursor(7, 0); // Adjust the position based on your preference
lcd.print(" "); // Round to nearest integer for better fit
lcd.setCursor(7, 0); // Adjust the position based on your preference
lcd.print(round(speed_mm_min)); // Round to nearest integer for better fit
lcd.setCursor(2, 1); // Adjust the position based on your preference
lcd.print(" "); // Round to nearest integer for better fit
lcd.setCursor(2, 1); // Adjust the position based on your preference
lcd.print(distanceTraveled / 1000); // Round to nearest integer for better fit
lcd.setCursor(7, 2); // Adjust the position based on your preference
lcd.print(" "); // Round to nearest integer for better fit
lcd.setCursor(7, 2); // Adjust the position based on your preference
lcd.print(round(deltaFlow)); // Round to nearest integer for better fit
lcd.setCursor(6, 3); // Adjust the position based on your preference
lcd.print(" "); // Round to nearest integer for better fit
lcd.setCursor(6, 3); // Adjust the position based on your preference
lcd.print(round(bentFlow_L_min)); // Round to nearest integer for better fit
}
}
aLastState = aState;
}