Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_I2C.h>
- // Initialize the LCD object
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- // Define the pin for the water level sensor
- const int waterLevelPin = A0;
- void setup() {
- // Initialize the LCD display
- lcd.init();
- lcd.backlight();
- // Set up the water level sensor pin as an input
- pinMode(waterLevelPin, INPUT);
- // Initialize the serial communication
- Serial.begin(9600);
- // Display an initial message on the LCD
- lcd.print("Water Level:");
- // Set the cursor to the second line
- lcd.setCursor(0, 1);
- // Send an initial message to the serial monitor
- Serial.println("Water Level Monitor");
- }
- int waterLevel = 0;
- void loop() {
- lcd.clear();
- // Display an initial message on the LCD
- lcd.print("Water Level:");
- // Set the cursor to the second line
- lcd.setCursor(0, 1);
- int numReadings = 10; // Number of readings to average
- int total = 0;
- // Take multiple readings and accumulate the total
- for (int i = 0; i < numReadings; i++) {
- waterLevel = analogRead(waterLevelPin);
- //Serial.println(waterLevel);
- if (waterLevel < 580)
- waterLevel = 580;
- total += waterLevel;
- // Wait for a moment before taking the next reading
- delay(100);
- }
- // Calculate the average
- int average = total / numReadings;
- // Map the average sensor value to the desired range (0-100%)
- //int percentage = map(average, 580, 720, 0, 100);
- // Print the water level percentage on the LCD
- lcd.setCursor(1, 1);
- lcd.print(average);
- // Wait for a moment before taking the next reading
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement