Hello everyone!
I am new to this forum page
I was making a project that is smart level water indicator in which i am using arduino UNO and a 16x2 LiquidcrytsalI2C display and also a JSN SR04T-v3.0 Waterproof Ultrasonic sensor so i want to basically measure the water level in the tank and want to cut off from the motor when the level reaches 100% and so the main problem is i have written a code that i will also provide here but the readings are not accurate and the value differs , i want a help from this forum to please rectify my code or provide me a sampe code that works like the same
Thank You
The Code
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD with its address and dimensions
const int trigPin = 3; // Ultrasonic sensor trig pin
const int echoPin = 2; // Ultrasonic sensor echo pin
const int setVolume = 500; // Set value for tank volume in centimeters
const float tankHeight = 200.0; // Height of the tank in centimeters (adjust as needed)
bool kelvinDisplayed = false; // Flag to track if Kelvin Electronics animation has been displayed
NewPing sonar(trigPin, echoPin, tankHeight); // Initialize the NewPing instance with the sensor pins and tank height
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
// Check if Kelvin Electronics animation has been displayed
if (!kelvinDisplayed) {
// Slide "Kelvin Electronics" animation once
slideAnimation("Kelvin Electronics ");
kelvinDisplayed = true; // Set flag to true after displaying animation
}
// Measure distance using ultrasonic sensor and display PV, SV, and percentage values
measureAndDisplay();
}
void slideAnimation(String text) {
// Slide "Kelvin Electronics" animation once
for (int i = 0; i <= text.length() + 16; i++) {
// Clear the display buffer
lcd.clear();
// Calculate the starting position for the text
int startPos = max(0, i - 16); // Ensure text starts at column 0
String displayText = text.substring(startPos, min(i, text.length())); // Extract text to display
// Set cursor to the beginning of the first row
lcd.setCursor(0, 0);
// Print the text on the LCD
lcd.print(displayText);
// Delay for smooth scrolling
delay(150);
}
}
void measureAndDisplay() {
// Measure distance using ultrasonic sensor
unsigned int distance = sonar.ping_cm();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Calculate percentage of SV value
float svPercentage = (distance / tankHeight) * 100;
// Display PV (Set Value) and SV (Process Variable) in centimeters
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PV: ");
lcd.print(setVolume);
lcd.print("cm");
lcd.setCursor(0, 1);
lcd.print("SV: ");
lcd.print(distance);
lcd.print("cm");
// Display SV Percentage
lcd.setCursor(10, 1);
lcd.print("SV%: ");
lcd.print(svPercentage);
lcd.print("%");
// Delay before repeating the loop
delay(1000); // Adjust delay as needed
}
Here the Pv is fixed value and it is the set volume and SV is the value from the sensor and then i want to show the percentage level too