Skip to main content
Arduino

Return to Revisions

2 of 2
formatting, mostly the code
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

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(){
 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);
 }
}
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /