How do you use Limit Switches with a CNC Shield V3? (AccelStepper)

I've followed this tutorial on YouTube (Ping Pong with the AccelStepper library and two limit switches - Polling and interrupts - YouTube) but I can't seem to get this code working. I'm guessing it's probably because I've defined the pins for the limit switches incorrectly. What do you guys think?

Just for some context, I've hooked up the NO of my limit switch to the Y+ Endstop on the CNC Shield and the COM to the GND (which i'm assuming is the one next to the Y+). I've done the same with my 2nd limit switch but with the NO connected to Y- Endstop.

image

According to this diagram, the Y limit axis is pin 10 which is why I've defined it to be 10 in my code.

#include <AccelStepper.h>

const byte limitSwitch = 10;

bool switchFlipped = false;
bool previousFlip = true;

AccelStepper stepperY(1, 3, 6);

void setup() {
pinMode(limitSwitch, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(limitSwitch), FlipDirection, FALLING);

stepperY.setMaxSpeed(3000);
stepperY.setAcceleration(1000);
stepperY.setSpeed(500);
delay(500);
}

void loop() {
  stepperY.runSpeed();
  flipCheck();
}

void flipCheck() {
  if(switchFlipped == true){
    if(previousFlip == true){
      stepperY.setSpeed(500);
    }
    if(previousFlip == false);
      stepperY.setSpeed(-500);
    switchFlipped = false;
  }
}

void FlipDirection(){
  switchFlipped = true;
  previousFlip = !previousFlip;
}

Is there a better way to achieve the same "ping pong" movement using a different library? I've tried writing my own code with the stepper.h library but that also didn't turn out that great in the end. However I somehow got the limit switches to semi-work.

Since your code depends on interrupts, you need to use a pin that supports interrupts:

...says pins 2 or 3 have interrupts on the Uno, and since you are already using 3 for "step pulse y" you can only use pin 3. So figure out how to hook the limit switches to pin3/StepPulseX instead of pin 10.

Alternately, don't use interrupts, keep your same connections and try adapting something like the Files/Examples/Digital/StateChangeDetection to read pin 10, and once that work, blend those bits in with your sketch.

Get rid of the

;


With if(. . .) suggest you always use:
if(. . .)
{
//do something
}

#include <AccelStepper.h>

const byte limitSwitch = 10;

bool switchFlipped = false;
bool previousFlip = true;

AccelStepper stepperY(1, 3, 6);

void setup() {
pinMode(limitSwitch, INPUT_PULLUP);

stepperY.setMaxSpeed(3000);
stepperY.setAcceleration(1000);
stepperY.setSpeed(500);
delay(500);
}

void loop() {
  stepperY.runSpeed();
  flipCheck();
}
void flipCheck() {
  if(digitalRead(limitSwitch) == LOW){
    stepperY.setSpeed(-500);
  }
  if(digitalRead(limitSwitch) == HIGH){
    stepperY.setSpeed(500);
  }
}

So I've made some minor changes and now the stepper motor does run. However the direction of the stepper motor changes only when the limit switch is pressed down, I'd like it to change direction after being pressed once if that makes sense.

You mean when the switch is released.

yep

Read this:

If you have trouble understanding, ask back here.

2 Likes

thanks man :pray: the stepper motor is working the way I want it to now.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.