Trouble with 4 steppers

Hello All I am having an issue with my code. I am trying to run two steppers 3&4 to a know distance, and then run stepper 1 from limit to limit while stepper 2 runs to limit and back one time. Then all stop. I can get either 3&4 to run or 1&2 to run but not both in the same loop and I'm only running 1&2 together? Any ideas would be very helpful. Thank you.

void loop() {
  // Main control loop
  switch (currentState) {
    case IDLE:
      // Check if both buttons are selected
      if (selectedButton1 > 0 && selectedButton2 > 0) {
        // Initialize distances for steppers based on button selections
        int moveDistance3 = 0;  // Distance for Stepper 3
        int moveDistance4 = 0;  // Distance for Stepper 4

        // Determine movement distances based on selected buttons
        if ((selectedButton1 == 1) && (selectedButton2 >= 4)) {
          moveDistance4 = distance1;
          moveDistance3 = distance1;

        } else if ((selectedButton1 == 1) && (selectedButton2 >= 5)) {
          moveDistance3 = distance2;
          moveDistance4 = distance2;
        } else if ((selectedButton1 == 1) && (selectedButton2 >= 6)) {
          moveDistance3 = distance3;
          moveDistance4 = distance3;
        } else if ((selectedButton1 == 2) && (selectedButton2 >= 4)) {
          moveDistance3 = distance1;

        } else if ((selectedButton1 == 2) && (selectedButton2 >= 5)) {
          moveDistance3 = distance2;

        } else if ((selectedButton1 == 2) && (selectedButton2 >= 6)) {
          moveDistance3 = distance3;

        } else if ((selectedButton1 == 3) && (selectedButton2 >= 4)) {
          moveDistance4 = distance1;

        } else if ((selectedButton1 == 3) && (selectedButton2 >= 5)) {
          moveDistance4 = distance2;

        } else if ((selectedButton1 == 3) && (selectedButton2 >= 6)) {
          moveDistance4 = distance3;
        }

        // Move the steppers if distances are set
        if (moveDistance3 > 0) stepper3.move(moveDistance3);
        if (moveDistance4 > 0) stepper4.move(moveDistance4);

        // Transition to moving steppers state
        currentState = MOVING_STEPPERS;
      }
      break;

    case MOVING_STEPPERS:
      // Run both steppers simultaneously
      stepper3.run();
      stepper4.run();

      // Check if both steppers have completed their movements
      if (stepper3.distanceToGo() == 0 && stepper4.distanceToGo() == 0) {
        // After movement, transition to slice mode
        currentState = SLICE_MODE;
      }
      break;

    case SLICE_MODE:
      // Call the Slice function to move stepper 1 and stepper 2
      Slice();

      // Check completion condition for the Slice
      if (sliceCompleted) {
        // Once Slice is completed, reset everything to IDLE state
        resetSystem();        // Reset variables for the next cycle
        currentState = IDLE;  // Transition back to idle
      }
      break;
  }
}

void Slice() {
  int speed = 8000;                   // Reduced speed value
  static bool direction1 = false;     // false for moving towards limit5, true for limit6 (Stepper 1)
  static bool direction2 = false;     // false for moving towards limit7, true for limit8 (Stepper 2)
  static bool tripCompleted = false;  // Flag to track if Stepper 2 has completed the trip

  // Check if the trip is completed
  if (!tripCompleted) {
    // Control Stepper 1 (Vertical Stepper)
    if (direction1 == false) {
      // Move Stepper 1 towards limit5 UP
      stepper1.setSpeed(-speed);
      stepper1.runSpeed();
      if (digitalRead(limit6) == LOW) {  // Assuming LOW means limit5 is reached
        direction1 = true;               // Change direction to DOWN
      }
    } else {
      // Move Stepper 1 towards limit6 DOWN
      stepper1.setSpeed(speed);
      stepper1.runSpeed();
      if (digitalRead(limit5) == LOW) {  // Assuming LOW means limit6 is reached
        direction1 = false;              // Change direction to UP
      }
    }

    // Control Stepper 2 (Horizontal Stepper)
    if (direction2 == false) {
      // Move Stepper 2 towards limit7
      stepper2.setSpeed(-speed);
      stepper2.runSpeed();
      if (digitalRead(limit8) == LOW) {  // Assuming LOW means limit7 is reached
        direction2 = true;               // Change direction to limit8
      }
    } else {
      // Move Stepper 2 towards limit8
      stepper2.setSpeed(speed);
      stepper2.runSpeed();
      if (digitalRead(limit7) == LOW) {  // Assuming LOW means limit8 is reached
        tripCompleted = true;            // Mark trip as complete and stop both steppers
      }
    }
  } else {
    // Trip completed, stop both steppers
    stepper1.stop();        // Stop Stepper 1
    stepper2.stop();        // Stop Stepper 2
    sliceCompleted = true;  // Mark slice as completed
  }
}

