LED stays ON when the sensor state goes LOW

I am trying to modify the code written by T.K.Hareendran for the Rain sensor module.

I wanted to flash an LED when the sensor senses rain, which works OK, except that when the sensor becomes dry again, the LED stays on, though I think if the LED was low at the time of the change it would stay low.
I used the code BlinkWithoutDelay in the Arduino examples in the Rain Alarm code to produce the flashing of the LED.

If this was in a finished project and the sounder, which I intend to use instead of the LED, stayed on all the time it would drive eveyone crazy.

I also think I need to think about having a long delay, perhaps if I had a button that activated the delay.

Any suggestions on how to overcome my first problem would be appreciated.

thudson:
Any suggestions ..... would be appreciated.

I suggest you post your code and a schematic.

(I'm guessing this is a lack of resistor on the pin.)

Opps! Sorry I meant to have included!

    /*
    * Arduino Rain Sensor Alarm
    * Realized using a pre-wired rain sensor module
    * Author: T.K.Hareendran
    * Prototyped &Tested at Technode Protolabz /1:04 AM 7/26/2015
    * Source: http://www.electroschematics.com
    */
    int rainSensePin= 0; // analog pin 0 - sensor i/p
   // constants won't change. Used here to set a pin number :
const int ledPin =  8;      // the number of the LED pin
    int curCounter= 0; // current counter - goes up by 1 every second while sensing
    
 

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

     
    void setup(){
       Serial.begin(9600);
       pinMode(ledPin, OUTPUT);
       pinMode(rainSensePin, INPUT);
        // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
    }
    void loop(){
       int rainSenseReading = analogRead(rainSensePin);
       Serial.println(rainSenseReading); // use this for serial monitoring if available
       delay(250);  // relax
       // check to see how long it is raining at the threshold level
       // rain strength value from 0 - 1023
       // heavy rain -to- no rain.
       if (curCounter >= 10){ // end of the time delay
          digitalWrite(ledPin, HIGH);  //raise an alert after x time
       }
     
       // If raining is continuing for x amount of time raise an alert
       // When raining is no longer detected, reset the counter
       if (rainSenseReading <400){ // while raining at threshold level - see text
          curCounter++; // increment sensing counter
          Serial.println("ITS RAINING");
      //This is the code I add from BlinkWithoutDelay
      //The orginal Rain Sensor code used the variable alertPin, but I have changed that to ledPin
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;   
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
      ledState = HIGH;
      else
      ledState = LOW;

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
      }
      // end of BlinkWithoutDelay code
      else if (rainSenseReading >400) { // if not raining at threshold level
         
          digitalWrite(ledPin, LOW); // don't raise an alert
          curCounter = 0; // reset the counter to 0
          // Added this code to change 
          if (ledState == HIGH)
      int ledState = LOW;
       }
      delay(1000);
      }
      }

Thanks!

          if (ledState == HIGH)
      int ledState = LOW;
       }

So, if the global ledState is low, create a local variable and assign it a value, just before it goes out of scope. Why would you do that?

Did not quite understand your post?

The first sentence, are you saying I should do that?

But then you question why I wanted to to make the LED go LOW??

When I run the code and simulate rain on the sensor pad and it's value goes less than 400, then after 10 secs the LED will flash on and off. If the value goes above 400 after I remove the damp sponge which simulates the rain condition, the LED on most occasions stays ON. What I want is for the LED to be OFF when the value goes above 400, whatever the ledState when this happens.

The first sentence, are you saying I should do that?

No. You do NOT want to create a local variable. You DO want to modify the global variable.

So, your homework assignment is to write two statements. One of them will create a variable and assign a value to it. The other will simply assign a new value to an existing variable. When you have completed this assignment, you will be able to recognize what is wrong with that snippet.

I guess you mean that it should not have int preceding it.

That aside, I put that there in desperation as I had tried every thing I could think of.
It did not work without int preceding it, so why does the LED not not stay LOW when the value increases beyond the value I have set?
Constructive suggestion please!

As you have it, it seems that when it detects rain, it would blink for 12.5 seconds, then stay on. When it does not detect rain, it should turn off. Is that not what you wanted? Perhaps you are confused by it staying on after the delay, falsely believing that it has something to do with the reading.

If it blinks too slowly, or fails to blink, it is because you have combined blink without delay, with delay(), in the same loop.

One more thing. You should change the comments to reflect the fact that you have modified the sketch. Imagine how you would feel if someone else changed your code (and broke it), then published it in the public domain.

Firstly, I don't get where you recon 12.5 secs?
There's a 250 mS delay after the sensor is read, then 10secs on the counter.
As I keep saying the LED flashes for 1 sec On/Off until the value set goes out on range and instead of then staying Off it stays On.
I do not want the LED to be on when it is out of range.

I apologise, regarding the comments, but as I am just experimenting and redeveloping the code, I would have altered it when I had finished developing the code.