Untitled

 avatar
unknown
plain_text
4 months ago
3.5 kB
1
Indexable
#include <Wire.h>                 // Standard I2C communication library
#include <jm_LiquidCrystal_I2C.h> // jm_LiquidCrystal_I2C library for I2C LCD communication

// Initialize the LCD
jm_LiquidCrystal_I2C lcd; // Declare the LCD object

// Define ultrasonic sensor pins
#define TRIG_PIN 8
#define ECHO_PIN 9

// Define relay module pin
#define RELAY_PIN 7

// Define push button pins
#define BUTTON_ON 10
#define BUTTON_OFF 11

// Variables for ultrasonic sensor
long duration;
float distance;

// Water level thresholds (in cm)
const float LOW_WATER_THRESHOLD = 10.0; // Adjust as needed
const float HIGH_WATER_THRESHOLD = 5.0;  // Adjust as needed

// Relay state
bool relayState = false;

void setup() {
  // Initialize serial communication for debugging
  Serial.begin(9600);

  // Initialize LCD
  lcd.begin(0x27, 20, 4); // Initialize the LCD with address 0x27, 16 columns, and 2 rows
  lcd.backlight();        // Enable the backlight
  lcd.setCursor(0, 0);
  lcd.print("Water Level Sys");
  delay(2000);
  lcd.clear();

  // Set pin modes
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(BUTTON_ON, INPUT_PULLUP);
  pinMode(BUTTON_OFF, INPUT_PULLUP);

  // Turn relay off initially
  digitalWrite(RELAY_PIN, LOW);

  // Display system ready message
  lcd.setCursor(0, 0);
  lcd.print("System Ready");
  delay(2000);
  lcd.clear();
}

void loop() {
  // Measure water level distance
  distance = measureDistance();

  // Display water level on the LCD
  lcd.setCursor(0, 0);
  lcd.print("Water Level: ");
  if (distance == 999.0) {
    lcd.setCursor(0, 1);
    lcd.print("Error          "); // Display error if no valid reading
  } else {
    lcd.setCursor(0, 1);
    lcd.print(distance);
    lcd.print(" cm    "); // Clear trailing characters
  }

  // Manual override using buttons (with debouncing)
  if (digitalRead(BUTTON_ON) == LOW) {
    delay(50); // Debounce delay
    if (digitalRead(BUTTON_ON) == LOW) {
      relayState = true;
    }
  }
  if (digitalRead(BUTTON_OFF) == LOW) {
    delay(50); // Debounce delay
    if (digitalRead(BUTTON_OFF) == LOW) {
      relayState = false;
    }
  }

  // Automatic control based on water level
  if (distance != 999.0) { // Avoid erroneous relay control
    if (distance > LOW_WATER_THRESHOLD) {
      relayState = true;
    } else if (distance < HIGH_WATER_THRESHOLD) {
      relayState = false;
    }
  }

  // Update relay state
  digitalWrite(RELAY_PIN, relayState ? HIGH : LOW);

  // Print data to serial monitor
  Serial.print("Distance: ");
  if (distance == 999.0) {
    Serial.println("Error: No valid reading");
  } else {
    Serial.print(distance);
    Serial.println(" cm");
  }

  delay(500); // Short delay for stability
}

// Function to measure distance using the ultrasonic sensor
float measureDistance() {
  // Send a 10us pulse to the trigger pin
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Measure the duration of the echo pulse
  duration = pulseIn(ECHO_PIN, HIGH, 30000); // Timeout after 30ms

  // If no echo is detected, return a large invalid value
  if (duration == 0) {
    return 999.0; // Error value
  }

  // Calculate distance in cm
  float distance = (duration * 0.034) / 2.0;
  return distance;
}
Editor is loading...
Leave a Comment