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]
