Smart talking plan code

 #include <Wire.h>

#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 16, 2);


// --- Sensor Pins ---

int soilPin = A0;

int lightPin = A1;


// --- LED Pins ---

int waterLED = 10;     // Blue LED

int sunlightLED = 11;  // Yellow LED

int happyLED = 12;     // Green LED


// --- Voice Modules ---

int waterVoicePin = 7;     // ISD1820 for "Need Water"

int sunlightVoicePin = 8;  // ISD1820 for "Need Sunlight"


// --- Buzzer ---

int buzzerPin = 4;


// --- Thresholds ---

int soilThreshold = 600;

int lightThreshold = 500;


// --- Flags ---

bool waterPlayed = false;

bool sunlightPlayed = false;


void setup() {

  Serial.begin(9600);

  lcd.init();

  lcd.backlight();


  pinMode(waterLED, OUTPUT);

  pinMode(sunlightLED, OUTPUT);

  pinMode(happyLED, OUTPUT);


  pinMode(waterVoicePin, OUTPUT);

  pinMode(sunlightVoicePin, OUTPUT);

  pinMode(buzzerPin, OUTPUT);


  digitalWrite(waterVoicePin, LOW);

  digitalWrite(sunlightVoicePin, LOW);


  lcd.setCursor(0, 0);

  lcd.print("Talking Plant 2.0");

  delay(2000);

  lcd.clear();

}


void loop() {


  int soilValue = analogRead(soilPin);

  int lightValue = analogRead(lightPin);


  Serial.print("Soil: ");

  Serial.print(soilValue);

  Serial.print(" | Light: ");

  Serial.println(lightValue);


  lcd.clear();


  // --- NEEDS WATER ---

  if (soilValue > soilThreshold) {

    lcd.setCursor(0, 0);

    lcd.print("I need water!");


    digitalWrite(waterLED, HIGH);

    digitalWrite(sunlightLED, LOW);

    digitalWrite(happyLED, LOW);


    if (!waterPlayed) {

      playWaterVoice();

      beep();

      waterPlayed = true;

      sunlightPlayed = false;

    }

  }


  // --- NEEDS SUNLIGHT ---

  else if (lightValue > lightThreshold) {

    lcd.setCursor(0, 0);

    lcd.print("Need sunlight!");


    digitalWrite(sunlightLED, HIGH);

    digitalWrite(waterLED, LOW);

    digitalWrite(happyLED, LOW);


    if (!sunlightPlayed) {

      playSunlightVoice();

      beep();

      sunlightPlayed = true;

      waterPlayed = false;

    }

  }


  // --- HAPPY PLANT ---

  else {

    lcd.setCursor(0, 0);

    lcd.print("I'm happy :)");


    digitalWrite(happyLED, HIGH);

    digitalWrite(waterLED, LOW);

    digitalWrite(sunlightLED, LOW);


    waterPlayed = false;

    sunlightPlayed = false;

  }


  delay(1000);

}


// --- Beep Sound ---

void beep() {

  tone(buzzerPin, 1500, 150);  

  delay(200);

}


// --- Water voice playback ---

void playWaterVoice() {

  digitalWrite(waterVoicePin, HIGH);

  delay(200);

  digitalWrite(waterVoicePin, LOW);

}


// --- Sunlight voice playback ---

void playSunlightVoice() {

  digitalWrite(sunlightVoicePin, HIGH);

  delay(200);

  digitalWrite(sunlightVoicePin, LOW);

}

Comments

Popular posts from this blog

ESP8266 NodeMCU Setup & Programming – The Only Guide You Need!

Control LED from Your Phone in Just Minutes!💡📲 | ESP8266 + Blynk for Beginners