Go Down

Topic: Stepper motor control with buttons and limit switches  (Read 93 times) previous topic - next topic

huskies_62

Hi,

I am trying to get a linear actuator set up. I am using a using a TB6600 controller on a nema 23 stepper motor. I have two buttons that I want to use to control the direction. I will use limit switches at either end to stop the stepper motor. I have found a code that moves the motor in either direction based on the buttons but it only is only for a limited amount of travel. I am struggling to figure out how to get it to run until it hits the limit switch. Any help would be greatly appreciated.

Code: [Select]


#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define EN  6

const int button1Pin = 7;
const int button2Pin = 8;
const int ledPin =  13;
int button1State = 0;
int button2State = 0;

char user_input;
int x;
int y;
int state;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
   
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(EN, OUTPUT);
  resetEDPins();
}

void loop() {
      button1State = digitalRead(button1Pin);
      button2State = digitalRead(button2Pin);
     
      if (button1State == HIGH) {
        digitalWrite(EN, LOW);
        StepForward();
        resetEDPins();
        delay(1000);
      }
      if (button2State == HIGH) {
        digitalWrite(EN, LOW);
        StepBack();
        resetEDPins();
        delay(1000);
      }else {
        digitalWrite(ledPin, LOW);
      }
}

void resetEDPins()
{
  digitalWrite(stp, LOW);
  digitalWrite(dir, LOW);
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(EN, HIGH);
}

void StepForward()
{
  digitalWrite(dir, LOW);

  for(x= 1; x<850; x++)
  {
        digitalWrite(stp,HIGH);
        delay(1);
        digitalWrite(stp,LOW);
        delay(1);
    }
}

void StepBack()
{
 digitalWrite(dir, HIGH);
  for(x= 1; x<850; x++)
  {
    digitalWrite(stp,HIGH);
    delay(1);
    digitalWrite(stp,LOW);
    delay(1);
  }
}
[code]

Delta_G

That's the hard way to code for a stepper.  The easy way is the Stepper library that comes with the IDE.

Look here:  https://www.arduino.cc/en/reference/stepper
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Robin2

That's the hard way to code for a stepper.  The easy way is the Stepper library that comes with the IDE.
Unfortunately the standard Stepper library is not intended for stepper motor drivers that take step and direction signals.

While the OP's code could be tidied up it seems to me not very "hard"

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

I have found a code that moves the motor in either direction based on the buttons but it only is only for a limited amount of travel.
The way your code is written it moves 850 steps every time you press the button with a delay() of 1000 millisecs between each group of moves.

And because the motor moves 850 steps in the FOR loop you cannot check for the limit switch.

Rather than check if the switch value is HIGH you need to check for when it changes from LOW to HIGH and then start a movement in the appropriate direction moving one step at a time and checking the limit switch between steps.

Something like this pseudo code

Code: [Select]
prevSwitchState = switchState
switchState = digitalRead(switchPin)
if (switchState == HIGH and prevSwitchState == LOw{
    motorMayMoveFwd = true;
}

if (motorMayMoveFwd == true) {
   // code to move one step
   if (limitSwitch is triggered) {
      motorMaayMoveFwd = false
   }
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.


Robin2

It moves 849 steps, not 850.
Gee golly - I'm real sorry I did not read the code carefully enough. :)

The difference is not material as far as my Reply #3 is concerned.


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TheMemberFormerlyKnownAsAWOL

Gee golly - I'm real sorry I did not read the code carefully enough.

The difference is not material as far as my Reply #3 is concerned.


...R
The comment was for the benefit of the OP.

Robin2

The comment was for the benefit of the OP.
Apologies - also for forgetting to put a smiley in Reply #5

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up