Window controlled by water sensor

My window closes when water is detected. However, it does not open when no water is detected. Help?

#define WATER_SENSOR1 4
#define steppermotorRight 10
#define steppermotorLeft 11
#define LS1 12
#define LS2 13




void setup()
{
  Serial.begin(9600); // open serial at 9600 bps
  
  pinMode (WATER_SENSOR1,INPUT);
  pinMode (steppermotorRight,OUTPUT);
  pinMode (steppermotorLeft, OUTPUT);
  pinMode (LS1,INPUT);
  pinMode (LS2,INPUT);
 
}

void loop()
{
  
  Serial.println(digitalRead(WATER_SENSOR1));
  delay(100);
  Window();
   
}

void Window()
{
  if(isExposedToWater())
  { 
    closeWindow();
  }
  else  openWindow();
}

boolean isExposedToWater()
{
	if(digitalRead(WATER_SENSOR1) == LOW)
		return true;
	else return false;
}

boolean limitSwitch1()
{
  if(digitalRead(LS1) == HIGH)
          return true;
  else return false;
}

boolean limitSwitch2()
{
  if(digitalRead(LS2) == HIGH)
          return true;
  else return false;
}

void closeWindow()
{
 if(limitSwitch2())
 {
   digitalWrite(steppermotorRight,HIGH);
   delay(2000);
 }
 else
   digitalWrite(steppermotorRight,LOW);

}

void openWindow()
{
  if(limitSwitch1())
 {
    digitalWrite(steppermotorLeft,HIGH);
    delay(2000);
 }
 else
    digitalWrite(steppermotorLeft,LOW);
  
}

You need to post all your code. You have not.

I have modified the post.

Hi, I can't see a problem with your sketch. Can you post a schematic diagram of the circuit. Have you tried printing out the values of the limit switches in loop() like you are doing for the water sensor?

Paul

Thanks.

How is this wired up?
I ask because you normally do not drive a stepping motor by giving a left and right signal like your code is suggesting. Normally you have a step and direction input to a motor. You either have a diffrent driver or you are missunderstanding how to drive it.

I'm using relays to control the motor. The arduino UNO gives the signal to the relays.

So it is not a stepper motor, it is an ordinary dc motor. I thought that was probably the case.

What abour those limit switch values?

please modify this function to get some logging

void Window()
{
  Serial.print(millis());
  if (isExposedToWater())
  {
    Serial.println("CLOSE");
    closeWindow();
  }
  else  
  {
    Serial.println("OPEN");
    openWindow();
  }
}

you use delay() on low level and that means that during the delay no limit switches can be testes or whatever.
better use a state machine that reflect the window.
4 states = { OPEN, OPENING, CLOSED, CLOSING}
initial state is CLOSING

depending on the sensors, limit switches and current state you may go to another state

void  setup()
{
  state = CLOSING;
}

void loop()
{
  // READ SENSORS
  boolean waterDetected = (digitalRead(WATER_SENSOR1) == HIGH);
  boolean limitSwitch1 = (digitalRead(LS1) == HIGH);
  boolean limitSwitch2 = (digitalRead(LS2) == HIGH);
  
  // DETERMINE NEW STATE
  switch (state)
  {
    case OPEN: 
    // monitor to see it should be closed -> change state if needed
     if (waterDetected) state = CLOSING;
     break;
    case OPENING: // monitor if it reached end or should be closed -> change state if needed
     if (waterDetected) state = CLOSING;
     if (limitSwitch1 ) state = OPEN;
      break;
    case CLOSED:
     if ( !waterDetected ) state = OPENING;
     break;
    case CLOSING:
     if (!waterDetected) state = OPENING;
     if (limitSwitch2 ) state = CLOSED;
     break;
  }

  // CHANGES MOTORS TO REFLECT STATE
  switch(STATE)
  {
    case OPEN: // switch off all motors
    case OPENING: // motor1 = on +  rest off
    case CLOSED: // switch off all motors
    case CLOSING: // motor2 = on +  rest off
  }
}

Get the idea?

note you might need to add some timer to see that serious water is detected so the sensor does not in toggling mode.
in another state machine you can have the behaviour that it closes completely before it opens again. in code:

case CLOSING:
if (limitSwitch2 ) state = CLOSED;
break;

  1. how do you power the thing? schema

