// Ultrasonic Water level indiacator version 2 . // If Tank is full Green led glows on pin no 4. // If Tank is half-filled blue led glows on pin no 3. // If Tank is Empty red led glows on pin no 2. // defines pins numbers const int trigPin = 8; const int echoPin = 10; int red = 2;//low int blue = 3;//medium int green =4;//high int buzzer=5; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input pinMode(red,OUTPUT); pinMode(blue,OUTPUT); pinMode(green,OUTPUT); pinMode(buzzer,OUTPUT); Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2000); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(15); digitalWrite(trigPin, LOW); delayMicroseconds(10); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance in cm: "); Serial.println(distance); delay(100); if(distance <=150) { if (distance >60 && distance <=70 ) //>80<110 { digitalWrite (red,HIGH);//empty tank digitalWrite (blue,LOW); digitalWrite(green,LOW); Serial.print("Water Tank is Empty..."); } else if ( distance >40 && distance <=60) //>65<80 { digitalWrite (blue,HIGH); // half-filled tank delay(500); digitalWrite (blue,LOW); delay(500); digitalWrite(green,LOW); digitalWrite(red,LOW); Serial.print("Half-filled Tank"); } else if ( distance >25 && distance <=40) //>65<80 { digitalWrite (blue,HIGH); // half-filled tank digitalWrite(green,LOW); digitalWrite(red,LOW); Serial.print("Ha........."); } else if ( distance <=25) { digitalWrite (green,HIGH);// full tank digitalWrite (blue,LOW); digitalWrite(red,LOW); digitalWrite(buzzer,HIGH); delay(500); digitalWrite(buzzer,LOW); delay(500); Serial.print("Water Tank is Full..."); } else {digitalWrite (green,LOW); digitalWrite (blue,LOW); digitalWrite(red,LOW); digitalWrite(buzzer,LOW); } } } }