void resetSystem() {
  // Reset selected button variables and state flags
  selectedButton1 = 0;
  selectedButton2 = 0;
  steppersMoved = false;
  sliceCompleted = false;
}

Please, post your complete sketch (on every question about sketches).

You would need to "time-share" each motor to run "simultaneously"... but even "simultaneous" is with one processor is micro-stepping each motor one step at a time. You would need four processors controlling the four motor drivers independently.

Was your code "computer" generated?

1 Like

Sorry about that, I wasn't sure of the protocol if it was allowed to post a whole code. Thanks for the response and here is the whole code:

#include <TouchScreen.h>
#include <LCDWIKI_GUI.h>
#include <LCDWIKI_KBV.h>
#include <AccelStepper.h>
LCDWIKI_KBV my_lcd(ILI9488, A3, A2, A1, A0, A4);
#define YP A3
#define XM A2
#define YM 9
#define XP 8

#define TS_MINX 950  // Increase this slightly to shift touch rightward
#define TS_MAXX 90   // Decrease slightly to shift touch leftward
#define TS_MINY 170  // Increase to improve the top part sensitivity
#define TS_MAXY 920  // Decrease slightly to adjust the bottom touch

#define OFFSET_Y -5  // Slightly move up all buttons

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define NAVY 0x000F        /*   0,   0, 128 */
#define DARKGREEN 0x03E0   /*   0, 128,   0 */
#define DARKCYAN 0x03EF    /*   0, 128, 128 */
#define MAROON 0x7800      /* 128,   0,   0 */
#define PURPLE 0x780F      /* 128,   0, 128 */
#define OLIVE 0x7BE0       /* 128, 128,   0 */
#define LIGHTGREY 0xC618   /* 192, 192, 192 */
#define DARKGREY 0x7BEF    /* 128, 128, 128 */
#define ORANGE 0xFD20      /* 255, 165,   0 */
#define GREENYELLOW 0xAFE5 /* 173, 255,  47 */
#define PINK 0xF81F        /* 255,   0, 255 */

#define MINPRESSURE 10
#define MAXPRESSURE 4000
#define SLICING_BUTTON_HEIGHT 400  // Decreased from 470
#define BUTTON_OFFSET_Y 20         // Shift all buttons up

bool button1Pressed = false;
bool button2Pressed = false;
bool button3Pressed = false;
bool button4Pressed = false;
bool button5Pressed = false;
bool button6Pressed = false;
bool stopLoop = false;

int limit1 = 22;  // Grey
int limit2 = 24;  // Purple
int limit3 = 26;  // Sausage Stepper Home
int limit4 = 28;  // Cheese Stepper Home
int limit5 = 30;  // Vertical Limit up
int limit6 = 32;  // Vertical Limit Down
int limit7 = 50;  // Blade Horizontal Home
int limit8 = 52;  // Blade Horizontal Limit

int StepPin1 = 36;              // Vertical Stepper
int DirPin1 = 34;               // Vertical Stepper
int StepPin2 = 40;              // Horizontal Stepper
int DirPin2 = 38;               // Horizontal Stepper
int StepPin3 = 46;              // Sausage Paddle Stepper
int DirPin3 = 48;               // Sausage Paddle Stepper
int StepPin4 = 42;              // Cheese Paddle Stepper
int DirPin4 = 44;               // Cheese Paddle Stepper
int stepperDirection = 1;       // 1 for up, -1 for down

