#include <EEPROM.h>
#define echo 5
#define tring 6
int flag_m,a,flag=1,b;
float distance;
void motor_1();
void motor_2();

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(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(tring, OUTPUT);
    delay(1000);
}


void loop() {
             if(b==0)
             {
              motor_1();
              b=1;
              EEPROM.write(2,b);
             }
             else if(b==1){
              motor_2();
              b=0;
              EEPROM.write(2,b);
             }
 
  
  delay(1000);

}
void f1()
{
  distance=readDistance();
    while(distance>80) //380
    {
      
      Serial.println(distance);
      distance=readDistance();
      if(flag==1)
      {
       flag_m=1;
       flag=0;
       EEPROM.write(1,flag_m);
       digitalWrite(10,HIGH);
      
      }
     delay(1000);
    }
    f2(); 

}
void f2()
{
  distance=readDistance();
  Serial.println(distance);
 
      while(flag==0)
      {
        
        
        distance=readDistance();
        Serial.println(distance);
        if(distance<30) //30
        {
          
        flag_m=0;
        flag=1;
        EEPROM.write(1,flag_m);
       
        digitalWrite(10,LOW);
        }
      delay(1000);
      } 
}

void motor_1()
{
   if(a==1)
  { a=0;
    distance=readDistance();
    while(distance>=50) //50
    {
     
      Serial.println(readDistance());
      if(flag==1){

       flag_m=1;
      EEPROM.write(1,flag_m);
     
      digitalWrite(10,HIGH);
      flag=0;
      
      }
      delay(1000);
      f2();
    }
    distance=readDistance();
    if(distance<50)//50
    {
      flag_m=0;
      flag=1;
      EEPROM.write(1,flag_m);
     
     Serial.println(readDistance());
     
    }
  }
  else{
   f1();
  }
}

void motor_2()
{
   if(a==1)
  { a=0;
    distance=readDistance();
    while(distance>=50) //50
    {
     
      Serial.println(readDistance());
      if(flag==1)
      {

       flag_m=1;
       EEPROM.write(1,flag_m);
     
       digitalWrite(9,HIGH);
       flag=0;
      
      }
      delay(1000);
      f4();
    }
    distance=readDistance();
    if(distance<50)//50
    {
      flag_m=0;
      flag=1;
      EEPROM.write(1,flag_m);
     
     Serial.println(readDistance());
     
    }
  }
  else{
   f3();
  }
}

void f3()
{
  distance=readDistance();
    while(distance>80) //380
    {
      
      Serial.println(distance);
      distance=readDistance();
      if(flag==1)
      {
       flag_m=1;
       flag=0;
       EEPROM.write(1,flag_m);
       digitalWrite(9,HIGH);//motor on
      
      }
     delay(1000);
    }
    f4(); 

}

void f4()
{
  distance=readDistance();
  Serial.println(distance);
 
      while(flag==0)
      {
        
        
        distance=readDistance();
        Serial.println(distance);
        if(distance<30) //30
        {
          
        flag_m=0;
        flag=1;
        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);
  int duration= pulseIn(echo,HIGH);
  float p= duration*0.034/2;
 totdidt+=p;
 delay(10);
  }
  return totdidt/5.0;
}