/*
 * 
 * Watering System v2.0
 * 
 * Resources:
 * 
 * LCD Wiring:
 * https://wiki.52pi.com/index.php?title=1602_Serial_LCD_Module_Display_SKU:Z-0234
 * 
 * LCD Libraries:
 * http://www.ardumotive.com/i2clcden.html
 * 
 * Sensor Values
 * Dry: (520 - 430]
 * Wet: (430  - 350]
 * Water: (350  - 260]
 * 
 * 
*/
//-- Libraries --------------------------------------------------------------------------
#include 
#include 
//-- Define pin setup -------------------------------------------------------------------
int outNotify01 = 13; // output of buzzer and LED
int driveOutput01 = 2; //Water Pump Swicth Output pin
int driveOutput02 = 3;
int driveOutput03 = 4;
int driveOutput04 = 5;
//-- Global addresses -------------------------------------------------------------------
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//-- Global Variables -------------------------------------------------------------------
int debugCount = 0;
int intSensorCount = 4;
const int intNeedWaterLevel = 430;
const int intSensorCheckCount = 9;
void setup() {
  pinMode(outNotify01, OUTPUT);
  
  pinMode(driveOutput01, OUTPUT);digitalWrite(driveOutput01, HIGH );
  pinMode(driveOutput02, OUTPUT);digitalWrite(driveOutput02, HIGH );
  pinMode(driveOutput03, OUTPUT);digitalWrite(driveOutput03, HIGH );
  pinMode(driveOutput04, OUTPUT);digitalWrite(driveOutput04, HIGH );
  lcd.begin(16,2);   // iInit the LCD for 16 chars 2 lines
  lcd.backlight();   // Turn on display, lcd.noBaklight() to turn off
  
}
void loop() {
 int sensorVal01 = 0;
 int sensorVal02 = 0;
 int sensorVal03 = 0;
 int sensorVal04 = 0;
  
 for (int i = 0; i < intSensorCheckCount; i++) {
  sensorVal01 = analogRead(A0); // define analog pin locations
  lcd.setCursor(0, 0);
  lcd.print("SENSOR 01: " + String(sensorVal01) + "." + String(i));
  if(sensorVal01 >= intNeedWaterLevel){
       lcd.setCursor(0, 1);
       lcd.print("I need water");
       digitalWrite(outNotify01, HIGH);delay(100);digitalWrite(outNotify01, LOW);
       digitalWrite(driveOutput01, LOW );
    
  } else {
      // turn off LED and buzzer ----------
      digitalWrite(outNotify01, LOW);
      // display status -------------------
      lcd.setCursor(0, 1);
      lcd.print("I am happy :)");
      // turn off pump --------------------
      digitalWrite(driveOutput01, HIGH );
      // small delay, break out of loop ---
      delay(2000);
      break;
    
   }
  // delay until next check
  delay(1500);
 }
 subDoSensorDelay();
 for (int i = 0; i < intSensorCheckCount; i++) {
  sensorVal02 = analogRead(A1);
  lcd.setCursor(0, 0);
  lcd.print("SENSOR 02: " + String(sensorVal02) + "." + String(i));
  if(sensorVal02 >= intNeedWaterLevel){
       lcd.setCursor(0, 1);
       lcd.print("I need water");
       digitalWrite(outNotify01, HIGH);delay(100);digitalWrite(outNotify01, LOW);
       digitalWrite(driveOutput02, LOW );
    
  } else {
      digitalWrite(outNotify01, LOW);
      
      lcd.setCursor(0, 1);
      lcd.print("I am happy :)");
      digitalWrite(driveOutput02, HIGH );
      delay(2000);
      break;
    
   }
  delay(1500);
 }
 subDoSensorDelay();
 for (int i = 0; i < intSensorCheckCount; i++) {
  sensorVal03 = analogRead(A2);
  lcd.setCursor(0, 0);
  lcd.print("SENSOR 03: " + String(sensorVal03) + "." + String(i));
  if(sensorVal03 >= intNeedWaterLevel){
       lcd.setCursor(0, 1);
       lcd.print("I need water");
       digitalWrite(outNotify01, HIGH);delay(100);digitalWrite(outNotify01, LOW);
       digitalWrite(driveOutput03, LOW );
    
  } else {
      digitalWrite(outNotify01, LOW);
      
      lcd.setCursor(0, 1);
      lcd.print("I am happy :)");
      digitalWrite(driveOutput03, HIGH );
      delay(2000);
      break;
    
   }
  delay(1500);
 }
 subDoSensorDelay();
 for (int i = 0; i < intSensorCheckCount; i++) {
  sensorVal04 = analogRead(A3);
  lcd.setCursor(0, 0);
  lcd.print("SENSOR 04: " + String(sensorVal04) + "." + String(i));
  if(sensorVal04 >= intNeedWaterLevel){
       lcd.setCursor(0, 1);
       lcd.print("I need water");
       digitalWrite(outNotify01, HIGH);delay(100);digitalWrite(outNotify01, LOW);
       digitalWrite(driveOutput04, LOW );
    
  } else {
      digitalWrite(outNotify01, LOW);
      
      lcd.setCursor(0, 1);
      lcd.print("I am happy :)");
      digitalWrite(driveOutput04, HIGH );
      delay(2000);
      break;
    
   }
  delay(1500);
 }
 // final delay before looping again
 subDoSensorDelay();
}
//-- Helper Functions ----------------------------------------------------------------------
void subDoSensorDelay(){
 digitalWrite(driveOutput01, HIGH );
 digitalWrite(driveOutput02, HIGH );
 digitalWrite(driveOutput03, HIGH );
 digitalWrite(driveOutput04, HIGH );
 lcd.clear();
 delay(3000);
}
void subDoExit(){
  lcd.clear(); // clear display
  lcd.noBacklight(); // turn off display
}