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?
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.
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;
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.
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.
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.
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.......