The circuit in question is designed to measure water flow using a water flow meter and display the data on an OLED display. It also includes a rotary encoder to allow user interaction. The Arduino UNO serves as the central microcontroller unit to process the sensor data, manage the display, and read the rotary encoder inputs.
The code for the Arduino UNO microcontroller is written in C++ and is designed to be compiled and uploaded using the Arduino IDE. The code is responsible for initializing the components, reading sensor data from the water flow meter, processing the rotary encoder input, and updating the OLED display with the relevant information.
// Include necessary libraries
#include <Arduino.h>
#include <U8g2lib.h>
#include <PinChangeInterrupt.h>
#include <NewEncoder.h>
// Define pins and variables
byte sensorPin = 7;
byte sensorInterrupt = digitalPinToPCINT(sensorPin);
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
// Function to count pulses from the water flow sensor
void pulseCounter() {
pulseCount++;
}
// Setup function for the gas flow sensor
void gasFlowSetup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
attachPCINT(sensorInterrupt, pulseCounter, FALLING);
}
// Function to read the gas flow rate
void readGasFlow() {
if ((millis() - oldTime) > 1000) {
detachPCINT(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
Serial.print("Flow rate: ");
Serial.print(int(flowRate));
Serial.print("L/min");
Serial.print("\t");
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.println("mL");
Serial.print("\t");
Serial.print(totalMilliLitres / 1000);
Serial.print("L");
pulseCount = 0;
attachPCINT(sensorInterrupt, pulseCounter, FALLING);
}
}
// Additional code for the rotary encoder and OLED display is omitted for brevity
// ...
// Arduino setup function
void setup() {
switchSetup();
displaySetup();
gasFlowSetup();
}
// Arduino main loop function
void loop() {
u8g2.firstPage();
do {
readIntakeSwitch();
readGasFlow();
draw();
} while (u8g2.nextPage());
}
The code above includes the setup and main loop functions, as well as the necessary interrupt service routines to handle the water flow meter's pulse output. It also initializes the OLED display and processes the rotary encoder input. The full code would include additional functions and logic to handle the display and encoder, which are not shown here for brevity.