Last active
August 29, 2015 14:09
-
-
Save Jennaltj/49f766fe0ff61bb485d0 to your computer and use it in GitHub Desktop.
water level detecting by arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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