Issues running 2 28BYJ-48 stepper motors
Hi all,
I'm working on a project where I have two 28BYJ-48 stepper motors with ULN2003 drivers running 'winches' from a Mega 2560.
So far I've got both stepper motors running smoothly when independent and from their own power supply (one is from the board and another from another power supply). I'm tracking the motors movement with rotary encoders at the other end of the winch drum and using the Accelstepper library to run the motors to and from preset encoder positions.
When I try to combine the program to run both simultaneously they get very jittery, stopping and starting at random intervals. From some reading I gather this could be a power supply issue, however their power supplies are separate and work fine independently. I'm wondering if there are conflicts in my code that are causing this problem.
When run, the program reads the saved EEPROM value for the position of the encoder (enc). looks at the first value in the cue array and moves towards this. When reached, it moves towards the next array value until it reaches it, then stops. I've basically duplicated this for each motor, is this too much for the arduino to handle at once, causing delays in the running of the program?
Code below and any advice would be much appreciated. Thanks.
'
#include <AccelStepper.h>
#include <EEPROM.h>
#define HALFSTEP 8
#define enc1A 26 // Encoder Outputs
#define enc1B 27
#define enc2A 48
#define enc2B 49
#define W1Pin1 25 // Winch 1 Pins
#define W1Pin2 24
#define W1Pin3 23
#define W1Pin4 22
#define W2Pin1 53 // Winch 2 Pins
#define W2Pin2 52
#define W2Pin3 51
#define W2Pin4 50
int W1Speed = 900; // Winch set Speeds
int W2Speed = -900;
int speed1;
int speed2;
bool W1endMov = 0; // Bool to singulate end direction switch
bool W2endMov = 0;
int enc1Pos; // Encoder positioning decs
int enc1MemAddress = 1;
int enc2Pos;
int enc2MemAddress = 3;
unsigned long encMemStart; // Mem Display Clock
unsigned long encMemCurrent;
const unsigned long encMemDispPeriod = 3000;
int cueList[] = {30, 70}; // Cue array
int W1cueNum = 0;
int W2cueNum = 0;
int enc1State; // Encoder reference readings
int enc1LState;
int enc2State;
int enc2LState;
AccelStepper winch1(HALFSTEP, W1Pin1, W1Pin3, W1Pin2, W1Pin4); // Winch pin set up
AccelStepper winch2(HALFSTEP, W2Pin1, W2Pin3, W2Pin2, W2Pin4);
void setup()
{
Serial.begin(9600); // Begin Serial
delay(5000);
Serial.println();
Serial.println("SERIAL BEGAN");
Serial.println();
delay(2000);
winch1.setMaxSpeed(950); // Set winch max speeds
winch2.setMaxSpeed(950);
pinMode(enc1A, INPUT); // Encoder pin modes
pinMode(enc1B, INPUT);
pinMode(enc2A, INPUT);
pinMode(enc2B, INPUT);
enc1Pos = EEPROM.read(enc1MemAddress); // Read Last Saved Encoder Position from EEPROM
enc2Pos = EEPROM.read(enc2MemAddress);
Serial.println();
Serial.println("Encoder Values Obtained"); delay(100);
Serial.print("Enc1Pos: "); Serial.print(enc1Pos); Serial.print(" Enc2Pos: "); Serial.println(enc2Pos);
Serial.println();
delay(2000);
encMemStart = millis(); // Zero clock and start prompt
Serial.println("START");
delay(500);
enc1LState = digitalRead(enc1A); // Read current encoder pulse states
enc2LState = digitalRead(enc2A);
}
void loop()
{
speed1 = W1Speed;
speed2 = W2Speed;
winch1.setSpeed(speed1);
winch2.setSpeed(speed2);
if (W1cueNum == 0) // WINCH 1 CUE 1
{
if (enc1Pos >= cueList[0])
{
speed1 = -W1Speed;
winch1.setSpeed(speed1);
winch1.runSpeed();
}
if (enc1Pos <= cueList[0])
{
speed1 = W1Speed;
winch1.setSpeed(speed1);
winch1.runSpeed();
}
}
if (enc1Pos == cueList[0] && W1endMov == 0)
{
W1endMov = 1;
winch1.stop();
Serial.println();
Serial.println("WINCH ONE CUE ONE REACHED");
Serial.println();
W1cueNum ++;
}
if (W2cueNum == 0) // WINCH 2 CUE 1
{
if (enc2Pos >= cueList[0])
{
speed2 = -W2Speed;
winch2.setSpeed(speed2);
winch2.runSpeed();
}
if (enc2Pos <= cueList[0])
{
speed2 = W2Speed;
winch2.setSpeed(speed2);
winch2.runSpeed();
}
}
if (enc2Pos == cueList[0] && W2endMov == 0)
{
W2endMov = 2;
winch2.stop();
Serial.println();
Serial.println("WINCH TWO CUE ONE REACHED");
Serial.println();
W2cueNum ++;
}
if (W1cueNum == 1) // WINCH 1 CUE 2
{
if (enc1Pos >= cueList[1])
{
speed1 = -W1Speed;
winch1.setSpeed(speed1);
winch1.runSpeed();
} else {
speed1 = W1Speed;
winch1.setSpeed(speed1);
winch1.runSpeed();
}
}
if (enc1Pos == cueList[1] && W1endMov == 1)
{
W1endMov = 0;
winch1.stop();
Serial.println();
Serial.println(" WINCH ONE CUE TWO REACHED, WINCH ONE CUES COMPLETED");
Serial.println();
W1cueNum ++;
}
if (W2cueNum == 1) // WINCH 2 CUE 2
{
if (enc2Pos >= cueList[1])
{
speed2 = -W2Speed;
winch2.setSpeed(speed2);
winch2.runSpeed();
} else {
speed2 = W2Speed;
winch2.setSpeed(speed2);
winch2.runSpeed();
}
}
if (enc2Pos == cueList[1] && W2endMov == 1)
{
W2endMov = 0;
winch2.stop();
Serial.println();
Serial.println(" WINCH TWO CUE TWO REACHED, WINCH TWO CUES COMPLETED");
Serial.println();
W2cueNum ++;
}
enc1State = digitalRead(enc1A); // Encoder 1 Trips
if(enc1State != enc1LState)
{
if(digitalRead(enc1B) != enc1State)
{
enc1Pos --;
} else {
enc1Pos ++;
}
Serial.print("W1Cue#: ");
Serial.print(W1cueNum);
Serial.print(" -- Encoder 1 Position: ");
Serial.print(enc1Pos);
Serial.print(" -- Speed: ");
Serial.print(winch1.speed());
Serial.print(" -- Motor Step Position: ");
Serial.println(winch1.currentPosition());
EEPROM.write(enc1MemAddress, enc1Pos);
}
enc1LState = enc1State;
enc2State = digitalRead(enc2A); // Encoder 2 Trips
if(enc2State != enc2LState)
{
if(digitalRead(enc2B) != enc2State)
{
enc2Pos ++;
} else {
enc2Pos --;
}
Serial.print("W2Cue#: ");
Serial.print(W2cueNum);
Serial.print(" -- Encoder 2 Position: ");
Serial.print(enc2Pos);
Serial.print(" -- Speed: ");
Serial.print(winch2.speed());
Serial.print(" -- Motor Step Position: ");
Serial.println(winch2.currentPosition());
EEPROM.write(enc2MemAddress, enc2Pos);
}
enc2LState = enc2State;
encMemCurrent = millis(); // Display Memory Value For Enc 1 and 2 Every 3 Seconds
if (encMemCurrent - encMemStart >= encMemDispPeriod)
{
Serial.print("Mem Pos Enc1: "); Serial.print(EEPROM.read(enc1MemAddress));
Serial.print(" || Mem Pos Enc2: "); Serial.println(EEPROM.read(enc2MemAddress));
encMemStart = encMemCurrent;
}
}'