Two Motors Different Speeds

Hello Everyone, I know this has been solved in the past but I am just not understanding the issue. Can someone help me? I am just trying to get two steppers to run at one time. I know the step and direction commands are blocking and I see the millis command being used but again, can someone help please? Im using a touch screen to start them instead of a switch.

void loop() {

TSPoint p = ts.getPoint();
boolean led_state = false;
if (p.z > 10 && p.z < 1000) { // Pressure setting

p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
if (p.x > 0 && p.x < 240) {
  if (p.y > 0 && p.y < 64) {  //Cheese and Sausage
    digitalWrite(dirPin, LOW);
    for (int x = 0; x < (stepsPerRevolution); x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1200);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1200);
      digitalWrite(stepPin2, HIGH);
      delayMicroseconds(1200);
      digitalWrite(stepPin2, LOW);
      delayMicroseconds(1200);
      Distance = Distance + 1;
    }

    digitalWrite(dirPin, HIGH);

    for (int x = 0; x < (stepsPerRevolution); x++) {
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1200);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1200);
      digitalWrite(stepPin2, HIGH);
      delayMicroseconds(1200);
      digitalWrite(stepPin2, LOW);
      delayMicroseconds(1200);

      Distance = 0;
    }
  }

Not going to happen when you step each motor individually in your code.

Use a library like AccelStepper to manage the motors.

Hello Arduino Community, well, I didn't get much help with my two steppers at one time inquiry. However, I searched the forums and I was able to figure out what I needed for my project. Thank you for the tip about using a different library, it sent me in the right direction. Here is the working code that I came up with. I am sure most would be able to accomplish this much more eloquently than I. Thanks again!

#include <MultiStepper.h>
#include <MCUFRIEND_kbv.h>
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h> // Touch Support
#include <AccelStepper.h>
#include <Adafruit_GFX.h>
#include <MobaTools.h>

const byte motorInterfaceType = 1;
const byte dirPin = 38; //Head
const byte stepPin = 36;

const byte dirPin1 = 42; //Thickness
const byte stepPin1 = 40;

const byte dirPin2 = 35; //Blade
const byte stepPin2 = 37;
const byte LimitPin = 22;
uint16_t identifier; //Store Screen Identifier
int steps = 0;
int pos = 0;
int stepsPerRevolution = 11000; //HEAD

int stepsPerRevolution2 = 1800; //THICK
int stepsPerRevolution3 = 2000; //MEDIUM
int stepsPerRevolution4 = 2200; //THIN
int Distance = 0;
int count = 0;

MoToStepper plateStepper(200, STEPDIR);
MoToStepper cutterStepper(400, STEPDIR); // Don't know steps/rev for this stepper
MoToStepper thicknessStepper(400, STEPDIR);

AccelStepper stepper1 = AccelStepper(motorInterfaceType, stepPin1, dirPin1); //Attaches A4988 driver to Accel Library

#define TS_MINX 920
#define TS_MINY 120
#define TS_MAXX 150
#define TS_MAXY 940
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Elegoo_GFX_Button buttons; //Button object declaration

#define BLUEBAR_MINX 180
#define BAR_MINY 30
#define BAR_HEIGHT 250
#define BAR_WIDTH 30
#define BLACK 0x0000 // macros for color (16 bit)
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

void setup() {
tft.begin(0x9341); // Initialise LCD
uint16_t ID = tft.readID();
tft.setRotation(2);
tft.begin(tft.readID());
tft.setRotation(2);
tft.fillScreen(BLACK);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(10, 10);
tft.println("Thermo-Slice");
tft.setCursor(50, 50);
tft.println("Deluxe");
tft.setCursor(40, 100);
tft.setTextSize(2);
tft.println("Invented By:");
tft.setCursor(30, 120);
tft.println("Richard Potter");
tft.setTextSize(1);
tft.setCursor(40, 200);
tft.println("Provisional Patented 2023");
tft.drawRect(0, 260, 244, 68, YELLOW);
tft.fillRect(0, 256, 240, 64, BLUE);
tft.setCursor(10, 270);
tft.setTextSize(3);
tft.println("Loading.....");
delay(7000); //End of Boot up screen.
for (int i; i < 250; i++) {
tft.fillRect(BAR_MINY - 0, BLUEBAR_MINX, i, 10, BLUE);
delay(0.000000000000000000000000000000000000000000000000001);
}
tft.setRotation(2);
tft.fillRect(0, 0, 240, 64, WHITE); // First WHITE Rectange
tft.fillRect(0, 64, 240, 64, RED); // Second YELLOW Rectangle
tft.fillRect(0, 128, 240, 64, YELLOW); // Third RED Rectangle
tft.fillRect(0, 192, 240, 64, BLUE); // Fourth MAGENTA Rectangle
tft.fillRect(0, 256, 240, 64, LIGHTGREY); // Fith ORANGE Rectangle
tft.drawRect(0, 0, 240, 64, BLACK); // Black borders to the rectangles
tft.drawRect(0, 64, 240, 64, BLACK); // Black borders to the rectangles
tft.drawRect(0, 128, 240, 64, BLACK); // Black borders to the rectangles
tft.drawRect(0, 192, 240, 64, BLACK); // Black borders to the rectangles
tft.drawRect(0, 256, 240, 64, BLACK); // Black borders to the rectangles
tft.setTextColor(BLACK); // Set Text Proporties
tft.setTextSize(3);
tft.setCursor(65, 90);
tft.println("MEDIUM"); // Write Text on LCD
tft.setTextSize(3);
tft.setCursor(65, 150);
tft.println("THICK ");
tft.setCursor(50, 215);
tft.println(" SAUSAGE ");
tft.setTextSize(2);
tft.setCursor(30, 280);
tft.println("Cheese & Sausage");

tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(70, 20);
tft.println("THIN ");

tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(45, 35);

while (digitalRead(LimitPin)) { // Do this until the switch is activated
digitalWrite(dirPin1, LOW);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin1, LOW);
delayMicroseconds(500);
}

