#include <DHT.h>
#include <LiquidCrystal.h>

// Define pin numbers
#define DHT_PIN 2      // Pin to which DHT22 sensor is connected
#define BUZZER_PIN 7   // Pin to which the buzzer is connected

// Define the type of DHT sensor
#define DHT_TYPE DHT22 // Change to DHT11 if using DHT11 sensor
DHT dht(DHT_PIN, DHT_TYPE);

// Initialize the LCD (pins connected to the LCD: RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

const int dirPin=3;
const int stepPin=3;
const int StepsPerRevolution=200;

void setup() {
  
  



  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  // Initialize the LCD
  lcd.begin(16, 2);
  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  
  // Initialize the DHT sensor
  dht.begin();

  // Set up the buzzer pin
  pinMode(BUZZER_PIN, OUTPUT);

  // Initialize serial communication
  Serial.begin(9600);

  // Set up the pins for the moisture sensor LEDs
  pinMode(12, OUTPUT); // High moisture LED
  pinMode(6, OUTPUT);  // Medium moisture LED
  pinMode(3, OUTPUT);  // Low moisture LED
  
  // Set up the pin for the moisture sensor
  pinMode(A0, INPUT);
}

void loop() {
  
 




  // Read the moisture sensor value
  int moistureValue = analogRead(A0);
  
  // Determine moisture level and set LEDs accordingly
  if (moistureValue >= 100 && moistureValue < 300) {
    digitalWrite(12, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(3, LOW);
    Serial.println("Medium amount of Moisture");
    delay(1000);
    digitalWrite(dirPin, LOW);
  } else if (moistureValue >= 300 && moistureValue < 2000) {
    digitalWrite(12, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    Serial.println("High Moisture");
    digitalWrite(dirPin, LOW);
    delay(1000);
  } else {
    digitalWrite(12, LOW);
    digitalWrite(6, LOW);
    digitalWrite(3, HIGH);
    Serial.println("Low Moisture");
    digitalWrite(dirPin, HIGH);
    for(int x=0;x<StepsPerRevolution;x++) {
      digitalWrite(stepPin,HIGH); // Trigger a step
      delayMicroseconds(2000); // Adjust delay between steps as needed
      digitalWrite(stepPin, LOW);
      delayMicroseconds(2000);
  }

  // Delay for stability
  // delay(1000);
  }

  // Read temperature and humidity from the DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Check if any reads failed and exit early (to try again).
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    lcd.setCursor(6, 0);
    lcd.print("Error  ");
    lcd.setCursor(10, 1);
    lcd.print("Error  ");
    return;
  }

  // Display temperature on the LCD
  lcd.setCursor(6, 0);
  lcd.print(temperature, 1);
  lcd.print("C");

  // Display humidity on the LCD
  lcd.setCursor(10, 1);
  lcd.print(humidity, 1);
  lcd.print("%");

  // Check if temperature is above a threshold value and activate the buzzer
 if (temperature > 30.0 || temperature<0.0) {
    digitalWrite(BUZZER_PIN, HIGH);
    tone(BUZZER_PIN, 10000); // Generate a tone of 1000 Hz
    
} else {
    digitalWrite(BUZZER_PIN, LOW);
    noTone(BUZZER_PIN); // Stop the tone
    
}


  // Delay before next loop iteration
  // delay(1000);
}




A4988