int selectedButton1 = 0; // Variable to store the state of Button 1
int selectedButton2 = 0; // Variable to store the state of Button 2
bool newSelectionMade();
bool stopMotor = false;         // Flag to stop the motor when needed
bool headMoveEnabled = false;   // Flag to control when HeadMove can run
bool stepper1Completed = false;  // Initially false, set to true when both steppers are
bool stepper2Completed = false;
bool direction1 = false;
bool direction2 = false;



AccelStepper stepper1(1, StepPin1, DirPin1);
AccelStepper stepper2(1, StepPin2, DirPin2);
AccelStepper stepper3(1, StepPin3, DirPin3);
AccelStepper stepper4(1, StepPin4, DirPin4);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


void setup() {

  Serial.begin(9600);
  my_lcd.Init_LCD();
  Serial.println(my_lcd.Read_ID(), HEX);
  my_lcd.Fill_Screen(BLACK);

  pinMode(limit1, INPUT);
  pinMode(limit2, INPUT);
  pinMode(limit3, INPUT);
  pinMode(limit4, INPUT);
  pinMode(limit5, INPUT);
  pinMode(limit6, INPUT);
  pinMode(limit7, INPUT);
  pinMode(limit8, INPUT);

  stepper1.setMaxSpeed(8000);
  stepper1.setAcceleration(8000);
  stepper2.setMaxSpeed(4000);
  stepper2.setAcceleration(4000);
  stepper3.setMaxSpeed(4000);
  stepper3.setAcceleration(4000);
  stepper4.setMaxSpeed(4000);
  stepper4.setAcceleration(4000);

  //Button 1
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 85, 5, 5, 25);
  my_lcd.Set_Draw_color(RED);
  my_lcd.Fill_Round_Rectangle(160, 80, 10, 10, 25);
  my_lcd.Set_Text_Size(2);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(RED);
  my_lcd.Print("Cheese &", 40, 20);
  my_lcd.Print("Sausage", 40, 50);

  //Button 2
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 85, 5, 165, 25);
  my_lcd.Set_Draw_color(ORANGE);
  my_lcd.Fill_Round_Rectangle(160, 90, 10, 160, 25);
  my_lcd.Set_Text_Size(2);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(ORANGE);
  my_lcd.Print("Cheese Only", 15, 120);

  //Button 3
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 168, 5, 248, 25);
  my_lcd.Set_Draw_color(BLUE);
  my_lcd.Fill_Round_Rectangle(160, 173, 10, 243, 25);
  my_lcd.Set_Text_Size(2);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(BLUE);
  my_lcd.Print("Sausage Only", 15, 200);

  //Button 4
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 85, 315, 5, 25);
  my_lcd.Set_Draw_color(YELLOW);
  my_lcd.Fill_Round_Rectangle(170, 80, 310, 10, 25);
  my_lcd.Set_Text_Size(3);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(YELLOW);
  my_lcd.Print("Thick", 200, 40);

  //Button 5
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 165, 315, 85, 25);
  my_lcd.Set_Draw_color(GREEN);
  my_lcd.Fill_Round_Rectangle(170, 160, 310, 90, 25);
  my_lcd.Set_Text_Size(3);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(GREEN);
  my_lcd.Print("MEDIUM", 190, 110);

  //Button 6
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 168, 315, 248, 25);
  my_lcd.Set_Draw_color(PURPLE);
  my_lcd.Fill_Round_Rectangle(170, 173, 310, 243, 25);
  my_lcd.Set_Text_Size(3);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(PURPLE);
  my_lcd.Print("THIN", 200, 195);

  //Advertisement
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 255, 5, 475, 25);
  my_lcd.Set_Draw_color(PINK);
  my_lcd.Fill_Round_Rectangle(160, 260, 10, 470, 25);
  my_lcd.Set_Text_Size(3);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(DARKCYAN);
  my_lcd.Print("WWW.", 300, 280);

  //TEMP
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 255, 315, 475, 25);
  my_lcd.Set_Draw_color(DARKCYAN);
  my_lcd.Fill_Round_Rectangle(170, 260, 310, 470, 25);


  AllHome();
}



