Advertisement
safwan092

Untitled

Dec 17th, 2023
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2.  
  3. // Initialize the LCD object
  4. LiquidCrystal_I2C lcd(0x27, 16, 2);
  5.  
  6. // Define the pin for the water level sensor
  7. const int waterLevelPin = A0;
  8.  
  9. void setup() {
  10. // Initialize the LCD display
  11. lcd.init();
  12. lcd.backlight();
  13. // Set up the water level sensor pin as an input
  14. pinMode(waterLevelPin, INPUT);
  15.  
  16. // Initialize the serial communication
  17. Serial.begin(9600);
  18.  
  19. // Display an initial message on the LCD
  20. lcd.print("Water Level:");
  21.  
  22. // Set the cursor to the second line
  23. lcd.setCursor(0, 1);
  24.  
  25. // Send an initial message to the serial monitor
  26. Serial.println("Water Level Monitor");
  27. }
  28.  
  29. int waterLevel = 0;
  30.  
  31. void loop() {
  32. lcd.clear();
  33. // Display an initial message on the LCD
  34. lcd.print("Water Level:");
  35.  
  36. // Set the cursor to the second line
  37. lcd.setCursor(0, 1);
  38. int numReadings = 10; // Number of readings to average
  39. int total = 0;
  40.  
  41. // Take multiple readings and accumulate the total
  42. for (int i = 0; i < numReadings; i++) {
  43. waterLevel = analogRead(waterLevelPin);
  44. //Serial.println(waterLevel);
  45. if (waterLevel < 580)
  46. waterLevel = 580;
  47.  
  48. total += waterLevel;
  49.  
  50. // Wait for a moment before taking the next reading
  51. delay(100);
  52. }
  53.  
  54. // Calculate the average
  55. int average = total / numReadings;
  56.  
  57. // Map the average sensor value to the desired range (0-100%)
  58. //int percentage = map(average, 580, 720, 0, 100);
  59.  
  60. // Print the water level percentage on the LCD
  61. lcd.setCursor(1, 1);
  62. lcd.print(average);
  63.  
  64.  
  65.  
  66. // Wait for a moment before taking the next reading
  67. delay(1000);
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement