0% found this document useful (0 votes)
3 views

LCD code

Uploaded by

yejixx999
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

LCD code

Uploaded by

yejixx999
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>

// Create an LCD object with I2C address 0x27 (common for many I2C LCDs)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define the analog pins for the sensors


const int sensor1Pin = A0;
const int sensor2Pin = A1;
const int sensor3Pin = A2;

// Define threshold for the sensor3 to determine water presence


const int waterThreshold = 500; // Adjust based on sensor calibration

// Define maximum sensor values observed


const int maxSensorValue = 1023; // Assuming 10-bit ADC resolution

void setup() {
// Initialize the LCD
lcd.begin();
lcd.backlight(); // Turn on the backlight (if applicable)

// Print initial messages to the LCD


lcd.setCursor(0, 0);
lcd.print("S1: % S2: %");
}

void loop() {
// Read the moisture levels from the sensors
int sensor1Value = analogRead(sensor1Pin);
int sensor2Value = analogRead(sensor2Pin);
int sensor3Value = analogRead(sensor3Pin);

// Check if the sensor value is within a reasonable range (e.g., 0 to 1000)


int maxSensorReading = 1000; // Adjust this based on your sensor's expected range
if (sensor1Value > maxSensorReading) sensor1Value = maxSensorReading;
if (sensor2Value > maxSensorReading) sensor2Value = maxSensorReading;

// Convert the sensor readings to percentages


int sensor1Percent = map(sensor1Value, 0, maxSensorReading, 0, 100);
int sensor2Percent = map(sensor2Value, 0, maxSensorReading, 0, 100);

// Clear the first line and update it with the new sensor values
lcd.setCursor(0, 0);
lcd.print("S1:");
lcd.print(sensor1Percent);
lcd.print("% S2:");
lcd.print(sensor2Percent);
lcd.print("%");

// Clear the second line


lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous message

// Check the sensor3 for water presence


lcd.setCursor(0, 1);
if (sensor3Value < waterThreshold) {
lcd.print("Tank: Water OK");
} else {
lcd.print("Tank: No Water");
}

// Wait for a second before updating


delay(1000);
}

You might also like