void loop() {
  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  // Variables for stepper movement distances
  int distance1 = 1000;  // Distance for Stepper 3 movement
  int distance2 = 2000;  // Distance for Stepper 4 movement
  int distance3 = 3000;  // Distance for Stepper 3 movement

  // Read touch screen
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, my_lcd.Get_Display_Width());
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, my_lcd.Get_Display_Height());

    if (p.z > ts.pressureThreshhold) {
      // Check which button is pressed for Stepper 3 (Buttons 1-3)
      if ((p.x > 0 && p.x < 160) && (p.y > 0 && p.y < 80)) {
        selectedButton1 = 1;  // Button 1 selected
        Serial.println("Button 1 selected");
      } else if ((p.x > 0 && p.x < 160) && (p.y > 100 && p.y < 180)) {
        selectedButton1 = 2;  // Button 2 selected
        Serial.println("Button 2 selected");
      } else if ((p.x > 0 && p.x < 160) && (p.y > 200 && p.y < 280)) {
        selectedButton1 = 3;  // Button 3 selected
        Serial.println("Button 3 selected");
      }

      // Check which button is pressed for Stepper 4 (Buttons 4-6)
      if ((p.x > 160 && p.x < 320) && (p.y > 0 && p.y < 80)) {
        selectedButton2 = 4;  // Button 4 selected
        Serial.println("Button 4 selected");
      } else if ((p.x > 160 && p.x < 320) && (p.y > 100 && p.y < 180)) {
        selectedButton2 = 5;  // Button 5 selected
        Serial.println("Button 5 selected");
      } else if ((p.x > 160 && p.x < 320) && (p.y > 200 && p.y < 280)) {
        selectedButton2 = 6;  // Button 6 selected
        Serial.println("Button 6 selected");
      }
    }
  }

  // Check if both buttons are selected
if (selectedButton1 > 0 && selectedButton2 > 0) {
    // Initialize distances for steppers based on button selections
    int moveDistance3 = 0;  // Distance for Stepper 3
    int moveDistance4 = 0;  // Distance for Stepper 4

    // Determine movement distances based on selected buttons
    if (selectedButton1 == 1) {
        moveDistance3 = distance1;  // Move Stepper 3 by distance1
        if (selectedButton2 == 4) {
            moveDistance4 = distance1;  // Move Stepper 4 by distance1
        } else if (selectedButton2 == 5) {
            moveDistance4 = distance2;  // Move Stepper 4 by distance2
        } else if (selectedButton2 == 6) {
            moveDistance4 = distance3;  // Move Stepper 4 by distance3
        }
    } else if (selectedButton1 == 2) {
        if (selectedButton2 == 4 || selectedButton2 == 5) {
            moveDistance3 = distance2;  // Move Stepper 3 by distance2
        } else if (selectedButton2 == 6) {
            moveDistance3 = distance3;  // Move Stepper 3 by distance3
        }
    } else if (selectedButton1 == 3) {
        if (selectedButton2 == 4) {
            moveDistance4 = distance1;  // Move Stepper 4 by distance1
        } else if (selectedButton2 == 5) {
            moveDistance4 = distance2;  // Move Stepper 4 by distance2
        } else if (selectedButton2 == 6) {
            moveDistance4 = distance3;  // Move Stepper 4 by distance3
        }
    }

    // Set the movement distances for both steppers
    if (moveDistance3 > 0) stepper3.move(moveDistance3);
    if (moveDistance4 > 0) stepper4.move(moveDistance4);

    // Run both steppers simultaneously
    bool isMoving = true;
    while (isMoving) {
        stepper3.run();
        stepper4.run();
        isMoving = (stepper3.distanceToGo() != 0 || stepper4.distanceToGo() != 0);
    }

    // Reset button selections
    selectedButton1 = 0;
    selectedButton2 = 0;
}

// Only call HeadHome after both steppers have completed their movements
if (stepper3.distanceToGo() == 0 && stepper4.distanceToGo() == 0) {
    HeadHome();  // Now it's safe to call HeadHome
}
}


