Help with 3-Phase Stepper motor Control and Forward/Reverse Relays

HI All
My Name is Joseph, I am looking to see if I could get some help getting my Arduino R3 Uno to control 2 3-Phase stepper motors and a forward/Reverse relay module to control a linear actuator. I also have 2 FUYU 1200mm Nema24 Stepper Motor Driven Linear rails that I am hoping to to have a frame that has the lift table that is to be controlled by the Forward/Reverse relay module. The relay module seems to operate with closing a switch and I am hoping the Arduino can do that but so far I haven't been able to figure that out. I am also looking to see if I can get help with controlling the stepper driver I have attached a link to each of the products mentioned and I have some rough code I Any help is appreciated.got from claude.ai for the rail.

// Pin definitions
const int STEP_PIN = 3;
const int DIR_PIN = 4;
const int ENABLE_PIN = 5;

// Motor specifications
const int STEPS_PER_REV = 200;

// Motor control variables
int motorSpeed = 60;  // Speed in RPM
bool motorRunning = false;
bool motorDirection = true;  // true = clockwise, false = counterclockwise
long stepsToMove = 0;
long stepsMoved = 0;

void setup() {
  Serial.begin(9600);
  
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(ENABLE_PIN, OUTPUT);
  
  digitalWrite(ENABLE_PIN, HIGH);  // Disable motor initially
  
  Serial.println("200-Step Stepper Motor Control");
  Serial.println("Commands:");
  Serial.println("s - Start/Stop motor");
  Serial.println("d - Change direction");
  Serial.println("f - Increase speed by 10 RPM");
  Serial.println("g - Decrease speed by 10 RPM");
  Serial.println("r<num> - Set number of revolutions (e.g., r2 for 2 revolutions)");
}

void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');
    processCommand(command);
  }
  
  if (motorRunning && stepsToMove > 0) {
    unsigned long stepDelay = calculateStepDelay(motorSpeed);
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(stepDelay);
    
    stepsMoved++;
    if (stepsMoved >= stepsToMove) {
      motorRunning = false;
      digitalWrite(ENABLE_PIN, HIGH);  // Disable motor
      Serial.println("Motion complete");
      stepsToMove = 0;
      stepsMoved = 0;
    }
  }
}

void processCommand(String command) {
  char cmd = command.charAt(0);
  switch (cmd) {
    case 's':
      toggleMotor();
      break;
    case 'd':
      changeDirection();
      break;
    case 'f':
      adjustSpeed(10);
      break;
    case 'g':
      adjustSpeed(-10);
      break;
    case 'r':
      setRevolutions(command.substring(1).toInt());
      break;
    default:
      Serial.println("Unknown command");
  }
}

void toggleMotor() {
  motorRunning = !motorRunning;
  digitalWrite(ENABLE_PIN, motorRunning ? LOW : HIGH);
  Serial.println(motorRunning ? "Motor started" : "Motor stopped");
}

void changeDirection() {
  motorDirection = !motorDirection;
  digitalWrite(DIR_PIN, motorDirection);
  Serial.println(motorDirection ? "Direction: Clockwise" : "Direction: Counterclockwise");
}

void adjustSpeed(int change) {
  motorSpeed += change;
  if (motorSpeed < 1) motorSpeed = 1;
  Serial.print("Speed set to ");
  Serial.print(motorSpeed);
  Serial.println(" RPM");
}

void setRevolutions(int revs) {
  stepsToMove = revs * STEPS_PER_REV;
  stepsMoved = 0;
  Serial.print("Set to move ");
  Serial.print(revs);
  Serial.println(" revolutions");
}

unsigned long calculateStepDelay(int rpm) {
  unsigned long totalMicrosecondsPerRevolution = 60L * 1000 * 1000 / rpm;
  return totalMicrosecondsPerRevolution / STEPS_PER_REV / 2;
}

Stepper Controlled Linear Rail

Controller for Linear Rail

Forward/Reverse Relay Module

Lift Table

Thanks Joseph

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