// LCD2004 and Pi Pico!
#include "pitches.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int F_v = 10;
int S_s = 11;
int M_s = 12;
int SPEAKER_PIN = 21;
bool a_loop1 ,a_loop2 = true;
int F_value, Soil_Value, motion_value;
void setup() {
  Wire.setSDA(8);
  Wire.setSCL(9);
  Wire.begin();
  pinMode(F_v, INPUT);
  pinMode(S_s, INPUT);
  pinMode(M_s, INPUT);
  pinMode(SPEAKER_PIN, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.begin(0, 2);
  lcd.print("Hello World!");
  lcd.setCursor(2, 1);
  lcd.print("> Pi Pico <");
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  delay(1000);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  delay(2000);
}
void loop()
{
  Sensor_Read();
  if (F_value == 1)
  {
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print("Activating");
    lcd.setCursor(0, 1);
    lcd.print("Motor");
    delay(1000);
    digitalWrite(2, LOW);
    while (a_loop1 == true)
    {
      lcd.clear ();
      lcd.setCursor(0, 0);
      lcd.print("Motor is ON");
      lcd.setCursor(0, 1);
      lcd.print("Filling W-tank");
      // delay(2000); // Adding a delay() here speeds up the simulation
      Sensor_Read();
      if (F_value == 0)
      {
        lcd.clear ();
        lcd.print("Motor is OFF");
        lcd.setCursor(0, 1);
        lcd.print("W-Tank is full");
        digitalWrite(2, HIGH);
        delay(2000); // Adding a delay() here speeds up the simulation
        a_loop1 = false;
      }
    }
    a_loop1 = true;
  }
  if (Soil_Value == 0)
  {
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print("Activating");
    lcd.setCursor(0, 1);
    lcd.print("Tank Valve");
    digitalWrite(3, LOW);
    lcd.clear ();
    while (a_loop2 == true)
    {
      lcd.setCursor(0, 0);
      lcd.print("Valve is ON");
      lcd.setCursor(0, 1);
      lcd.print("Watering Field");
      // delay(2000); // Adding a delay() here speeds up the simulation
      Sensor_Read();
      if (Soil_Value == 1)
      {
        lcd.clear ();
        lcd.print("Valve is Closed");
        lcd.setCursor(0, 1);
        lcd.print("Field fully moist");
        digitalWrite(3, HIGH);
        delay(2000); // Adding a delay() here speeds up the simulation
        a_loop2 = false;
      }
    }
    a_loop2 = true;
  }
  if (motion_value == 1)
  {
    lcd.clear ();
    lcd.print("Birds detected");
    lcd.setCursor(0, 1);
    lcd.print("Activating Repeller");
  gameOver();
  }
}//void loop ENd
void Sensor_Read()
{
  //F_value = 0;
  //Soil_Value = 0;
  F_value = digitalRead(F_v);
  Soil_Value = digitalRead(S_s);
  motion_value = digitalRead(M_s);
}
void gameOver() {
  // Play a Wah-Wah-Wah-Wah sound
  tone(SPEAKER_PIN, NOTE_DS5);
  delay(30);
  tone(SPEAKER_PIN, NOTE_D5);
  delay(30);
  tone(SPEAKER_PIN, NOTE_CS5);
  delay(30);
  /*for (byte i = 0; i < 10; i++) {
    for (int pitch = -10; pitch <= 10; pitch++) {
      tone(SPEAKER_PIN, NOTE_C5 + pitch);
      delay(5);
    }
  }*/
  noTone(SPEAKER_PIN);
  delay(500);
}