Skip to content

Instantly share code, notes, and snippets.

@Jennaltj
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jennaltj/49f766fe0ff61bb485d0 to your computer and use it in GitHub Desktop.
Save Jennaltj/49f766fe0ff61bb485d0 to your computer and use it in GitHub Desktop.
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