Integrating 4 Endstops into Code that lets 2 Stepper motors drive with a CNC Shi

Hello Community,

I am building up a module comparable to a H-Bot, but rather simple. There is just one stepper motor running for the x-Axis and another for y.

This is how I want to just let the belt drive drive around a sensor stopping every few centimeters to let the sensor measure at each point.

I have already put together a sketch that works:

#define EN 8

//Direction pin
#define X_DIR 5
#define Y_DIR 6

//Step pin
#define X_STP 2
#define Y_STP 3

#define MOTOR_X_ENABLE_PIN 8
#define MOTOR_X_STEP_PIN 2
#define MOTOR_X_DIR_PIN 5

#define MOTOR_Y_ENABLE_PIN 8
#define MOTOR_Y_STEP_PIN 3
#define MOTOR_Y_DIR_PIN 6

#define endstop_x1 9
#define endstop_y1 10

//DRV8825
int delayTime=300; //Delay between each pause (uS)
int stps=6400;// Steps to move

void step(boolean dir, byte dirPin, byte stepperPin, int steps)

{

digitalWrite(dirPin, dir);

delay(100);

for (int i = 0; i < steps; i++) {

digitalWrite(stepperPin, HIGH);

delayMicroseconds(delayTime);

digitalWrite(stepperPin, LOW);

delayMicroseconds(delayTime);

}

}

void setup(){

pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);

pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);

pinMode(EN, OUTPUT);

digitalWrite(EN, LOW);

}

void loop(){

step(true, X_DIR, X_STP, stps); //X, Counterclockwise

delay(1000);

step(true, X_DIR, X_STP, stps); //X, Counterclockwise

delay(1000);

step(true, X_DIR, X_STP, stps); //X, Counterclockwise

delay(1000);

step(true, X_DIR, X_STP, stps); //X, Counterclockwise

delay(1000);

step(true, X_DIR, X_STP, stps); //X, Counterclockwise

delay(1000);

step(false, Y_DIR, Y_STP, stps); //Y, Clockwise

delay(1000);

step(false, X_DIR, X_STP, stps); //X, Clockwise

delay(1000);

}

The only problem now is that I want to integrate 4 Endstoppers to the CNC Shield. The next step would be now to instead of following a certain route, the sensor should follow the x-axis in one direction until a end stop is reached. Then it should move one step on the y-axis and start on the x-axis again in the other direction until it reaches an endstop again.

In the code I just want to have it like this:

step(true, X_DIR, X_STP, stps); //X, Counterclockwise

if Endstop is hitted
step (false, Y-DIR, Y_STP, stps)
step false, X_DIR, X_STP, stps)

I have looked up a couple of possibilities, but I couldnt find any apropriate solution to include the Endstops into the Arduino sketch so far. Can you assist me somehow? I imagined it to be not that hard, but if anyone has some experiences with endstops it might be solved rather quickly. Thanks in advance!

Please modify your Post and use the Code button </> rather than the Quote button for your code.

If you want to detect an end-stop then you MUST use non-blocking code so that the stop-switch can be checked between every step. The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

The code in the second example in this Simple Stepper Code is non-blocking.

You may find that using the non-blocking run() or runSpeed() functions of the AccelStepper library make life easier.

...R
Stepper Motor Basics

Before each step, check the limit switch. If you want 4 separate endstops you will have to assign two more input pins.

const byte MOTOR_ENABLE_PIN = 8;


const byte MOTOR_X_STEP_PIN  = 2;
const byte MOTOR_X_DIR_PIN = 5;


const byte MOTOR_Y_STEP_PIN = 3;
const byte MOTOR_Y_DIR_PIN = 6;


const byte endstop_x1 = 9;
const byte endstop_y1 = 10;


//DRV8825
coinst int delayTime = 300; //Delay between each pause (uS)
int stps = 6400; // Steps to move


void stepX(int steps)
{
  byte limitPin;


  if (steps < 0)
  {
    digitalWrite(MOTOR_X_DIR_PIN, LOW);
    limitPin = endstop_x1;
    steps = -steps;
  }
  else
  {
    digitalWrite(MOTOR_X_DIR_PIN, HIGH);
    limitPin = endstop_x1;
  }


  for (int i = 0; i < steps; i++)
  {
    if (digitalRead(limitPin) == LOW)
      return;  // Already hit the limit switch
    digitalWrite(MOTOR_X_STEP_PIN, HIGH);
    delayMicroseconds(delayTime);
    digitalWrite(MOTOR_X_STEP_PIN, LOW);
    delayMicroseconds(delayTime);
  }
}


void stepY(int steps)
{
  byte limitPin;


  if (steps < 0)
  {
    digitalWrite(MOTOR_Y_DIR_PIN, LOW);
    limitPin = endstop_y1;
    steps = -steps;
  }
  else
  {
    digitalWrite(MOTOR_Y_DIR_PIN, HIGH);
    limitPin = endstop_y1;
  }


  for (int i = 0; i < steps; i++)
  {
    if (digitalRead(limitPin) == LOW)
      return;  // Already hit the limit switch
    digitalWrite(MOTOR_Y_STEP_PIN, HIGH);
    delayMicroseconds(delayTime);
    digitalWrite(MOTOR_Y_STEP_PIN, LOW);
    delayMicroseconds(delayTime);
  }
}


void setup()
{
  pinMode(MOTOR_X_DIR_PIN, OUTPUT);
  pinMode(MOTOR_X_STEP_PIN, OUTPUT);
  pinMode(endstop_x1, INPUT_PULLUP);


  pinMode(MOTOR_Y_DIR_PIN, OUTPUT);
  pinMode(MOTOR_Y_STEP_PIN, OUTPUT);
  pinMode(endstop_y1, INPUT_PULLUP);




  pinMode(MOTOR_ENABLE_PIN, OUTPUT);
  digitalWrite(MOTOR_ENABLE_PIN, LOW);
}


void loop()
{
  stepX(-stps); //X, Counterclockwise
  delay(1000);
  stepX(-stps); //X, Counterclockwise
  delay(1000);
  stepX(-stps); //X, Counterclockwise
  delay(1000);
  stepX(-stps); //X, Counterclockwise
  delay(1000);
  stepX(-stps); //X, Counterclockwise
  delay(1000);
  stepY(stps); //Y, Clockwise
  delay(1000);
  stepX(stps); //X, Clockwise
  delay(1000);
}

Wow thank you so much this is working just perfetly!

I didn't expect the solution coming so quick :smiley: