Stepper Motor Disabling

Hi all, I am really struggling with some stepper motor code.
I have attached the basic code where I am controlling the direction of 2 stepper motors using 4 buttons.

Button1 spins motor1 CW, and Button2 spins motor1 CCW. I want to implement a limit switch that, if pressed, Button1 will become disabled, even after the limit switch is released.

Button2 should then still work, spin motor1 CCW but also reset the limit switch, so if Button2 is pressed, Button1 can be pressed again.

Every way I try this, it ends up with motor1 juddering and stepping slower but still CW even when limit switch is held or pressed.

// Define constants
const int motor1_dir_pin = 0;     // Direction pin for Motor 1
const int motor1_step_pin = 1;    // Stepper pin for Motor 1
const int motor2_dir_pin = 3;     // Direction pin for Motor 2
const int motor2_step_pin = 4;    // Stepper pin for Motor 2

const int ms_pin = 6;             // MS1 & MS2 pins for both Motors

const int button5_MS_pin = 9;     // Microstep enabling button for both Motors
const int button4_acw_pin = 10;   // Anti-clockwise button for Motor 2
const int button3_cw_pin = 11;    // Clockwise button for Motor 2
const int button2_acw_pin = 12;   // Anti-clockwise button for Motor 1
const int button1_cw_pin = 13;    // Clockwise button for Motor 1

const int motor1_enable_pin = 2;  // Enable pin for Motor 1
const int motor2_enable_pin = 5;  // Enable pin for Motor 2

const int limitSwitch1 = 19;
const int limitSwitch2 = 18;
const int limitSwitch3 = 17;
const int limitSwitch4 = 16;

// Read the state of the directional buttons
bool button1_state;
bool button2_state;
bool button3_state;
bool button4_state;
bool limitSwitch1_state;
bool limitSwitch2_state;
bool limitSwitch3_state;
bool limitSwitch4_state;

void setup()
{
// Define output pins
 pinMode(motor1_step_pin,OUTPUT);
 pinMode(motor1_dir_pin, OUTPUT);
 pinMode(motor2_step_pin,OUTPUT);
 pinMode(motor2_dir_pin, OUTPUT);
 pinMode(ms_pin,OUTPUT);
 pinMode(button1_cw_pin, INPUT_PULLUP);
 pinMode(button2_acw_pin,INPUT_PULLUP);
 pinMode(button3_cw_pin, INPUT_PULLUP);
 pinMode(button4_acw_pin,INPUT_PULLUP);
 pinMode(button5_MS_pin, INPUT_PULLUP);
 pinMode(limitSwitch1, INPUT_PULLUP);
 pinMode(limitSwitch2, INPUT_PULLUP);
 pinMode(limitSwitch3, INPUT_PULLUP);
 pinMode(limitSwitch4, INPUT_PULLUP);

 pinMode(motor1_enable_pin, OUTPUT);
 pinMode(motor2_enable_pin, OUTPUT);
}

void loop()
{
button1_state = digitalRead(button1_cw_pin);
button2_state = digitalRead(button2_acw_pin);
button3_state = digitalRead(button3_cw_pin);
button4_state = digitalRead(button4_acw_pin);

limitSwitch1_state = digitalRead(limitSwitch1);
limitSwitch2_state = digitalRead(limitSwitch2);
limitSwitch3_state = digitalRead(limitSwitch3);
limitSwitch4_state = digitalRead(limitSwitch4);


// Read state of microstepping button
bool button5_state = digitalRead(button5_MS_pin);

// Set the microstepping based on button press
 if (button5_state == LOW)
  {digitalWrite(ms_pin, HIGH);}
  else
  {digitalWrite(ms_pin, LOW);} // If micostepping button is held, enable microstepping until release*/
  
  // Control motor 1 based on state of buttons 1 & 2
   if (button1_state == LOW) // Motor travels only when limit switch is not pressed
    {
    
      digitalWrite(motor1_dir_pin, LOW);
      digitalWrite(motor1_enable_pin, LOW);
          digitalWrite(motor1_step_pin, HIGH);
          delayMicroseconds(500);
          digitalWrite(motor1_step_pin, LOW);
          delayMicroseconds(500);            // When button 1 is pressed, Motor 1 rotates clockwise
    }
    else if (button2_state == LOW)
    {
      digitalWrite(motor1_dir_pin, HIGH);
      digitalWrite(motor1_enable_pin, LOW);
          digitalWrite(motor1_step_pin, HIGH);
          delayMicroseconds(500);
          digitalWrite(motor1_step_pin, LOW);
          delayMicroseconds(500);            // When button 2 is pressed, Motor 1 rotates anti-clockwise
    }
  //{
  // Control motor 2 based on state of buttons 3 & 4
    if (button3_state == LOW)
    {
      digitalWrite(motor2_dir_pin, HIGH);
      digitalWrite(motor2_enable_pin, LOW);
          digitalWrite(motor2_step_pin, HIGH);
          delayMicroseconds(500);
          digitalWrite(motor2_step_pin, LOW);
          delayMicroseconds(500);            // When button 3 is pressed, Motor 2 rotates clockwise
    }
    if (button4_state == LOW)
    {
      digitalWrite(motor2_dir_pin, LOW);
      digitalWrite(motor2_enable_pin, LOW);
          digitalWrite(motor2_step_pin, HIGH);
          delayMicroseconds(500);
          digitalWrite(motor2_step_pin, LOW);
          delayMicroseconds(500);            // When button 4 is pressed, Motor 2 rotates anti-clockwise
    }
}

[sterretje edit] fixed code tags
[/sterretje edit]

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help

As your topic does not relate to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum

@charlie_hix thanks for trying to use code tags; I've fixed your attempt.
Code tags are 3 backticks (not ticks) on their own line, one set before the code, one set after the code.

Thankyou for this! As you can tell I'm very new to all this coding work

As I posted earlier

if (!limit_switch) // limit switch pressed is LOW
  flag = 1; // set a flag to disable button 1 functions

if (!flag) { // if the limit switch has not set the flag
  // contains all button 1
}

You should make a circuit diagram.

Your drawing does not match your sketch. Please, make both match.

Is the model of motor driver DRV4988?

The power applied to Arduino Vin should be greater than 7vdc (and less than 12vdc). If you want to power the Arduino with 5vdc, use the 5V pin.

What model Arduino do you have? Pins 0 and 1 on an Uno or Nano are the Hardware Serial pins, and should never be used in your sketch (if your sketch is transmitting while you are trying to upload code, problems occur).

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