#include "SoftwareSerial.h"
#include "String.h"
SoftwareSerial mySerial(7, 8);

const int L1 = 5; //Low level sensor connected to pin 2

const int L4 = 2; //High Level Sensor connected to pin 5

const int pump = 11; //The pin to which the pump is connected
const int buzzer = 10; //Th epin to which the buzzer is connected
const int LED = 13;

int L1Status;
int lastL1Status;

int L4Status;
int lastL4Status = LOW;

int iTemp = 0; //Temp int var to be used in timer counter

void setup()
{
  Serial.begin(9600); //initialize the serial communication for debugging
  delay(500);
  Serial.println("Ready");
  mySerial.begin(19200);               // the GSM Shield baud rate
  delay(500);

  pinMode(L1, INPUT);
  pinMode(L4, INPUT);

  pinMode(pump, OUTPUT);
  pinMode(buzzer, OUTPUT);

  pinMode(LED, OUTPUT);
}
void loop()
{
  L1Status = digitalRead(L1); //get the Low level sensor state
  L4Status = digitalRead(L4); //Get the High Level Sensor State

  if (L1Status == LOW) { //if the Tank is empty
    digitalWrite(pump, HIGH); //Turn on the Pump
    digitalWrite(LED, HIGH); //Turn on LED, indicating the Pump is ON
    Serial.println("Pump is ON");
    iTemp = 0;
  }
  if (L4Status == HIGH) {
    digitalWrite(pump, LOW); //Turn off the Pump
    digitalWrite(LED, LOW); //Turn Off the Indicating LED, to show the pump is OFF
    Serial.println("Pump is OFF");
  }

  if ( L4Status != lastL4Status) {  //if the state has changed
    if (L4Status == HIGH) {
      //if the current state is HIGH then HIgh Level Sensor has gone from Off to On
      //The code for this condition //The tank is Full
      //Dial a Missed Call, to indicate the Tank has been filled
      Serial.println("The High Level Sensor Has Toggled from OFF to ON");
      digitalWrite(buzzer, HIGH);
      powerUp();
      delay(100);
      DialMissCall();
      delay(100);
      powerDown();
      digitalWrite(buzzer, LOW);
    }
    lastL4Status = L4Status;
  }
  check_timer();
}
void check_timer() {
  iTemp++;//Incremented at an interval of 1 sec
  delay(1000);
  if (iTemp == 30) //Upper limit of delay to Stop Motor (in secs)
  {
    if (L1Status == LOW) //If the water level has still not increased
      Serial.println("The Water Level is Not Increasing, is the Ground Resovoir Tank empty?");
    digitalWrite(pump, LOW); //Turn off the pump
    digitalWrite(LED, LOW); //Turn Off the Indicating LED, to show the pump is OFF
    Serial.println("The Pump Has Been Turned OFF to Protect it");

    Serial.println("Sending a Message to the Owner about the problem");
    powerUp();
    delay(100);
    SendTextMessage();//Send a warning Test Message
    delay(100);
    powerDown();
    Serial.println("Waiting till a Manual Reset");
    wait();
  }
}

void wait() {
  while (1) {
    delay(1000);
    Serial.println("Is everything All Right !");
    Serial.println("Check the Ground Reservoir, and Reset Manually");
  }
}

void powerUp()//The following code is power up subroutine for Arduino if using software triger:
{
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);
  delay(1000);
  digitalWrite(9, HIGH);
  delay(2000);
  digitalWrite(9, LOW);
  delay(3000);
}

void powerDown()//The following code is power down subroutine for Arduino if using software triger:
{
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);
  delay(1000);
  digitalWrite(9, HIGH);
  delay(2000);
  digitalWrite(9, LOW);
  delay(3000);
}

void SendTextMessage()//this function is to send a sms message
{
  mySerial.print("AT+CMGF=1\r");    //Because we want to send the SMS in text mode
  delay(100);
  mySerial.println("AT + CMGS = \"+919938040894\"");//send sms message, be careful need to add a country code before the cellphone number
  delay(100);
  mySerial.println("Something is Wrong with your Automatic Level Controller! Please Check it Manually.");//the content of the message
  delay(100);
  mySerial.println((char)26);//the ASCII code of the ctrl+z is 26
  delay(100);
  mySerial.println();
}

//this function is to dial a voice call and Dissconnect it after 10 Seconds

void DialMissCall()
{
  mySerial.println("ATD + +919938040894;");//dial the number
  delay(100);
  delay(10000);
  mySerial.println("ATH + 0");
  delay(100);
  mySerial.println();
}