#include <SoftwareSerial.h>

SoftwareSerial gprsSerial(7,8);


#define PUMP_A_STATUS 10
#define PUMP_B_STATUS 11
#define PING_STATUS 12

const byte sump = 3;
const byte biofilter = 4;
byte old_sumpState = HIGH;
byte old_bioState = HIGH;
unsigned long CURRENT_A;
unsigned long PREVIOUS_A;
unsigned long CURRENT_B;
unsigned long PREVIOUS_B;
unsigned long CURRENT_C;
unsigned long PREVIOUS_C;
unsigned long CURRENT_D;
unsigned long PREVIOUS_D;
unsigned long interval = 600000;

int PREV_LEVEL_A = 0;
int PREV_LEVEL_B = 0;


void setup ()
{
  
  Serial.begin(19200);
  gprsSerial.begin(19200); // GPRS shield baud rate
  delay(200);
  gprsSerial.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  delay(200);
  pinMode(
  SMS_START();
}
void loop ()
  {
  // see if switch is open or closed
  byte sumpState = digitalRead (sump);
  
  // has it changed since last time?
  if (sumpState != old_sumpState)
    {
    old_sumpState =  sumpState;  // remember for next time 
    if (sumpState == LOW)
       {
     CURRENT_A=millis();
     SMS_SUMP_HIGH();
     delay (1000); 
     } else 
        {
          PREV_LEVEL_A = 0;
        }
    else
       {
       Serial.println ("Switch opened.");
       }  // end if switchState is HIGH
    }  // end of state change
    
    
     if (digitalRead (sump) == LOW)
     {
     //Serial.println ("SUMP LEVEL HIGH.");
     CURRENT_A=millis();
     SMS_SUMP_HIGH();
     RESET_SUMP();
     delay (1000); 
     } else 
        {
          PREV_LEVEL_A = 0;
        }
byte bioState =digitalRead (biofilter);
     
     if (digitalRead (biofilter) == LOW)
     {
     //Serial.println ("BIOFILTER LEVEL HIGH.");
     CURRENT_B=millis();
     SMS_BIOFILTER_HIGH();
     RESET_BIOFILTER();
     delay (1000); 
     }else 
      {
        PREV_LEVEL_B = 0; 
      }
      
      
       if (digitalRead (PUMP_A_STATUS) == HIGH)
     {
     //Serial.println ("BIOFILTER LEVEL HIGH.");
     CURRENT_B=millis();
     SMS_BIOFILTER_HIGH();
     RESET_BIOFILTER();
     delay (1000); 
     }else 
      {
        PREV_LEVEL_B = 0; 
      }
  }
 
    // end of loop
   void SMS_START()
{
 //Serial.println("Sending Text...");
  gprsSerial.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  delay(100);
  // send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:
  // +15408985543
  gprsSerial.println("AT+CMGS = \"+27829729702\"");
  delay(100);
  gprsSerial.println("SYSTEM START"); //the content of the message
  delay(100);
  gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
  delay(100);
  //gprsSerial.println();
  //Serial.println("Text Sent.");
}

    void SMS_SUMP_HIGH()
{
 if (CURRENT_A-PREVIOUS_A>interval){
 // Serial.println("Sending Text...");
  gprsSerial.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  delay(200);
  // send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:
  // +15408985543
  gprsSerial.println("AT+CMGS = \"+27829729702\"");
  delay(200);
  gprsSerial.println("SUMP LEVEL HIGH"); //the content of the message
  delay(200);
  gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
  delay(200);
  PREVIOUS_A=millis();
  //gprsSerial.println();
  //Serial.println("Text Sent.");
} //else {Serial.println("INTERVAL TOO SHORT TO SEND SMS A");
//}
}

 void SMS_BIOFILTER_HIGH()
{
 if (CURRENT_B-PREVIOUS_B>interval){
 // Serial.println("Sending Text...");
  gprsSerial.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  delay(200);
  // send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:
  // +15408985543
  gprsSerial.println("AT+CMGS = \"+27829729702\"");
  delay(200);
  gprsSerial.println("BIO HIGH"); //the content of the message
  delay(200);
  gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
  delay(200);
  PREVIOUS_B=millis();
  //gprsSerial.println();
  //Serial.println("Text Sent.");
} //else {Serial.println("INTERVAL TOO SHORT TO SEND SMS B");
//}
}

void SMS_PUMP_A_STATUS()
{
 if (CURRENT_C-PREVIOUS_C>interval_2){
 // Serial.println("Sending Text...");
  gprsSerial.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  delay(200);
  // send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:
  // +15408985543
  gprsSerial.println("AT+CMGS = \"+27829729702\"");
  delay(200);
  gprsSerial.println("PUMP A ERROR"); //the content of the message
  delay(200);
  gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
  delay(200);
  PREVIOUS_C=millis();
  gprsSerial.println();
  } 
}

void SMS_PUMP_B_STATUS()
{
 if (CURRENT_D-PREVIOUS_D>interval_2){
 // Serial.println("Sending Text...");
  gprsSerial.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  delay(200);
  // send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:
  // +15408985543
  gprsSerial.println("AT+CMGS = \"+27829729702\"");
  delay(200);
  gprsSerial.println("PUMP B ERROR"); //the content of the message
  delay(200);
  gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
  delay(200);
  PREVIOUS_C=millis();
  gprsSerial.println();
  } 
}


