# reversing DC motor rotation using TWO limit Switches Problem !! need help

I'd like to write a code for reversing DC motor rotation direction as follows:

1. Start the motor rotation clockwise (CW).
2. When reach Limit Switch (CW) → motor will stop for 5 sec.
3. after 5 sec. of stopping → the motor will rotate in counter clockwise direction(CCW).
4. When reach Limit Switch (CCW) → motor will stop for 5 sec.

and repeat point 1, 2, 3 and 4...

Actually I wrote a code for reversing the motor direction using two limit switches and it is working good. But when I added delay the motor did not stop using the limit Switches and continue to rotate... If any one can help... Here is my code for reversing the direction with out delay 5 sec. My limit SWs are connected on Normal Open.

``````int sw1, sw2, sw3;
const int LM1 = 0;
const int LM2 = 1;
const int LM3 = 6;
const int CW = 10;
const int CCW = 9;
const int Buzzer = 7;

void setup() {
pinMode(LM1, INPUT);
pinMode(LM2, INPUT);
pinMode(LM3, INPUT);
pinMode(CW, OUTPUT);
pinMode(CCW, OUTPUT);
}

void loop(){

if (sw1 == LOW && sw2 == HIGH) {
do {
digitalWrite(Buzzer, HIGH);
digitalWrite(CW, HIGH);// change the direction of rotation (DOWN Movement)
digitalWrite(CCW, LOW);

} while (sw2 == HIGH);
}
else if (sw1 == HIGH && sw2 == LOW) {
do {
digitalWrite(Buzzer, HIGH);
digitalWrite(CW, LOW);// change the direction of rotation (UP Movement)
digitalWrite(CCW, HIGH);

} while (sw1 == HIGH);
}
}
``````

Normally you use state machine for this kind of programming: you have a `state` variable that remembers what you are doing, and depending on the current state you decide what to do next. Below is an example with two states. I have embedded a few assumptions about the wiring of your motor and your switches, which may be wrong, so you have to check the code.

``````void loop() {
static enum {MOVING_CW, MOVING_CCW} state;

switch (state) {
case MOVING_CW:
if (digitalRead(LM2) == LOW) {  // hit switch
digitalWrite(CW, LOW);      // stop
delay(5000);
digitalWrite(CCW, HIGH);    // restart
state = MOVING_CCW;
}
break;
case MOVING_CCW:
if (digitalRead(LM1) == LOW) {  // hit switch
digitalWrite(CCW, LOW);     // stop
delay(5000);
digitalWrite(CW, HIGH);     // restart
state = MOVING_CW;
}
break;
}
}
``````

This will start in the `MOVING_CW` state, but you have to actually start the movement in `setup()`. It has the drawback of having long delay()s in the loop, which is undesirable if you have other tasks to perform. These delays can be removed by “remembering” that you are waiting as two extra states:

``````void loop() {
static enum {MOVING_CW, MOVING_CCW, STOPPED_CW, STOPPED_CCW} state;
static unsigned long time_stopped;
unsigned long now = millis();

switch (state) {
case MOVING_CW:
if (digitalRead(LM2) == LOW) {  // hit switch
digitalWrite(CW, LOW);      // stop
state = STOPPED_CW;
time_stopped = now;
}
break;
case MOVING_CCW:
if (digitalRead(LM1) == LOW) {  // hit switch
digitalWrite(CCW, LOW);     // stop
state = STOPPED_CCW;
time_stopped = now;
}
break;
case STOPPED_CW:
if (now - time_stopped >= 5000) {
digitalWrite(CCW, HIGH);    // restart
state = MOVING_CCW;
}
break;
case STOPPED_CCW:
if (now - time_stopped >= 5000) {
digitalWrite(CW, HIGH);     // restart
state = MOVING_CW;
}
break;
}
}
``````

Maybe the code with the 5 second delay is more usefull. Because you are telling us the code you now deliver is supposed to work?

Probably using delay(5000) will work?

• I added delay (5000); at the end of my code -- > Doesn't work also I tried to add delay at the start of my code ---> Doesn't work Jun 22, 2015 at 7:39
• You will have to make another state (idle). Here you will have to put the commands low, this way the motor will actually stop. Then you can add the delay to it, so the motor will stop for the 5 second duration. Jun 22, 2015 at 7:43