Hi, Here's an example (for different motors) that controls two motors. THis is for a demo we run that has two modes: Automatic runs both motors opposite directions, accelerates etc. Manual has 2 pots. The motors move (slowly) to the pot position. YMMV. Hopefully some ideas..
/* YourDuinoStarter Example: 2 Stepper Motors
- WHAT IT DOES: Runs 2 28BYJ-48 stepper motors with AccelStepper Library
- Motors accelerate and decelerate simultaneously in opposite rotations
- SEE the comments after "//" on each line below
- Derived from example code by Mike McCauley
- modified by Celem for single stepper
- modified by lowres for two steppers
- V1.01 11/30/2013
Questions: terry@yourduino.com */
/*-----( Import needed libraries )-----*/
// AccelStepper library from: http://www.airspayce.com/mikem/arduino/AccelStepper/
#include <AccelStepper.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define FULLSTEP 4
#define HALFSTEP 8
// motor pins
#define motorPin1 2 // Blue - 28BYJ48 pin 1
#define motorPin2 3 // Pink - 28BYJ48 pin 2
#define motorPin3 4 // Yellow - 28BYJ48 pin 3
#define motorPin4 5 // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
#define motorPin5 6 // Blue - 28BYJ48 pin 1
#define motorPin6 7 // Pink - 28BYJ48 pin 2
#define motorPin7 8 // Yellow - 28BYJ48 pin 3
#define motorPin8 9 // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
#define PotLeftMotorPin A0 // For manual control
#define PotRightMotorPin A1
#define ModeSwitchPin 11
#define AutoMode 1 // Define Auto or manual mode
#define ManualMode 0
/*-----( Declare objects )-----*/
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
/*-----( Declare Variables )-----*/
int RunMode; //Set Auto or Manual
int StartAutoModeOnce; // Do setup for Auto Mode
int StartManualModeOnce; // Do setup for Auto Mode
int LeftPotValue; // Read from the manual potentiometerrs
int RightPotValue;
long int LeftMotorTarget ;
long int RightMotorTarget ;
int ModeSwitchValue;
void setup() /****** SETUP: RUNS ONCE ******/
{
pinMode(ModeSwitchPin,INPUT_PULLUP);
RunMode = AutoMode;
// RunMode = ManualMode;
StartAutoModeOnce = 1;
StartManualModeOnce = 1;
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
ModeSwitchValue = digitalRead(ModeSwitchPin);
if (ModeSwitchValue == ManualMode)
RunMode = ManualMode;
else
RunMode = AutoMode;
if (RunMode == AutoMode)
{
if (StartAutoModeOnce == 1)
{
InitAuto();
StartAutoModeOnce = 0;
}
//Change direction at the limits
if (stepper1.distanceToGo() == 0)
{
stepper1.moveTo(-stepper1.currentPosition());
}
if (stepper2.distanceToGo() == 0)
{
stepper2.moveTo(-stepper2.currentPosition());
}
}// end Automode reversing
//------------------( MANUAL MODE )---------------------------------------
if (RunMode == ManualMode)
{
LeftPotValue = 4 * analogRead(PotLeftMotorPin); //Read the knob positions
LeftMotorTarget = LeftPotValue;
RightPotValue = 4 * analogRead(PotRightMotorPin);
RightMotorTarget = RightPotValue;
if (StartManualModeOnce == 1)
{
InitManual();
StartManualModeOnce = 0;
}
//Change direction at the limits
if (stepper1.distanceToGo() == 0)
{
// stepper1.moveTo(-stepper1.currentPosition());
stepper1.moveTo(RightMotorTarget);
}
if (stepper2.distanceToGo() == 0)
{
stepper2.moveTo(LeftMotorTarget);
}
}// END Manual Mode
//----( ALWAYS run in Loop )-------
stepper1.run();
stepper2.run();
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
void InitAuto() /****** Automatic contra-rotation 1 rev ******/
{
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(50.0);
stepper1.setSpeed(200);
stepper1.moveTo(2048); // 1 revolution
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(50.0);
stepper2.setSpeed(200);
stepper2.moveTo(-2048); // 1 revolution
StartManualModeOnce = 1;
}//--(end InitAuto )---
void InitManual() /****** Control By Knobs ******/
{
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(50.0);
stepper1.setSpeed(200);
stepper1.moveTo(20); // 1 revolution
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(50.0);
stepper2.setSpeed(200);
stepper2.moveTo(-20); // 1 revolution
StartAutoModeOnce = 1;
}//--(end InitAuto )---
//*********( THE END )***********