I made this sketch to move a stepper a certain distance with a button press then move a bit further with another press of the button and then back to the start after the second distance is complete.
I'm using a mega 2560 and the sketch works well, see below.
I now want to do the same with two motors in unison son I changed how the motors are defined so I could have stepper1 and stepper2 but I am getting the error below when trying to compile.
Error compiling for board Arduino/Genuino Mega or Mega 2560.
I am an engineer but quite new to Arduino sketching any help would be appreciated.
first sketch.... this works
// Include the AccelStepper library:
#include <AccelStepper.h>
const int buttonPin = 4;
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// button to start each position:
pinMode(buttonPin, INPUT);
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(1000);
}
void loop() {
int buttonState (LOW);
while (digitalRead(buttonPin)== LOW);
{
// Set the current position to 0:
stepper.setCurrentPosition(0);
// Run the motor forward at 600 steps/second until the motor reaches 400 steps (2 revolutions):
while(stepper.currentPosition() != 400)
{
stepper.setSpeed(600);
stepper.runSpeed();
}
delay(1000);
int buttonState (LOW);
while (digitalRead(buttonPin)== LOW);
{
// Reset the position to 0:
stepper.setCurrentPosition(0);
// Run the motor forward at 600 steps/second until the motor reaches 400 steps (1 revolution):
while(stepper.currentPosition() != 400)
{
stepper.setSpeed(600);
stepper.runSpeed();
}
delay(1000);
// Reset the position to 0:
stepper.setCurrentPosition(0);
// Run the motor backward at 400 steps/second until the motor reaches 600 steps (3 revolutions):
while(stepper.currentPosition() != -800)
{
stepper.setSpeed(-400);
stepper.runSpeed();
}
delay(3000);
}
}
}
second sketch...… this doesn't work
#include <AccelStepper.h>
/*Example sketch to control a stepper motor with A4988 stepper motor driver, AccelStepper library and Arduino: number of steps or revolutions. More info: https://www.makerguides.com */
// Include the AccelStepper library:
const int buttonPin = 4;
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
// Create a new instance of the AccelStepper class:
AccelStepper stepper1(AccelStepper::DRIVER, 3, 2);
void setup() {
pinMode(buttonPin, INPUT);
// Set the maximum speed in steps per second:
stepper1.setMaxSpeed(1000);
}
void loop() {
int buttonState (LOW);
while (digitalRead(buttonPin)== LOW);
{
// Set the current position to 0:
stepper1.setCurrentPosition(0);
// Run the motor forward at 200 steps/second until the motor reaches 400 steps (2 revolutions):
while(stepper1.currentPosition() != 400)
{
stepper1.setSpeed(600);
stepper1.runSpeed();
}
delay(1000);
int buttonState (LOW);
while (digitalRead(buttonPin)== LOW);
{
// Reset the position to 0:
stepper1.setCurrentPosition(0);
// Run the motor forward at 600 steps/second until the motor reaches 400 steps (1 revolution):
while(stepper1.currentPosition() != 400)
{
stepper1.setSpeed(600);
stepper1.runSpeed();
}
delay(1000);
// Reset the position to 0:
stepper1.setCurrentPosition(0);
// Run the motor forward at 400 steps/second until the motor reaches 600 steps (3 revolutions):
while(stepper1.currentPosition() != -800)
{
stepper1.setSpeed(-400);
stepper1.runSpeed();
}
delay(3000);
}
}
}