#include "AiEsp32RotaryEncoder.h"
// Pin assignments based on your hardware design
const int trigPin = 12; // Ultrasonic Trigger Pin
const int echoPin = 14; // Ultrasonic Echo Pin
// Rotary encoder pins (used to simulate flow sensor)
#define ROTARY_ENCODER_DT_PIN 18
#define ROTARY_ENCODER_CLK_PIN 19
#define ROTARY_ENCODER_BUTTON_PIN 21
#define ROTARY_ENCODER_VCC_PIN -1
#define ROTARY_ENCODER_STEPS 4
// Rotary encoder object
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_DT_PIN, ROTARY_ENCODER_CLK_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
long duration; // Variable for Ultrasonic sensor duration
int distance; // Variable for distance measured by the Ultrasonic sensor
unsigned long lastTime; // Time for encoder movement check
unsigned long lastPulseTime = 0; // Time of last pulse
float flowRate = 0; // Flow rate in cm³/s
float pulsesToCm3PerSec = 0.1; // Conversion factor for pulses to cm³/s
// Function for ultrasonic distance to percentage conversion (2 cm = 0%, 400 cm = 100%)
int convertToPercentage(int distance) {
if (distance <= 2) return 0;
if (distance >= 400) return 100;
return map(distance, 2, 400, 0, 100); // Mapping the value between 0 and 100%
}
// Rotary encoder ISR handler
void IRAM_ATTR readEncoderISR() {
rotaryEncoder.readEncoder_ISR();
}
void rotary_loop() {
unsigned long currentTime = millis();
unsigned long deltaTime = currentTime - lastTime;
lastTime = currentTime;
// Update flow rate based on the change in the rotary encoder position
int currentPosition = rotaryEncoder.readEncoder();
static int lastPosition = 0;
int deltaPosition = currentPosition - lastPosition;
lastPosition = currentPosition;
// Calculate flow rate based on the number of pulses (speed of encoder movement)
if (deltaTime > 0) {
if (deltaPosition != 0) {
flowRate = (deltaPosition * pulsesToCm3PerSec) / (deltaTime / 1000.0); // Flow in cm³/s
lastPulseTime = millis(); // Track the time of the last movement
} else if (millis() - lastPulseTime > 1000) {
// Set flowRate to 0 if no pulses were detected for more than 1 second
flowRate = 0;
}
}
// Print flow rate
Serial.print("Flow rate: ");
Serial.print(flowRate);
Serial.println(" cm³/s");
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize rotary encoder
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setBoundaries(-1000, 1000, false); // Min -1000, Max 1000, no wrap-around
rotaryEncoder.setAcceleration(2000); // Enable acceleration for fast turning
// Set up ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Rotary encoder processing
rotary_loop();
// Trigger ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure echo duration
duration = pulseIn(echoPin, HIGH);
// Calculate distance from duration (in cm)
distance = duration * 0.034 / 2;
// Convert distance to percentage (2 cm = 0%, 400 cm = 100%)
int percentage = convertToPercentage(distance);
// Display distance and percentage on Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Percentage: ");
Serial.print(percentage);
Serial.println(" %");
// Delay for a brief moment
delay(100);
}