Combine 2 working sketches into 1 PROBLEM SOLVED

Hello Everyone
I need some guidance. I have these 2 sketches that work perfectly on their own .

// Include the AccelStepper Library
#include <AccelStepper.h>

// Define step constant
//#define FULLSTEP 4
#define HALFSTEP 8

// Creates an instance
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper stepper1(HALFSTEP, 8, 10, 9, 11);

#define buttonPin 6

//Define variables
//variable that stores the state of the button:
int buttonReading;
//State of the blinds, low is closed, high is open:
int state = LOW;
int previous = LOW;

void setup() {
  Serial .begin(9600);
  pinMode (buttonPin, INPUT_PULLUP);

  // set the maximum speed, acceleration factor,
  // initial speed and the target position
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(50.0);
  stepper1.setSpeed(200);
  stepper1.setCurrentPosition(0);
  //myStepper.moveTo(2038);
}

void loop()
{
  buttonReading = digitalRead(buttonPin);

  if (buttonReading == LOW && previous == HIGH) {
    if (state == LOW) {
      state = HIGH;
    }
    else {
      state = LOW;
    }
  }

  previous = buttonReading;

  if (state == HIGH) {
    while (stepper1.currentPosition() != 2000)
    {
      stepper1.setSpeed(200);
      stepper1.runSpeed();
    }
    Serial.println("unloading car");
  }
  if (state == LOW) {
    while (stepper1.currentPosition() != 0)
    {
      stepper1.setSpeed(-200);
      stepper1.runSpeed();
    }
    Serial.println("ready for loaded car");
  }
} 
/*Example sketch to control a stepper motor with A4988 stepper motor driver and Arduino without a library. More info: https://www.makerguides.com */

#include <AccelStepper.h>  // Include the AccelStepper Library 

// Define stepper motor connections and steps per revolution:
#define dirPin 4
#define stepPin 5

#define buttonPin 6

//Define variables
//variable that stores the state of the button:
int buttonReading;
//State of the blinds, low is closed, high is open:
int state = LOW;
int previous = LOW;
AccelStepper stepper0(1, stepPin, dirPin);

void setup() {
  Serial .begin(9600);
  pinMode (buttonPin, INPUT_PULLUP);
  stepper0.setMaxSpeed(200);
  
  stepper0.setSpeed(200);
  stepper0.setCurrentPosition(0);

}



void loop()
{
  buttonReading = digitalRead(buttonPin);

  if (buttonReading == LOW && previous == HIGH) {
    if (state == LOW) {
      state = HIGH;
    }
    else {
      state = LOW;
    }
  }

  previous = buttonReading;

  if (state == HIGH) {
    while (stepper0.currentPosition() != 6000)
    {
      stepper0.setSpeed(200);
      stepper0.runSpeed();
    }
    Serial.println("unloading car");
  }
  if (state == LOW) {
    while (stepper0.currentPosition() != 0)
    {
      stepper0.setSpeed(-200);
      stepper0.runSpeed();
    }
    Serial.println("Ready for loaded car");
  }
}

I need to combine the 2 sketches so that with a button push it runs stepper 1 to the set position and stops, then when stepper 1 reaches its position stepper 2 runs to its position and stops. with a second button push stepper 2 goes back to its start position, after it reaches that position stepper 1 then returns to its start position and stops waiting for the next button push to start the cycle over again. This program will control an HO scale rotary car dumper that has been 3d printed . I have done numerous searches and get no examples I have found for something similar working. my attempts have either not compiled giving errors orcompiled but have done nothing or only 1 of the steppers will do what they are supposed to. I dont want anyone to write the code, I would like someone point me to a proper way to accomplish it.
Thanks for any help James

Please post your best effort at combining the sketches

this sketch will compile and upload when the button is pushed stepper1 starts and stepper0 starts while stepper 0 is making its way to its position stepper1 makes 2 cycles then stops when stepper0 makes it to its position. then on the second button push it reverses and goes back to the starting positions for both steppers. so it is working but I need stepper1 to go to its position and stop then stepper0 do its move and stop. then on the second button push stepper 0 needs to return to its start position then stop then stepper1 needs to retun to its start position and stop.

#include <AccelStepper.h>

/*Example sketch to control a stepper motor with A4988 stepper motor driver and Arduino without a library. More info: https://www.makerguides.com */

// Define stepper motor connections :
#define dirPin 4
#define stepPin 5

// Define step constant
//#define FULLSTEP 4
#define HALFSTEP 8

// Creates an instance for stepper 1
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper stepper1(HALFSTEP, 8, 10, 9, 11);

#define buttonPin 6

//Define variables
//variable that stores the state of the button:
int buttonReading;
//State of the blinds, low is closed, high is open:
int state = LOW;
int previous = LOW;

// creates an instance for stepper 0
AccelStepper stepper0(1, stepPin, dirPin);

void setup() {
  Serial .begin(9600);
  pinMode (buttonPin, INPUT_PULLUP);

  // set the maximum speed, acceleration factor, initial speed and the start position for stepper 1
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(50.0);
  stepper1.setSpeed(200);
  stepper1.setCurrentPosition(0);

  // set the maximum speed, acceleration factor, initial speed and the start position for stepper 0
  stepper0.setMaxSpeed(200);
  stepper0.setAcceleration(50.0);
  stepper1.setSpeed(200);
  stepper0.setCurrentPosition(0);
}

