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?
if( isWater = digitalRead(watersensor); == LOW){ // waterread sensor
willSerial.println(isWater);
switch
to low whenif( waterisWater is){
detected. if( wasWater ) {
digitalWrite waterstart_ms = millis(led,HIGH); // ledFirst willwater: lightnote upthe iftime
there's water
}
if( millis() - waterstart_ms >= 1*5*1000 ){//end if
Water continues for 5s (trial first); time to light
else{LED?
digitalWrite(led,LOW HIGH);
} }//ledend WILLif
NOT light up if there's no water in water sensor
wasWater = true;
} // end first if
else {
wasWater = false; // No water; LED off
digitalWrite(led, LOW);
}
}