/*
Bismillah
Nama : M. Alamsyah
ver : 1.0.0
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Encoder.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int potPin = A0;
const float maxFlow = 30.0; // Set maximum flow rate to 30 L/min
const float maxVolume = 10000.0; // Set maximum total volume to 10000 L
const int updateInterval = 500;
unsigned long previousMillis = 0;
float totalVolume = 0.0;
Encoder encoder(2, 3);
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Flowmeter Simulation:");
display.display();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= updateInterval) {
previousMillis = currentMillis;
int potValue = analogRead(potPin);
// Map potValue to the range 0 to 30
float flow = map(potValue, 0, 1023, 0, maxFlow);
float flowRatePerSecond = flow / 60.0;
totalVolume += flowRatePerSecond * (updateInterval / 1000.0);
// Limit total volume to the maximum value
totalVolume = constrain(totalVolume, 0, maxVolume);
display.clearDisplay();
display.setCursor(0, 16);
display.print("Flow Rate: ");
display.print(flowRatePerSecond, 2); // Display flow rate with 2 decimal places
display.print(" mL/s");
display.setCursor(0, 32);
display.print("Total Volume: ");
display.print(totalVolume, 2); // Display total volume with 2 decimal places
display.print(" mL");
display.display();
}
long encoderPosition = encoder.read();
if (encoderPosition > 0) {
// Perubahan posisi ke kanan
} else if (encoderPosition < 0) {
// Perubahan posisi ke kiri
}
delay(50); // Tambahkan delay untuk mengurangi debounce
}