Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 487 characters in body
Source Link
hmppp
  • 13
  • 3

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);
   }
}

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 


void setup(){

 
pinMode(watersensor, INPUT);    
pinMode(led, OUTPUT);                   
 
}

void loop(){

      
if(digitalRead(watersensor) == LOW){ // water sensor will switch to low when water is detected.
    digitalWrite(led,HIGH); // led will light up if there's water 
    } //end if
         
else{
    digitalWrite(led,LOW); } //led WILL NOT light up if there's no water in water sensor
    } // end else

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);
   }
}
edited title
Link
hmppp
  • 13
  • 3

How to write a script if water sensor cmescomes into contact with water for more than 2 mins and LED lights up.

Source Link
hmppp
  • 13
  • 3

How to write a script if water sensor cmes into contact with water for more than 2 mins and LED lights up.

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 


void setup(){


pinMode(watersensor, INPUT);    
pinMode(led, OUTPUT);                   

}

void loop(){

      
if(digitalRead(watersensor) == LOW){ // water sensor will switch to low when water is detected.
    digitalWrite(led,HIGH); // led will light up if there's water 
   } //end if
         
else{
    digitalWrite(led,LOW); } //led WILL NOT light up if there's no water in water sensor
    } // end else