I am a newbie to arduino and programming. I would like to come out with a code for water sensor to detect the presence of water for more than 2mins before lighting up the LED lightbulb. Currently using Arduino, Grove baseshield with grove water sensor http://wiki.seeedstudio.com/Grove-Water_Sensor/.
I have the simple base code but not sure how do i go about adding in the function to detect water for a longer period of time before the light lights up:
#define watersensor 8 //Grove water sensor to Arduino Digital Pin 8
#define led 4 //Grove LED to Digital Pin 4
int isWater;
bool wasWater = false;
//bool isWater, wasWater = false; // true => currently we are not seeing water
uint32_t waterstart_ms = 0; // time first saw water
void setup() {
pinMode(watersensor, INPUT);
pinMode(led, OUTPUT);
}
void loop()
{
// Is there water?
isWater = digitalRead(watersensor); // read sensor
Serial.println(isWater);
if( isWater ){
if( wasWater ) {
waterstart_ms = millis(); // First water: note the time
}
if( millis() - waterstart_ms >= 1*5*1000 ){// Water continues for 5s (trial first); time to light LED?
digitalWrite(led, HIGH);
}//end if
wasWater = true;
} // end first if
else {
wasWater = false; // No water; LED off
digitalWrite(led, LOW);
}
}