I am running two stepper motors and want stepper1 to move to a certain position and then stepper2 to move to a certain position with the press of a button. This is the code I have used so far:
#include <AccelStepper.h>
// Define some steppers and the pins the will use
//AccelStepper stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper1(AccelStepper::FULL2WIRE, 8, 9);
AccelStepper stepper2(AccelStepper::FULL2WIRE, 10, 11);
int ButtonState = 0;
int OnOrOffState = 0;
void setup()
{
stepper1.setMaxSpeed(700.0);
stepper1.setAcceleration(200.0);
stepper1.moveTo(10000);
stepper2.setMaxSpeed(300.0);
stepper2.setAcceleration(200.0);
stepper2.moveTo(4000);
}
void loop() {
ButtonState = digitalRead(2);
if (ButtonState == LOW){
OnOrOffState = 1;}
while(OnOrOffState ==1){
stepper1.run();
if (stepper1.distanceToGo() == 0)
stepper2.run();
if (stepper2.distanceToGo() == 0){
OnOrOffState = 0;
}
}
}
This seems to work but what I want to do next is be able to press the same button again and it run in reverse.
stepper2 to return to it's original position followed by stepper1.
Can you help me with this part of the code please.
I am using an Arduino Uno with two HY-DIV268N-5A drivers and two Nema 23 motors. 24v power supply.
I think your code in loop() needs a complete re-write. The calls to stepper.run() should not be inside any WHILE or IF statement. They should be called on every iteration of loop().
Stepper.run() will do nothing when the motor gets to its destination.
Your buttons should set the values for the destinations, not whether or not stepper.run() gets called.
If you want someone to write the code for you, you can go to Gigs & Collaborations...
If you want to be guided to the right path and learn something, study the advice you are given
and be grateful someone's given their time free to help you progress.
OK, I'm getting there.
Now I just need to insert the code for the button.
I want to press the button and it moves stepper 1 and 2 to there locations and stop. when I press the button again I want them to return.
Here is the code I have so far.
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9);
Stepper myStepper2(stepsPerRevolution, 10,11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(1200);
myStepper2.setSpeed(600);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("counterclockwise");
myStepper.step(-20000);
Serial.println("clockwise");
myStepper2.step(5000);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper2.step(-5000);
Serial.println("clockwise");
myStepper.step(20000);
delay(500);
}