Stoping a rain sensor linked motor from repeating one motion

Hi everyone,
I am new to Arduino and am piecing together a rain sensor and a motor to help me close my window when the rain starts, and open the window when the rain sensor dries up. The hardware is set up to spin the motor in both directions and pick up signal from the sensor. But as I tested my sketch, it repeats the closing act as long as the board is wet. But the window just needs to be closed once, leaving the only subsequent action to be the opening of the window. Does anyone know how to fix the code I have below? Thank you!

//pin 8 = direction
//pin 9 = enable

const int floodSensors = 2; // the number of the Flood Sensor pin
const int motorDirection = 8; // the number of the Motor Direction pin
const int motorEnable = 9; // the number of the Motor Enable pin

int floodSensorState = 0; // variable for reading the floodSensors status

int windowClosed = false;

void setup() {

pinMode(floodSensors, INPUT); // initialize the flood Sensor pin as an input:
pinMode(motorDirection, OUTPUT); //set direction pin as output
pinMode(motorEnable, OUTPUT); //set enable pin as output
}

void loop()
{
floodSensorState = digitalRead(floodSensors); // read the state of the flood Sensor value:

if (windowClosed == false)
{
closeWindow();
}

if (windowClosed == true)
{
openWindow();
}
}

void closeWindow()
{
if (floodSensorState == HIGH)
{
//start off going forward at 50% throttle
digitalWrite(8, HIGH); //forward
analogWrite(9,128); //50% PWM
delay(5000);

digitalWrite(9, HIGH); //full speed
delay(10000);

digitalWrite(9, LOW); //turn enable pin off
delay(5000);

windowClosed == true;
}
}

void openWindow()
{
if(floodSensorState == LOW)
{
digitalWrite(8, LOW); //backward
analogWrite(9,128); //50% PWM
delay(5000);

digitalWrite(9, HIGH); //full speed
delay(10000);

digitalWrite(9, LOW); //turn enable pin off
delay(5000);

windowClosed == false;
}
}

Breath_Rain_Sensing_Motor_V2.ino (1.38 KB)

You only want to close the window if the sensor is wet and the window is open and open it if is dry and the window is closed. If neither condition is true then do nothing but read the sensor again.

start of loop
  read sensor
  if wet and windowOpen is true
    closeWindow()
    set windowOpen to false
  end if
  else
  if not wet and windowOpen is false
    openWindow()
    set windowOpen to true
  end if
end of loop

Thank you HeliBob for pointing out the logic mistake. I updated the code as the following but the motor still doesn't function per what I thought the code would do.

//pin 8 = direction //pin 9 = enable

const int floodSensors = 2; // the number of the Flood Sensor pin const int motorDirection = 8; // the number of the Motor Direction pin const int motorEnable = 9; // the number of the Motor Enable pin

int floodSensorState = LOW; // variable for reading the floodSensors status

int windowOPEN = true;

void setup() {

pinMode(floodSensors, INPUT); // initialize the flood Sensor pin as an input: pinMode(motorDirection, OUTPUT); //set direction pin as output pinMode(motorEnable, OUTPUT); //set enable pin as output }

void loop() { ** **floodSensorState = digitalRead(floodSensors); // read the state of the flood Sensor value: if (windowOPEN == true && floodSensorState == HIGH) ** {** ** closeWindow();** ** }** else if (windowOPEN == false && floodSensorState == LOW) ** {** ** openWindow();** ** }** }

void closeWindow() { //if (floodSensorState == HIGH) { //start off going forward at 50% throttle digitalWrite(8, HIGH); //forward analogWrite(9,128); //50% PWM delay(5000);

digitalWrite(9, HIGH); //full speed delay(10000);

digitalWrite(9, LOW); //turn enable pin off delay(5000);

windowOPEN = false; } }

void openWindow() { //if(floodSensorState == LOW) { digitalWrite(8, LOW); //backward analogWrite(9,128); //50% PWM delay(5000);

digitalWrite(9, HIGH); //full speed delay(10000);

digitalWrite(9, LOW); //turn enable pin off delay(5000);

windowOPEN = true; } }

What does your flood sensor consist of, and how is it wired to the arduino? You program looks like it should work, but perhaps you have an electrically floating pin.

the rain sensor has ground and a positive that goes through a 10k resister and crossed by signal wire connected to pin 2. photo of the setup below: https://www.evernote.com/shard/s94/sh/d1af4191-f103-4a4f-b8e2-d73fb17be968/ac2f3674adeb24f8b8a7bfe02a5013af

thanks for looking into this :)

mgpopinjay: the rain sensor has ground and a positive that goes through a 10k resister and crossed by signal wire connected to pin 2. photo of the setup below:

OK, that looks alright.

I see one thing in your latest code, and that is that the rain sensor or the window is backwards to what you say should happen. else if (windowOPEN == false && floodSensorState == LOW) should close the window, not open it. and the same reversal is in the other if.

One thing I would check is to write a small sketch that only looks at the rain sensor. The way you read it now is with a digitalRead(), and I am wondering if the equivalent of switch bounce is happening as the sensor gets wet and then as it dries. I'd try reading the sensor pin over and over (with digitalRead()), quickly, and when you detect the first LOW, start counting LOW readings. When it returns HIGH, count the HIGH readings (use unsigned long variables) every time it transitions, report the number of counts for the previous state, using Serial.println(). Ideally, you'd see a single LOW when it gets wet, followed by a single HIGH when it dries, but real life isn't like that. Study the results, and try to determine if it's going to mess up your code.

If it does transition often after a single wetting, try again, but this time use an analog pin, and analogRead(). If you get good transitions there, or get some indication that the sensor is wetting gradually, then drying gradually, you can build in what we call hysteresis.. when it wets, don't trasnition to dry until it reads higher than the value that tiggered the wet state.

You can also play with the resistor vaules. The easiest one to use is the internal pullup.

Hi everyone,

Thank you all for the tips . I managed to get everything wired up and recoded per your suggestions. It's fully functional now and I may link it up to a wind sensor in the coming weeks. Below are the pictures:

cheers,

phil

Hey,

I am wondering if you would be able to give me with this code as I am making the same device as you.

Thanks

Bailey

What about adding the two lines as below

if (windowOPEN == true && floodSensorState == HIGH)
  {
  closeWindow();
  windowOpen = false;  // <<<<<<<<<<<<<<<
  }

else if (windowOPEN == false && floodSensorState == LOW)
  {
  openWindow();
  windowOpen = true; // <<<<<<<<<<<<<<<<
  }
}

…R