void loop()
{
  buttonReading = digitalRead(buttonPin);

  if (buttonReading == LOW && previous == HIGH) {
    if (state == LOW) {
      state = HIGH;
    }
    else {
      state = LOW;
    }
  }

  previous = buttonReading;

  if (state == HIGH) {
    while (stepper0.currentPosition() != 6000)
      while (stepper1.currentPosition() != 6000)
      {
        stepper0.setSpeed(200);
        stepper0.runSpeed();
        stepper1.setSpeed(200);
        stepper1.runSpeed();
      }
    Serial.println("unloading car");
  }
  if (state == LOW) {
    while (stepper0.currentPosition() != 0)
      while (stepper1.currentPosition() != 0)
      {
        stepper0.setSpeed(-200);
        stepper0.runSpeed();
        stepper1.setSpeed(-200);
        stepper1.runSpeed();
      }
    Serial.println("Ready for loaded car");
  }
}

The requirement (note my added emphasis)

But you have got the while loop controlling stepper 1 inside the while loop conrolling stepper 0 so the 2 stepper movements will not happen sequentionally

ok is there a tutorial that would guide me to the correct solution? Or can you suggest where I should be looking for the solution? Thanks for the replys

There will not be a tutorial on a solution to your specific problem but as long as you can control the movement of the steppers you have all you need already

What you need to do is to arrange the code so that it does what you describe, ie when a button press is detected first move stepper 0 to its new position then move stepper 1 to its new position

Do you understand the problem with your current code ?

yes i think so

Start by only moving stepper 0 when the button becomes pressed. stepper 1 can be added after you have got stepper 0 working correctly

Hello UKHeliBob
After a good nights sleep and contemplating your suggestions I have it working correctly such a simple solution has taken me over a week to get right.
Thanks so much for your input James

I am glad that you got it working

Please post your working sketch so that others with a similar problem can benefit

Here is the modified code to make 2 steppers run in a sequence then reverse the motions with the pushes of a button . I hope this helps someone that might be trying to do the same thing. And thanks to UKHeliBob for guiding in the right direction and making me to use my brain!

*Example sketch to control a stepper motor with A4988 stepper motor driver and Arduino with the Accelstepper library the original sketch did not use a library. More info: https://www.makerguides.com */

/* I modified the sketch from makerguides to run a nema 17 steppermotor with an A4988 driver and a 28BYJ-48 steppermotor with a ULN2003a driver board to run in a sequence with a button push then return to its start position with the second button push*/

#include <AccelStepper.h>

// Define stepper motor connections :
#define dirPin 4
#define stepPin 5

// Define step constant  use a value of 4 for full step use a value of 8 for halfsteps for the unl2003a driver board
//#define FULLSTEP 4
#define HALFSTEP 8

// Creates an instance for stepper 1
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper stepper1(HALFSTEP, 8, 10, 9, 11);

// creates an instance for stepper 0
AccelStepper stepper0(1, stepPin, dirPin);

#define buttonPin 6  // defines the push button pin;

//Define variables
//variable that stores the state of the button:
int buttonReading;
//State of the blinds, low is closed, high is open:
int state = LOW;
int previous = LOW;


void setup() {
  Serial .begin(9600);
  pinMode (buttonPin, INPUT_PULLUP);

  // set the maximum speed, acceleration factor, initial speed and the start position for stepper 1
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(50.0);
  stepper1.setSpeed(200);
  stepper1.setCurrentPosition(0);

  // set the maximum speed, acceleration factor, initial speed and the start position for stepper 0
  stepper0.setMaxSpeed(200);
  stepper0.setAcceleration(50.0);
  stepper1.setSpeed(200);
  stepper0.setCurrentPosition(0);
}

void loop()
{
  buttonReading = digitalRead(buttonPin);

  if (buttonReading == LOW && previous == HIGH) {
    if (state == LOW) {
      state = HIGH;
    }
    else {
      state = LOW;
    }
  }

  previous = buttonReading;

  if (state == HIGH) {
    while (stepper1.currentPosition() != 2000) // adjust this number for the go to position;
    {
      stepper1.setSpeed(200);  
      stepper1.runSpeed();
    }
   // Serial.println("clamping car");
  }
  if (state == HIGH) {
    while (stepper0.currentPosition() != 6000) // adjust this number for the go to position;
    {
      stepper0.setSpeed(200);
      stepper0.runSpeed();
    }
    //Serial.println("rotating car");
  }
  if (state == LOW) {
    while (stepper0.currentPosition() != 0)
    {
      stepper0.setSpeed(-200);
      stepper0.runSpeed();
    }
    //Serial.println("rotating car back");
  }
  if (state == LOW) {
    while (stepper1.currentPosition() != 0)
    {
      stepper1.setSpeed(-200);
      stepper1.runSpeed();
    }
    //Serial.println("Ready for loaded car");
  }
}

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