#include <EEPROM.h>
#define echo 5
#define tring 6
#define echo1 2
#define tring1 3
int flag_m,a,flag=1,b,p;
float distance,dis2;
int duration;
int duration1;

void setup() 
{
  Serial.begin(115200);
  a=EEPROM.read(1);
  if(a!=0 && a!=1)
  {
    a=0;
    EEPROM.write(1,a);
  }
   b=EEPROM.read(2);
  if(b!=0 && b!=1)
  {
    b=0;
    EEPROM.write(2,b);
  }
 
    pinMode(10, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(tring, OUTPUT);
    pinMode(echo, INPUT);
     pinMode(tring1, OUTPUT);
    pinMode(echo1, INPUT);
    delay(1000);
}


void loop() {
  if(a==1)
  { a=0;
    dis2=readDistance_1();
    distance=readDistance();
    while(distance>=50) //50
    {
      Serial.println("reserver distance ");
      Serial.println(readDistance_1());
      Serial.println("tank distance ");
      Serial.println(readDistance());
      if(flag==1 && b==0 && dis2<30 ) // mesure reserver distance
      {

       flag_m=1;
       b=1;
       EEPROM.write(2,b);
      EEPROM.write(1,flag_m);
     // motor_on();
      digitalWrite(10,HIGH);
      p=1;
      flag=0;
      }
      else if(flag==1 && b==1 && dis2<30 )  // mesure reserver distance
      {

       flag_m=1;
       b=0;
       EEPROM.write(2,b);
      EEPROM.write(1,flag_m);
     // motor_on();
      digitalWrite(9,HIGH);
      p=0;
      flag=0;
      }
      delay(1000);
      f2();
    }
    //dis2=readDistance_1();
    distance=readDistance();
    if(distance<50)//50
    {
      flag_m=0;
      flag=1;
      EEPROM.write(1,flag_m);
     // motor_off();
     Serial.println("reserver distance ");
      Serial.println(readDistance_1());
      Serial.println("tank distance ");
      Serial.println(readDistance());
    }
  }
  else{
   f1();
  }
  
  delay(1000);

}
void f1()
{
  distance=readDistance();
  dis2=readDistance_1();
    while(distance>80) //380
    {
      Serial.println("reserver distance ");
      Serial.println(dis2);
      Serial.println("tank distance ");
      Serial.println(distance);
      dis2=readDistance_1();
      distance=readDistance();
      if(flag==1 && b==0 && dis2<50)  // mesure reserver distance
      {
       flag_m=1;
       flag=0;
       b=1;
       EEPROM.write(2,b);
       EEPROM.write(1,flag_m);
       digitalWrite(10,HIGH);
       p=1;
      // motor_on(); 
      }
      else if(flag==1 && b==1 && dis2<50) // mesure reserver distance
      {
       flag_m=1;
       flag=0;
       b=0;
       EEPROM.write(2,b);
       EEPROM.write(1,flag_m);
       digitalWrite(9,HIGH);
       p=0;
      // motor_on(); 
      }
      else if(p==1 && dis2>80) //mesure reserver distance
        {
          // motor_off()
       flag_m=0;
       flag=1;
        EEPROM.write(1,flag_m);
        digitalWrite(10,LOW);
        b=1;
        }
        else if(p==0 && dis2>80) //mesure reserver distance
        {
          // motor_off()
       flag_m=0;
       flag=1;
        EEPROM.write(1,flag_m);
        digitalWrite(9,LOW);
        b=0;
        }

     delay(1000);
    }
    f2(); 

}
void f2()
{
  distance=readDistance();
  dis2=readDistance_1();
  Serial.println("reserver distance ");
      Serial.println(dis2);
      Serial.println("tank distance ");
      Serial.println(distance);
      while(flag==0)
      { 
        distance=readDistance();
        dis2=readDistance_1();
        Serial.println("reserver distance ");
      Serial.println(dis2);
      Serial.println("tank distance ");
      Serial.println(distance);
        if(distance<30 && b==1) //30
        {
          // motor_off()
        flag_m=0;
        flag=1;
        b=1;
        EEPROM.write(2,b);
        EEPROM.write(1,flag_m);
        digitalWrite(10,LOW);
        }
        else if(distance<30 && b==0) //30
        {
          // motor_off()
        flag_m=0;
        flag=1;
        b=0;
        EEPROM.write(2,b);
        EEPROM.write(1,flag_m);
        digitalWrite(9,LOW);
        }
        else if(dis2>80 && b==1) //mesure reserver distance
        {
          // motor_off()
        flag_m=0;
        flag=1;
        b=1;
        EEPROM.write(1,flag_m);
        digitalWrite(10,LOW);
        }
        else if(dis2>80 && b==0) //mesure reserver distance
        {
          // motor_off()
        flag_m=0;
        flag=1;
        b=0;
        EEPROM.write(1,flag_m);
        digitalWrite(9,LOW);
        }
      delay(1000);
      } 
}
float readDistance()
{
  float totdidt=0.0;
  for(int i=0;i<5;i++)
  {
  digitalWrite(tring, LOW);
  delayMicroseconds(2);
  digitalWrite(tring, HIGH);
  delayMicroseconds(10);
  digitalWrite(tring, LOW);
  duration= pulseIn(echo,HIGH);
  float p= duration*0.034/2;
 totdidt+=p;
 delay(10);
  }
  return totdidt/5.0;
}

float readDistance_1()
{
  float totdidt=0.0;
  for(int i=0;i<5;i++)
  {
  digitalWrite(tring1, LOW);
  delayMicroseconds(2);
  digitalWrite(tring1, HIGH);
  delayMicroseconds(10);
  digitalWrite(tring1, LOW);
  duration1= pulseIn(echo1,HIGH);
  float p= duration1*0.034/2;
 totdidt+=p;
 delay(10);
  }
  return totdidt/5.0;
}