0

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(){
  sw3 = digitalRead(LM3);
  sw1 = digitalRead(LM1);
  sw2 = digitalRead(LM2);

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

      sw2 = digitalRead(LM2);
    } 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);

      sw1= digitalRead(LM1);
    } while (sw1 == HIGH);
  }
}
3

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;
    }
}
0

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 – mohammad.zuhd Jun 22 '15 at 7:39
  • 3
    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. – KoenR Jun 22 '15 at 7:43
  • kindly give me idea or example about this issue – mohammad.zuhd Jun 22 '15 at 8:00
  • Motor CW > Motor idle > Motor CCW > Motor idle > Motor CW….. Every change in limit switching should change the state of the motor. In your program now it is: Motor CW > Motor CCW > Motor CW… Are you able to put the motor on or off with program code? Or are you only able to change its direction? If so, would writing both CW and CCW LOW deliver any problems? – KoenR Jun 22 '15 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.