void HeadHome() {
  int speed = 8000;                // Reduced speed value
  static bool direction1 = false;  // false for moving towards limit5, true for limit6 (Stepper 1)
  static bool direction2 = false;  // false for moving towards limit7, true for limit8 (Stepper 2)
  static bool tripCompleted = false;  // Flag to track if Stepper 2 has completed the trip

  // Check if the trip is completed
  if (!tripCompleted) {
    // Control Stepper 1 (Vertical Stepper)
    if (direction1 == false) {
      // Move Stepper 1 towards limit5 UP
      stepper1.setSpeed(-speed);
      stepper1.runSpeed();
      if (digitalRead(limit6) == LOW) {  // Assuming LOW means limit5 is reached
        direction1 = true;               // Change direction to DOWN
      }
    } else {
      // Move Stepper 1 towards limit6 DOWN
      stepper1.setSpeed(speed);
      stepper1.runSpeed();
      if (digitalRead(limit5) == LOW) {  // Assuming LOW means limit6 is reached
        direction1 = false;              // Change direction to UP
      }
    }

    // Control Stepper 2 (Horizontal Stepper)
    if (direction2 == false) {
      // Move Stepper 2 towards limit7
      stepper2.setSpeed(-speed);
      stepper2.runSpeed();
      if (digitalRead(limit8) == LOW) {  // Assuming LOW means limit7 is reached
        direction2 = true;               // Change direction to limit8
      }
    } else {
      // Move Stepper 2 towards limit8
      stepper2.setSpeed(speed);
      stepper2.runSpeed();
      if (digitalRead(limit7) == LOW) {  // Assuming LOW means limit8 is reached
        tripCompleted = true;            // Mark trip as complete and stop both steppers
      }
    }
  } else {
    // Trip completed, stop both steppers
    stepper1.stop();  // Stop Stepper 1
    stepper2.stop();  // Stop Stepper 2
  }
}













void AllHome() {
  // Homing routine for all steppers
  // Homing for vertical stepper
  while (digitalRead(limit5)) {
    stepper1.setSpeed(4000);
    stepper1.runSpeed();
  }

  while (!digitalRead(limit5)) {
    stepper1.setSpeed(-4000);
    stepper1.runSpeed();
  }

  while (digitalRead(limit7)) {
    stepper2.setSpeed(4000);
    stepper2.runSpeed();
  }
  stepper2.setSpeed(-4000);
  while (!digitalRead(limit7)) {
    stepper2.runSpeed();
  }
  stepper2.setSpeed(0);  // Stop the stepper

  // Homing for sausage paddle stepper
  while (digitalRead(limit4)) {
    stepper3.setSpeed(-4000);
    stepper3.runSpeed();
  }
  stepper3.setSpeed(4000);
  while (!digitalRead(limit4)) {
    stepper3.runSpeed();
  }
  stepper3.setSpeed(0);  // Stop the stepper

  // Homing for cheese paddle stepper
  while (digitalRead(limit3)) {
    stepper4.setSpeed(-4000);
    stepper4.runSpeed();
  }
  stepper4.setSpeed(4000);
  while (!digitalRead(limit3)) {
    stepper4.runSpeed();
  }
  stepper4.setSpeed(0);  // Stop the stepper
}

type or paste code here

OMG, I am very sorry guys, I posted the wrong code, please ignore the previous post. Here is the correct code:

#include <TouchScreen.h>
#include <LCDWIKI_GUI.h>
#include <LCDWIKI_KBV.h>
#include <AccelStepper.h>
LCDWIKI_KBV my_lcd(ILI9488, A3, A2, A1, A0, A4);
#define YP A3
#define XM A2
#define YM 9
#define XP 8

#define TS_MINX 950  // Increase this slightly to shift touch rightward
#define TS_MAXX 90   // Decrease slightly to shift touch leftward
#define TS_MINY 170  // Increase to improve the top part sensitivity
#define TS_MAXY 920  // Decrease slightly to adjust the bottom touch

#define OFFSET_Y -5  // Slightly move up all buttons

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define NAVY 0x000F        /*   0,   0, 128 */
#define DARKGREEN 0x03E0   /*   0, 128,   0 */
#define DARKCYAN 0x03EF    /*   0, 128, 128 */
#define MAROON 0x7800      /* 128,   0,   0 */
#define PURPLE 0x780F      /* 128,   0, 128 */
#define OLIVE 0x7BE0       /* 128, 128,   0 */
#define LIGHTGREY 0xC618   /* 192, 192, 192 */
#define DARKGREY 0x7BEF    /* 128, 128, 128 */
#define ORANGE 0xFD20      /* 255, 165,   0 */
#define GREENYELLOW 0xAFE5 /* 173, 255,  47 */
#define PINK 0xF81F        /* 255,   0, 255 */