while (!digitalRead(LimitPin)) { // Do this until the switch is not activated
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, LOW);
delayMicroseconds(500);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(500);
}

plateStepper.attach(stepPin, dirPin); //HEAD
cutterStepper.attach(stepPin2, dirPin2); //Blade
thicknessStepper.attach(stepPin1, dirPin1); //Thickness
pinMode(LimitPin, INPUT);

plateStepper.setSpeedSteps(30000); // = 12500 steps/sec
plateStepper.setRampLen(50); // acceleration: steps until full speed is reached

cutterStepper.setSpeedSteps(20000); // adjust for your needs
cutterStepper.setRampLen(50); // acceleration: steps until full speed is reached

thicknessStepper.setSpeedSteps(15000);
thicknessStepper.setRampLen(50);
Serial.begin(9600);
Serial.begin(9600);
digitalRead(LimitPin);
}

void loop() {

TSPoint p = ts.getPoint();
boolean led_state = true;
if (p.z > 10 && p.z < 1000) { // Pressure setting

p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
if (p.x > 0 && p.x < 240) {
  if (p.y > 0 && p.y < 64) {  //Cheese and Sausage

    plateStepper.writeSteps(-stepsPerRevolution);  // Do one turn
    cutterStepper.rotate(1);                       // rotate until stopped
    while (plateStepper.moving())
      ;  // wait until one turn is done

    cutterStepper.rotate(0);  // stop cutterStepper
    delay(500);

    if (plateStepper.readSteps() == -stepsPerRevolution)
      ;                          //{
    plateStepper.writeSteps(0);  // Back one turn to starting position
    cutterStepper.rotate(1);     // rotate until stopped
    while (plateStepper.moving())
      ;  // wait until one turn is done

    cutterStepper.rotate(0);  // stop cutterStepper
  }



  if (p.y > 64 && p.y < 128) {  //Sausage
    TSPoint p = ts.getPoint();

    plateStepper.writeSteps(-stepsPerRevolution + 4500);  // Do one turn
    cutterStepper.rotate(1);                              // rotate until stopped
    while (plateStepper.moving())
      ;  // wait until one turn is done

    cutterStepper.rotate(0);  // stop cutterStepper
    delay(500);

    if (plateStepper.readSteps() == -stepsPerRevolution + 175)
      ;
    plateStepper.writeSteps(0);  // Back one turn to starting position
    cutterStepper.rotate(1);     // rotate until stopped
    while (plateStepper.moving())
      ;  // wait until one turn is done

    cutterStepper.rotate(0);  // stop cutterStepper
  }
  if (p.y > 128 && p.y < 194) {
    TSPoint p = ts.getPoint();
    while (digitalRead(LimitPin)) {  // Do this until the switch is activated
      digitalWrite(dirPin1, LOW);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
    }

    while (!digitalRead(LimitPin)) {  // Do this until the switch is not activated
      digitalWrite(dirPin1, HIGH);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
    }

    for (int x = 0; x < stepsPerRevolution2; x++) {
      digitalWrite(dirPin1, HIGH);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
    }
  }
  if (p.y > 195 && p.y < 250) {
    TSPoint p = ts.getPoint();
    while (digitalRead(LimitPin)) {  // Do this until the switch is activated
      digitalWrite(dirPin1, LOW);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
    }

    while (!digitalRead(LimitPin)) {  // Do this until the switch is not activated
      digitalWrite(dirPin1, HIGH);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
    }

    for (int x = 0; x < stepsPerRevolution3; x++) {
      digitalWrite(dirPin1, HIGH);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
    }
  }

  if (p.y > 251 && p.y < 320) {
    TSPoint p = ts.getPoint();
    while (digitalRead(LimitPin)) {  // Do this until the switch is activated
      digitalWrite(dirPin1, LOW);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
    }

    while (!digitalRead(LimitPin)) {  // Do this until the switch is not activated
      digitalWrite(dirPin1, HIGH);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
    }

    for (int x = 0; x < stepsPerRevolution4; x++) {
      digitalWrite(dirPin1, HIGH);
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(500);
    }
  }
}

}
}

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