Stepper motor control with buttons and limit switches

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.

#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]

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

Look here: Arduino - Stepper

Delta_G:
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

huskies_62:
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

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

It moves 849 steps, not 850.

TheMemberFormerlyKnownAsAWOL:
It moves 849 steps, not 850.

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

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

...R

Robin2:
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.

TheMemberFormerlyKnownAsAWOL:
The comment was for the benefit of the OP.

Apologies - also for forgetting to put a smiley in Reply #5

...R