CODING


CODING :-


 Expalantion :-








Copy Code from below & u can try :-

// Connections  defines pins numbers
const int trigPin = 8;  // Sensor Trigger pin to Arduino pin 8
const int echoPin = 9; // Sensor ECHO pin to Arduino pin 9
int const buzzPin = 13;  // Buzzer Red wire to Arduino pin 13 & black wire to Arduino Ground 

// defines variables
int waterlevel;    // Variable for storing Water level values ,Water level Rise 
long duration;
int distance;     // distance between Sensor and object , here object = Water
void setup() {
  //depth of tank is 11cm 
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
Serial.begin(9600); // Starts the serial communication
}
void loop() {
  
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance

// Distance = velocity of sound in air * time taken to reach and reflect/2
distance= duration*0.034/2;  // Velocity of Sound in Air = 342 m/s 


// Prints the distance on the Serial Monitor
//Serial.print("Distance= cm ");
//Serial.println(distance);


waterlevel=20-distance; // Sensor placed at 20cm away from bottom of bucket
Serial.println(waterlevel); // function for water level
Serial.print("\nwaterlevel=  ");  // Prints 
Serial.print(" (cm)");  // Prints unit " cm "
delay(10);

// For determining how Volume Water in litres Filled at Certain Height
// So 20 cm = 5.6 litres and, so for 1 litre = 3.2cm height 

// conditional Statement
if (waterlevel ==3.2) //  if water reaches to the height 3.2cm than Volume is 1 litre
{  
Serial.print("\n Water = 1 litre\n");
delay(1000);
}
else if (waterlevel == 6.4)
{
  Serial.print("\n Water = 2 litre\n");
  delay(1000); 
}
else if (waterlevel == 9.6)
{
  Serial.print(" \n Water = 3 litre\n"); 
  delay(1000);
}
else if (waterlevel == 12.8)
{
  Serial.print(" \n Water = 4 litre\n"); 
  delay(1000);
}
else if (waterlevel == 16)

{
  Serial.print("\n Water = 5 litre\n"); 
  delay(1000);
}

// For Tank OVERFLOW ALARM
// if distance between Sensor and water is 2cm or less , Buzzer should start BUZZING.. and With Message
// " WATER OVERFLOWING "

if (distance <= 2) {
Serial.print("\nWater Overflowing\n ");   // Indicates " Water overflowing "
delay(10);
// Buzz
digitalWrite(buzzPin, HIGH);   // Switch Buzzer HIGH or ON or starts Buzzing 
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);   // else Buzzer off

}

}

No comments:

Post a Comment