//AUTOMATIC WATER LEVEL CONTROLLER

#define PIN_TRIG 26
#define PIN_ECHO 25
#define LOWLEDred   18
#define MIDLEDyellow   19
#define HIGHLEDgreen  21
#define MOTORPUMP   27

unsigned int level = 0;

void setup() {
  
  pinMode(LOWLEDred,OUTPUT);
  pinMode(MIDLEDyellow,OUTPUT);
  pinMode(HIGHLEDgreen,OUTPUT);
  pinMode(MOTORPUMP,OUTPUT);
  digitalWrite(LOWLEDred,HIGH);
  digitalWrite(MIDLEDyellow,HIGH);
  digitalWrite(HIGHLEDgreen,HIGH);
  digitalWrite(MOTORPUMP,LOW);

  Serial.begin(115200);
  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECHO, INPUT);
}

void loop() {
 // DISTANCE IN CM
  digitalWrite(PIN_TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);

  //DISTANCE IN INCHES
  int duration = pulseIn(PIN_ECHO, HIGH);
  Serial.print("Distance in CM: ");
  Serial.println(duration / 58);
  Serial.print("Distance in inches: ");
  Serial.println(duration / 148);


  // WATER LEVEL
  level = duration / 58;                     
   if (level < 100)
  {
        digitalWrite(LOWLEDred,LOW);             
        digitalWrite(MOTORPUMP,HIGH);
        digitalWrite(HIGHLEDgreen,HIGH);
        digitalWrite(MIDLEDyellow,HIGH);

  }
  else if ((level > 200) && (level < 400))       
  {
        digitalWrite(LOWLEDred,HIGH);
        digitalWrite(HIGHLEDgreen,HIGH);
        digitalWrite(MIDLEDyellow,LOW);
  }
   else if (level >= 400)
  {
        digitalWrite(HIGHLEDgreen,LOW);
        digitalWrite(MIDLEDyellow,HIGH);
        digitalWrite(LOWLEDred,HIGH);
        digitalWrite(MOTORPUMP,LOW);
  }    

  delay(1000);
}
NOCOMNCVCCGNDINLED1PWRRelay Module