#define MINPRESSURE 10
#define MAXPRESSURE 4000
#define SLICING_BUTTON_HEIGHT 400  // Decreased from 470
#define BUTTON_OFFSET_Y 20         // Shift all buttons up

bool button1Pressed = false;
bool button2Pressed = false;
bool button3Pressed = false;
bool button4Pressed = false;
bool button5Pressed = false;
bool button6Pressed = false;
bool stopLoop = false;



int limit1 = 22;  // Grey
int limit2 = 24;  // Purple
int limit3 = 26;  // Sausage Stepper Home
int limit4 = 28;  // Cheese Stepper Home
int limit5 = 30;  // Vertical Limit up
int limit6 = 32;  // Vertical Limit Down
int limit7 = 16;  // Blade Horizontal Home
int limit8 = 15;  // Blade Horizontal Limit

int StepPin1 = 36;         // Vertical Stepper
int DirPin1 = 34;          // Vertical Stepper
int StepPin2 = 40;         // Horizontal Stepper
int DirPin2 = 38;          // Horizontal Stepper
int StepPin3 = 46;         // Sausage Paddle Stepper
int DirPin3 = 48;          // Sausage Paddle Stepper
int StepPin4 = 42;         // Cheese Paddle Stepper
int DirPin4 = 44;          // Cheese Paddle Stepper
int stepperDirection = 1;  // 1 for up, -1 for down

int selectedButton1 = 0;  // Variable to store the state of Button 1
int selectedButton2 = 0;  // Variable to store the state of Button 2

bool stepper1Completed = false;  // Initially false, set to true when both steppers are
bool stepper2Completed = false;
bool currentState = false;

int moveDistance3 = 0;
int moveDistance4 = 0;
int distance1 = 1000;
int distance2 = 2000;
int distance3 = 3000;


bool sliceCompleted = false;
bool tripCompleted = false;
bool direction1 = true;  // or false based on your logic
bool direction2 = true;  // or false based on your logic
bool steppersMoved = false;

enum State {
  IDLE,
  MOVING_STEPPERS,
  SLICE_MODE,
  // Add other states as needed
};

//State currentState = IDLE; // Initialize currentState


AccelStepper stepper1(1, StepPin1, DirPin1);
AccelStepper stepper2(1, StepPin2, DirPin2);
AccelStepper stepper3(1, StepPin3, DirPin3);
AccelStepper stepper4(1, StepPin4, DirPin4);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


