SOLVED Stepper motor with limit switches and direction switch

I started this project some weeks ago, posted about it here and got some feedback that seemed to say "go away, tidy up your sketch and think more about what you want it to do".
I have done exactly that but as I cannot find the original thread I am starting again.

The project is a slide driven back and forth by a stepper motor, there is a changeover microswitch at each end of the travel and a single pole changeover switch to restart in the opposite direction.

I am using 4 pins to sense and control the direction of travel, and have a working stepper motor setup.

Attached below a schematic of the wiring of the whole setup and a diagram of the switch connections with a logic grid showing which pins are high (black blob) through the phases of movement.

The switching side of things has been tested with LED's in all 4 pins and it follows the expected logic.
The motor runs in both directions using the restart switch.

I do not have the belt drive connected at present, this is a good thing and here is why!

The motor does not stop as expected when the carriage is moved to the end of the slide rails, my sketch is meant to look for a low signal on pin 8 or 9 whilst either 11 or 12 are high (other pin will be low) and stop the motor, until the switch is thrown and the motor restarts in the other direction.

// constants won't change. They're used here to set pin numbers:
const int ForwardPin = 12;     // the number of the pin that when high sends slide forward
const int StopForwardPin = 8;     // the number of the pin that stops motor when forward
const int BackwardPin = 11;     // the number of the pin that when high sends slide backward
const int StopBackwardPin = 9;  // the number of the pin that stops the motor when backward
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin

// variables will change:
boolean ForwardState = 0;         // variable for reading the micro-switch status
boolean StopForwardState = 0;     // variable that stops motor when forward
boolean BackwardState = 0;   // variable for reading the microswitch status
boolean StopBackwardState = 0;  //variable that stops the motor when backward

int slidePosition = 2;  // Tells progam what position the slide is in


/**
    setup inputs for switches and outs for motor pins
    serial begin to read the switches to test for errors
*/
void setup() {
  // initialize the pin as an inputs:
  pinMode(smDirectionPin, OUTPUT);
  pinMode(smStepPin, OUTPUT);
  // initialize the switch pins as inputs:
  pinMode(ForwardPin, INPUT);
  pinMode(StopForwardPin, INPUT);
  pinMode(BackwardPin, INPUT);
  pinMode(StopBackwardPin, INPUT);
  Serial.begin(9600);
}

/**
   this function turns motor foward
*/
void SlideForward() {
  // turn motor foward:
  digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
  /*turns the motor 1 step*/
  for (int i = 0; i < 1; i++)
    digitalWrite(smStepPin, HIGH);
  delayMicroseconds(70);
  digitalWrite(smStepPin, LOW);
  delayMicroseconds(70);
}

/**
   this function turns motor backwards
*/
void SlideBackward() {
  digitalWrite(smDirectionPin, LOW); //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
  /*Turns the motor fast 1 step*/
  for (int i = 0; i < 1; i++)
    digitalWrite(smStepPin, HIGH);
  delayMicroseconds(70);
  digitalWrite(smStepPin, LOW);
  delayMicroseconds(70);
}

/**
   Stop the motor form moving
*/
void stopSlideFromMoving() {
  digitalWrite(smStepPin, LOW);
}

void loop() {
  // read the state of the switches value:
  ForwardState = digitalRead(ForwardPin);
  StopForwardState = digitalRead(StopForwardPin);
  BackwardState = digitalRead(BackwardPin);
  StopBackwardState = digitalRead(StopBackwardPin);


  // check which pin 11 or 12 is HIGH:
  if (ForwardState == HIGH,
      StopForwardState == HIGH,
      BackwardState == LOW) {
    SlideForward();
  } else if (ForwardState == HIGH,
             StopForwardState == LOW,
             BackwardState == LOW) {
    stopSlideFromMoving();
  }

  if (BackwardState == HIGH,
      StopBackwardState == HIGH,
      ForwardState == LOW) {
    SlideBackward();
  } else if (BackwardState == HIGH,
             StopBackwardState == LOW,
             ForwardState == LOW) {
    stopSlideFromMoving();
  }
}

Here is my sketch, can anyone point me in the right direction to get it running please?

Look under your profile for your posts.
https://forum.arduino.cc/index.php?topic=615440.msg4170675#msg4170675

Time to look at the reference section for ‘if’ :

if (ForwardState == HIGH,
StopForwardState == HIGH,
BackwardState == LOW)

