0

I have a brushed motor hooked up to a NodeMCU that moves a plate left (moveLeft) and right (moveRight). When a button (buttonOpen) is pressed, it turns to the left until the plate hits a limit switch (limit1), then the system stops and the motor driver goes to sleep. Then when a different button (buttonClose) is pressed, it moves the plate to the right, until a different limit switch (limit2) is pressed.

Now the thing is that I need the DC motor to brake once the limit switch is triggered, otherwise it will slowly roll over the switch (which is bad).

I noticed that I needed to apply the brake for at least a fraction of a second to get the motor to stop. If the brake is too short, the motor will continue to spin. That is why I created a loop that lasts 0.8 seconds (stopMotor)

The problem is that the brake is only being applied when the motor moves right. But when it moves left, it does not get applied and coasts to a stop. I can't figure this out for the life of me. I really need to to brake and stop in either direction.

Here's the code.

const int IN1 = 13;
const int IN2 = 12;
const int Enable1 = 14;
const int buttonOpen = 2; // Yellow
const int buttonClose = 0; // Black
const int limit1 = 5;
const int limit2 = 4;

void setup()
{
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(Enable1, OUTPUT);
  pinMode(buttonOpen, INPUT);
  pinMode(buttonClose, INPUT);
  pinMode(limit1, INPUT);
  pinMode(limit2, INPUT);
}

void moveLeft()
{
  digitalWrite(Enable1, HIGH); // wakes up driver
  digitalWrite(IN1, LOW); // 
  digitalWrite(IN2, HIGH); // spins counter-clockwise
}

void moveRight()
{
  digitalWrite(Enable1, HIGH); // wakes up driver
  digitalWrite(IN1, HIGH); // 
  digitalWrite(IN2, LOW); // spins clockwise
}

void stopMotor() //brake motor for 0.1 seconds to avoid coasting then place             driver to sleep
{
  int period = 80L;       // 0.08 seconds
  for( int tStart = millis();  (millis()-tStart) < period;  ){
    digitalWrite(IN1, HIGH); // brake
    digitalWrite(IN2, HIGH); // brake
  }
  digitalWrite(Enable1, LOW); // Sleep driver
}

void loop(){ 
  // Move motor right
  if (digitalRead(buttonOpen) == LOW){
    while(digitalRead(limit2) == HIGH){
      moveRight();
      yield();   
    }
  }
  else
    (stopMotor());

  // Move motor left 
  if (digitalRead(buttonClose) == LOW){
    while(digitalRead(limit1) == HIGH){
      moveLeft();
      yield();  
    }
  }
  else
    (stopMotor());
}
  • I'm not sure, if this has to do with your problem, but the logic in your loop() is weird. When you hit a limit switch the code does not go directly to the stop function, because its in the else statement of the control button, which will not be evaluated, when you are inside its if statement. Insteat the stop Funktion will be executed in the next if statement ( for the other direction). Instead stop the Motor directly after it hit the limit switch – chrisl May 25 '18 at 9:29
  • And the yield() function does WLAN stuff in the ESP, right? As I remember this has to be executed often enough or the webserver, you may want to use in future, will be unresponsive. Since it may take relatively long time for the motor to hit the limit switch, you can execute it inside the while() loop. Or rewrite your code to be completely non-blocking – chrisl May 25 '18 at 9:36
  • 1
    Your whole methodology belies a complete misunderstanding of how the Arduino works. You do not need to constantly tell it "Go right!Go right!Go right!Go right!Go right!Go right!Go right!". You only need to do it once and it will keep going right until you tell it otherwise. What you need is a finite state machine to switch between different modes depending on the various external stimulii. – Majenko May 25 '18 at 9:45
  • Thanks guys. Yes the yield() function keeps the ESP from resetting since I have blocking code which times the ESP out. – Daniel Frenkel May 25 '18 at 13:42
  • And I found this code that uses a state machine in almost my exact same situation link. But I was very confused about this methodology so thanks for the link to that tutorial, I'll be sure to dive into it – Daniel Frenkel May 25 '18 at 13:44

Your Answer

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

Browse other questions tagged or ask your own question.