void setup() {

  Serial.begin(9600);
  my_lcd.Init_LCD();
  Serial.println(my_lcd.Read_ID(), HEX);
  my_lcd.Fill_Screen(BLACK);

  pinMode(limit1, INPUT);
  pinMode(limit2, INPUT);
  pinMode(limit3, INPUT);
  pinMode(limit4, INPUT);
  pinMode(limit5, INPUT);
  pinMode(limit6, INPUT);
  pinMode(limit7, INPUT);
  pinMode(limit8, INPUT);

  stepper1.setMaxSpeed(8000);
  stepper1.setAcceleration(8000);
  stepper2.setMaxSpeed(4000);
  stepper2.setAcceleration(4000);
  stepper3.setMaxSpeed(4000);
  stepper3.setAcceleration(4000);
  stepper4.setMaxSpeed(4000);
  stepper4.setAcceleration(4000);

  //Button 1
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 85, 5, 5, 25);
  my_lcd.Set_Draw_color(RED);
  my_lcd.Fill_Round_Rectangle(160, 80, 10, 10, 25);
  my_lcd.Set_Text_Size(2);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(RED);
  my_lcd.Print("Cheese &", 40, 20);
  my_lcd.Print("Sausage", 40, 50);

  //Button 2
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 85, 5, 165, 25);
  my_lcd.Set_Draw_color(ORANGE);
  my_lcd.Fill_Round_Rectangle(160, 90, 10, 160, 25);
  my_lcd.Set_Text_Size(2);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(ORANGE);
  my_lcd.Print("Cheese Only", 15, 120);

  //Button 3
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 168, 5, 248, 25);
  my_lcd.Set_Draw_color(BLUE);
  my_lcd.Fill_Round_Rectangle(160, 173, 10, 243, 25);
  my_lcd.Set_Text_Size(2);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(BLUE);
  my_lcd.Print("Sausage Only", 15, 200);

  //Button 4
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 85, 315, 5, 25);
  my_lcd.Set_Draw_color(YELLOW);
  my_lcd.Fill_Round_Rectangle(170, 80, 310, 10, 25);
  my_lcd.Set_Text_Size(3);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(YELLOW);
  my_lcd.Print("Thick", 200, 40);

  //Button 5
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 165, 315, 85, 25);
  my_lcd.Set_Draw_color(GREEN);
  my_lcd.Fill_Round_Rectangle(170, 160, 310, 90, 25);
  my_lcd.Set_Text_Size(3);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(GREEN);
  my_lcd.Print("MEDIUM", 190, 110);

  //Button 6
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 168, 315, 248, 25);
  my_lcd.Set_Draw_color(PURPLE);
  my_lcd.Fill_Round_Rectangle(170, 173, 310, 243, 25);
  my_lcd.Set_Text_Size(3);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(PURPLE);
  my_lcd.Print("THIN", 200, 195);

  //Advertisement
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 255, 5, 475, 25);
  my_lcd.Set_Draw_color(PINK);
  my_lcd.Fill_Round_Rectangle(160, 260, 10, 470, 25);
  my_lcd.Set_Text_Size(3);        // Adjust text size as needed
  my_lcd.Set_Text_colour(BLACK);  // Choose text color
  my_lcd.Set_Text_Back_colour(DARKCYAN);
  my_lcd.Print("WWW.", 300, 280);

  //TEMP
  my_lcd.Set_Draw_color(WHITE);
  my_lcd.Fill_Round_Rectangle(165, 255, 315, 475, 25);
  my_lcd.Set_Draw_color(DARKCYAN);
  my_lcd.Fill_Round_Rectangle(170, 260, 310, 470, 25);


  AllHome();
}



void loop() {
  // Main control loop
  switch (currentState) {
    case IDLE:
      // Check if both buttons are selected
      if (selectedButton1 > 0 && selectedButton2 > 0) {
        // Initialize distances for steppers based on button selections
        int moveDistance3 = 0;  // Distance for Stepper 3
        int moveDistance4 = 0;  // Distance for Stepper 4

        // Determine movement distances based on selected buttons
        if ((selectedButton1 == 1) && (selectedButton2 >= 4)) {
          moveDistance4 = distance1;
          moveDistance3 = distance1;

        } else if ((selectedButton1 == 1) && (selectedButton2 >= 5)) {
          moveDistance3 = distance2;
          moveDistance4 = distance2;
        } else if ((selectedButton1 == 1) && (selectedButton2 >= 6)) {
          moveDistance3 = distance3;
          moveDistance4 = distance3;
        } else if ((selectedButton1 == 2) && (selectedButton2 >= 4)) {
          moveDistance3 = distance1;

        } else if ((selectedButton1 == 2) && (selectedButton2 >= 5)) {
          moveDistance3 = distance2;

        } else if ((selectedButton1 == 2) && (selectedButton2 >= 6)) {
          moveDistance3 = distance3;

        } else if ((selectedButton1 == 3) && (selectedButton2 >= 4)) {
          moveDistance4 = distance1;

        } else if ((selectedButton1 == 3) && (selectedButton2 >= 5)) {
          moveDistance4 = distance2;

        } else if ((selectedButton1 == 3) && (selectedButton2 >= 6)) {
          moveDistance4 = distance3;
        }

        // Move the steppers if distances are set
        if (moveDistance3 > 0) stepper3.move(moveDistance3);
        if (moveDistance4 > 0) stepper4.move(moveDistance4);

        // Transition to moving steppers state
        currentState = MOVING_STEPPERS;
      }
      break;

    case MOVING_STEPPERS:
      // Run both steppers simultaneously
      stepper3.run();
      stepper4.run();

      // Check if both steppers have completed their movements
      if (stepper3.distanceToGo() == 0 && stepper4.distanceToGo() == 0) {
        // After movement, transition to slice mode
        currentState = SLICE_MODE;
      }
      break;

    case SLICE_MODE:
      // Call the Slice function to move stepper 1 and stepper 2
      Slice();

      // Check completion condition for the Slice
      if (sliceCompleted) {
        // Once Slice is completed, reset everything to IDLE state
        resetSystem();        // Reset variables for the next cycle
        currentState = IDLE;  // Transition back to idle
      }
      break;
  }
}

