Arduino UNO + Stepper Motor (Nema SX17-1005VLQCEF) + drv8825 + 2 end stops

Hello guys,

I am here to ask for advice after day of struggling and searching all over the internet. Let me just say on the start that I am quite unexperienced in Arduino and coding, even I do some projects from time to time on it.

Now to my project. The main function of my project is stepper motor which goes clockwise and counter-clockwise according the last endstop pressed. To reel spool of filament (from 2pcs of 1kg filament making 1pc of 2kg filament) and to preserve the spacing between filament.

Functionality:

  1. Start on the Arduino, stepper is turned off
  2. Press zero button, stepper will go counter-clockwise till it hits the endstop1.
  3. Press start button, stepper will go clockwise till it hits the endstop2, then go counter-clockwise and so long back and forth until the start button is pressed again and the motor stops.
    (Not a topic right now: 4. The speed of stepper motor is controled by potentiometer)

My connection:
stepper + driver: https://www.instructables.com/id/Controll-a-Stepper-Motor-With-the-DRV8825/
note: except the gnd cable from 12v to 5v, all the same

buttons: https://www.instructables.com/id/5-Simple-Button-and-Led-Projects-with-Arduino/
note: except the LED

The only 2 things I could do were that I started Arduino and when one endstop was pressed then the rotation went until the limit of rotation was reached no matter if the other endstop was pressed. Or to just simply set number of degrees to rotate until it stopped. So I could not figure out how to stop the stepper by endstop.

As I said before, I am not much experienced with Arduino, but I would like to ask for advice here with code. Please could anybody help me to edit my code to functionality as written above? I would be very gratefull.

Many thanks, Tom

This is the farest I get today and I am struggling with it :(.

#include <Arduino.h>
#include "A4988.h"

// using a 200-step motor (most common)
#define MOTOR_STEPS 200
#define DIRECTION 8
#define STEP 9
#define MS1 10
#define MS2 11
#define MS3 12
A4988 stepper(MOTOR_STEPS, DIRECTION, STEP, MS1, MS2, MS3);

int ENDSTOP1 = 2;
int ENDSTOP2 = 4;
int ZEROBUTTON = 5;
int STARTSTOP = 6;

void setup() {
    // Set target motor RPM to 1RPM and microstepping to 8 (1/8 of full step mode)
    stepper.begin(1, 8);
    pinMode(DIRECTION, OUTPUT);
    digitalWrite(DIRECTION, LOW);
    
    pinMode(ENDSTOP1,INPUT);
    pinMode(ENDSTOP2,INPUT);
}

void loop() {
    // If button ZEROBUTTON pressed, start stepper counter-clockwise till it hits ENDSTOP1, then stop.
    // If STARTSTOP pressed, stepper goes clockwise till hits ENDSTOP2, then switch direction
    // to clockwise till hits ENDSTOP1, back and forth till the STARTSTOP is pressed again.
    
if(digitalRead(ENDSTOP1) == HIGH){
      digitalWrite(DIRECTION, HIGH);
      stepper.rotate(28.8);
      delay(50);
      }
if(digitalRead(ENDSTOP2) == HIGH){
      digitalWrite(DIRECTION, LOW);
      stepper.rotate(-28.8);
      delay(50);
}
}

You're doing what 99.9% of beginners do, hooking up switches without reading the first word of any instructions. You connect the switch between 5V and an input pin, so when the switch is pressed the pin reads HIGH, BUT, when the switch is NOT pressed the pin is connected to NOTHING, it's "floating" and could read HIGH, LOW or anything in between, so the processor might see button presses even when there aren't any. That can be fixed by connecting a PULLDOWN RESISTOR, (about 10k or so) between the input pin and ground. That will keep the pin pulled solidly LOW until a button press forces it to 5V (HIGH). The easiest way, since the AVR MCU has built in PULLUP resistors is to connect the switch between input pin and ground and enable the built in pullup for that pin, then the pin will read HIGH until until a button press forces it LOW. The logic is reversed but that is the standard way with digital logic. So connect ENDSTOP1 switch between ground and pin 2, then in setup():

pinMode(ENDSTOP1,INPUT_PULLUP);

In loop():

if(digitalRead(ENDSTOP1) == LOW){
      digitalWrite(DIRECTION, HIGH);
      stepper.rotate(28.8);
      delay(50);
}

And do the same with ENDSTOP2.

These links may help (in addition to what @JCA79B has said.
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

...R