Maybe you wanted:

if (ForwardState == HIGH && StopForwardState == HIGH && BackwardState == LOW)

I don't see the need for double throw end switches, the switch is either closed or it's not. Pseudo code:

if(startFwd_switch && fwdEnd_switch)
  runFwd;
else
  stop;
if(startRev_switch && revEnd_switch)
 runRev;
else
  stop;

Maybe you wanted:

if (ForwardState == HIGH && StopForwardState == HIGH && BackwardState == LOW)

Thankyou, this is my first ever try at any kind of programing, I sort of guessed I had the syntax wrong somewhere, I will head for the workshop and redo the sketch and report back :slight_smile:

I don't see the need for double throw end switches, the switch is either closed or it's not. Pseudo code:

I am still mulling over that one... It may be that I am used to DC motors where you need the other pole with attached diodes to enable restarting the motor in the other direction....

I did try the following setup using a single pin to detect the end stop and diodes to stop the back EMF.
Would that have worked if I had got my sketch right?

1 Like

It works! Thank you Larryd
It works exactly as intended, I had been looking all over the internet for a project that worked like this, all I ever found were questions never answers.

I have posted the final sketch in case it is helpful for others, I like to share :slight_smile:

// constants won't change. They're used here to set pin numbers:
const int ForwardPin = 12;     // the number of the pin that when high sends slide forward
const int StopForwardPin = 8;     // the number of the pin that stops motor when forward
const int BackwardPin = 11;     // the number of the pin that when high sends slide backward
const int StopBackwardPin = 9;  // the number of the pin that stops the motor when backward
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin

// variables will change:
boolean ForwardState = 0;         // variable for reading the micro-switch status
boolean StopForwardState = 0;     // variable that stops motor when forward
boolean BackwardState = 0;   // variable for reading the microswitch status
boolean StopBackwardState = 0;  //variable that stops the motor when backward

int slidePosition = 2;  // Tells progam what position the slide is in


/**
    setup inputs for switches and outs for motor pins
    serial begin to read the switches to test for errors
*/
void setup() {
  // initialize the pin as an inputs:
  pinMode(smDirectionPin, OUTPUT);
  pinMode(smStepPin, OUTPUT);
  // initialize the switch pins as inputs:
  pinMode(ForwardPin, INPUT);
  pinMode(StopForwardPin, INPUT);
  pinMode(BackwardPin, INPUT);
  pinMode(StopBackwardPin, INPUT);
  Serial.begin(9600);
}

/**
   this function turns motor foward
*/
void SlideForward() {
  // turn motor foward:
  digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
  /*turns the motor 1 step*/
  for (int i = 0; i < 1; i++)
    digitalWrite(smStepPin, HIGH);
  delayMicroseconds(70);
  digitalWrite(smStepPin, LOW);
  delayMicroseconds(70);
}

/**
   this function turns motor backwards
*/
void SlideBackward() {
  digitalWrite(smDirectionPin, LOW); //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
  /*Turns the motor fast 1 step*/
  for (int i = 0; i < 1; i++)
    digitalWrite(smStepPin, HIGH);
  delayMicroseconds(70);
  digitalWrite(smStepPin, LOW);
  delayMicroseconds(70);
}

/**
   Stop the motor form moving
*/
void stopSlideFromMoving() {
  digitalWrite(smStepPin, LOW);
}

void loop() {
  // read the state of the switches value:
  ForwardState = digitalRead(ForwardPin);
  StopForwardState = digitalRead(StopForwardPin);
  BackwardState = digitalRead(BackwardPin);
  StopBackwardState = digitalRead(StopBackwardPin);


  // check which pin 11 or 12 is HIGH:
  if (ForwardState == HIGH && StopForwardState == HIGH && BackwardState == LOW) {
    SlideForward();
  } else if (ForwardState == HIGH && StopForwardState == LOW && BackwardState == LOW) {
    stopSlideFromMoving();
  }

  if (BackwardState == HIGH && StopBackwardState == HIGH && ForwardState == LOW) {
    SlideBackward();
  } else if (BackwardState == HIGH && StopBackwardState == LOW && ForwardState == LOW) {
    stopSlideFromMoving();
  }
}

Is it possible to amend the thread title to say solved?

1 Like

“Is it possible to amend the thread title to say solved?”

You can do this by editing your first post and changing the title.

Done, thank you for your help.