Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
water level detecting by arduino
const int pingPin = A0;
//-------denoise----------------
int denoise[10];
int temp = 0;
//========================denoise
void setup()
{
Serial.begin(9600);
}
void loop()
{
long duration;
float cm;
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin,HIGH);
cm= microsecondsToCentimeters(duration);
//-----denoise
for(int i = 0; i < 10; i++)
{
denoise[i] = cm;
}
for(int i = 0; i < 10 ; i++)
{
for(int j = 0; j< 10 - i; j++)
{
if(denoise[i] > denoise[i+1])
{
temp = denoise[i];
denoise[i] = denoise[i+1];
denoise[i+1] = temp;
}
}
}
cm = (denoise[4] + denoise[5]) / 2;
//============denoise
Serial.println(cm,DEC);
delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 0.29 / 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.