I'm using 2 RY5W-K relays to control the 2 wires of the motor. It's a 12V DC motor but i'm using 5V as the window closes too fast when it's operating at 5V. The limit switches are normally closed.Sorry i couldn't come up wit h the shematic. Thank you so much for your help.

mdfaheem:
.Sorry i couldn't come up wit h the shematic.

Why not, just draw it with a pencil and paper and then post a photo of your drawing. You could be making all sorts of other mistakes which seems likely given the track record of this post.

Here is the schematic.

Connected Normally closed limit switches to pins 12 and 13. Pin 4 is the water sensor input.

If I read that correctly, in NC unenergised mode both sides of the motor are at 0V and when the coil is energised, both sides are at 5V…

The coil only get energised when it gets a signal from the arduino. Hence, at any one time only 1 coil gets energized since curretn has to flow from 5v to GND through the motor.

mdfaheem:
The coil only get energised when it gets a signal from the arduino. Hence, at any one time only 1 coil gets energized since curretn has to flow from 5v to GND through the motor.

Ah yeah, sorry, me being dumb. I was thinking they were on the same i/o pin.

It's okay. Do you know how to declare the states like in the example above? I can't switch from one state to another. the program shows me error.

I'm currently working on a similar project (automatic door opener/closer for a chicken coop).
If you're interested this is the schematic of what I've come up with so far. Excuse the scruffy diagram. (I used paint).

The limit switches will directly disconnect power from the motor but also provide inputs to allow the arduino to sense when they have been triggered.. The two relays driving the motor are acting as one double pole relay.

as was stated, your sketch shows the power and ground on the relays incorrectly.

I put it in paint and added colors.

what is missing is the transistors to control the relays, the diodes to adsorb the back EMF and resistors to limit current to the transistors.

further, the motor is shown on the same power supply as the arduino.

the concept is good, and workable the listed relay method will not.

here is your original and a possible alternative.

one pin allows power to the DPDT relay.
the DPDT relay is controlled by a second pin. one way is forward, the other is reverse.

window1.bmp (81.2 KB)

window-better.bmp (163 KB)

you could use an H-bridge such as the L298 . it would use half the device and need only 2 pins.

as Ken F stated, you could run the power through an end-switch so that once the end-switch is reached, it cuts power and the only way to power the motor would be in the opposite direction. very clever.

not sure if you can open the wires on the output of the L298.......

I rewrote the state-machine for the window, as this project is a very good example to show how state machines can work

it compiles but is not tested.

//
//    FILE: stateMachineDemo.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo
//    DATE: 2014-09-27
//     URL:
//
// Released to the public domain
//

#define WATER_SENSOR1 4
#define steppermotorRight 10
#define steppermotorLeft 11
#define LS1 12
#define LS2 13

enum state { OPEN, OPENING, CLOSED, CLOSING } ;
state window = CLOSING;


void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");

  window = CLOSING;  // savest assumption.
}

void loop()
{
  // READ SENSORS
  boolean waterDetected = (digitalRead(WATER_SENSOR1) == HIGH);
  boolean limitSwitch1 = (digitalRead(LS1) == HIGH);
  boolean limitSwitch2 = (digitalRead(LS2) == HIGH);
  
  // UPDATE STATE IF NEEDED
  switch  (window)
  {
    case OPEN: 
     if (waterDetected) window = CLOSING;
     break;
    case OPENING:
     if (waterDetected) window = CLOSING;
     if (limitSwitch1 ) window = OPEN;
      break;
    case CLOSED:
     if ( !waterDetected ) window = OPENING;
     break;
    case CLOSING:
     if (!waterDetected) window = OPENING;
     if (limitSwitch2 ) window = CLOSED;
     break;
  }

  // CONTROL MOTORS TO REFLECT STATE OF WINDOW
  switch (window)
  {
    case OPEN:  // note the fall through
    case CLOSED:
      digitalWrite(steppermotorRight, LOW);
      digitalWrite(steppermotorLeft, LOW);
      break;
    case OPENING:
      digitalWrite(steppermotorRight, HIGH);
      digitalWrite(steppermotorLeft, LOW);
      break;
    case CLOSING:
      digitalWrite(steppermotorRight, LOW);
      digitalWrite(steppermotorLeft, HIGH);
      break;
  }

  // add printing state code here
}