void Slice() {
  int speed = 8000;                   // Reduced speed value
  static bool direction1 = false;     // false for moving towards limit5, true for limit6 (Stepper 1)
  static bool direction2 = false;     // false for moving towards limit7, true for limit8 (Stepper 2)
  static bool tripCompleted = false;  // Flag to track if Stepper 2 has completed the trip

  // Check if the trip is completed
  if (!tripCompleted) {
    // Control Stepper 1 (Vertical Stepper)
    if (direction1 == false) {
      // Move Stepper 1 towards limit5 UP
      stepper1.setSpeed(-speed);
      stepper1.runSpeed();
      if (digitalRead(limit6) == LOW) {  // Assuming LOW means limit5 is reached
        direction1 = true;               // Change direction to DOWN
      }
    } else {
      // Move Stepper 1 towards limit6 DOWN
      stepper1.setSpeed(speed);
      stepper1.runSpeed();
      if (digitalRead(limit5) == LOW) {  // Assuming LOW means limit6 is reached
        direction1 = false;              // Change direction to UP
      }
    }

    // Control Stepper 2 (Horizontal Stepper)
    if (direction2 == false) {
      // Move Stepper 2 towards limit7
      stepper2.setSpeed(-speed);
      stepper2.runSpeed();
      if (digitalRead(limit8) == LOW) {  // Assuming LOW means limit7 is reached
        direction2 = true;               // Change direction to limit8
      }
    } else {
      // Move Stepper 2 towards limit8
      stepper2.setSpeed(speed);
      stepper2.runSpeed();
      if (digitalRead(limit7) == LOW) {  // Assuming LOW means limit8 is reached
        tripCompleted = true;            // Mark trip as complete and stop both steppers
      }
    }
  } else {
    // Trip completed, stop both steppers
    stepper1.stop();        // Stop Stepper 1
    stepper2.stop();        // Stop Stepper 2
    sliceCompleted = true;  // Mark slice as completed
  }
}

void resetSystem() {
  // Reset selected button variables and state flags
  selectedButton1 = 0;
  selectedButton2 = 0;
  steppersMoved = false;
  sliceCompleted = false;
}

void AllHome() {
  // Homing routine for all steppers
  // Homing for vertical stepper
  while (digitalRead(limit5)) {
    stepper1.setSpeed(4000);
    stepper1.runSpeed();
  }

  while (!digitalRead(limit5)) {
    stepper1.setSpeed(-4000);
    stepper1.runSpeed();
  }

  while (digitalRead(limit7)) {
    stepper2.setSpeed(4000);
    stepper2.runSpeed();
  }
  stepper2.setSpeed(-4000);
  while (!digitalRead(limit7)) {
    stepper2.runSpeed();
  }
  stepper2.setSpeed(0);  // Stop the stepper

  // Homing for sausage paddle stepper
  while (digitalRead(limit4)) {
    stepper3.setSpeed(-4000);
    stepper3.runSpeed();
  }
  stepper3.setSpeed(4000);
  while (!digitalRead(limit4)) {
    stepper3.runSpeed();
  }
  stepper3.setSpeed(0);  // Stop the stepper

  // Homing for cheese paddle stepper
  while (digitalRead(limit3)) {
    stepper4.setSpeed(-4000);
    stepper4.runSpeed();
  }
  stepper4.setSpeed(4000);
  while (!digitalRead(limit3)) {
    stepper4.runSpeed();
  }
  stepper4.setSpeed(0);  // Stop the stepper
}

Your motor drivers have a "step" pin. When you want "simultaneous" movement of multiple motors, you will need to give each motor-to-be-moved a step command. If one motor needs to move a lot, and one motor needs to move a little, you will need to give each motor proportional stepping. For example, if MOTOR1 needs to move twice as far as MOTOR2, MOTOR1 gets STEPS*0.5 or MOTOR2 gets STEPS * 2. To do this, you will want to study "arduino blink without delay" that can be found in a search.

1 Like

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