Hey everyone,
I'm trying to power two Nema 17 stepper motors via an Arduino Uno board, one DM332T digital stepping driver, and an L298N motor driver. When testing them, I was able to get them to run individually utilizing two different sets of code. However, when I plug them both into the Uno board and try running a single program, I can't get either to work. Any help or guidance would be greatly appreciated!
Here's the code:
#define PUL1_PIN 2
#define DIR1_PIN 3
#define PUL2_PIN 5
#define DIR2_PIN 6
#define PUL3_PIN 8
#define DIR3_PIN 9
class stepperMotor{
public:
// function to stop motor, value of 0 is the "off" signal
void stop(void){
enable = 0;
}
//function to start motor, value of 1 is the "on" signal
void start(void){
enable = 1;
}
void init(int _pulsePin, int _dirPin, unsigned long _delayTime, bool _direction){
pulsePin = _pulsePin;
dirPin = _dirPin;
delayTime = _delayTime;
direction = _direction;
togglePulse = LOW;
enable = 0;
pinMode(pulsePin, OUTPUT); //sets pulsePin variable as a digital output, motorTwo pin 8
pinMode(dirPin, OUTPUT); //sets dirPin variable as digital output, motorTwo pin 9
}
void init2(int _pulsePin1, int _dirPin1, int _pulsePin2, int _dirPin2, unsigned long _delayTime1, bool _direction1) {
pulsePin1 = _pulsePin1;
dirPin1 = _dirPin1;
pulsePin2 = _pulsePin2;
dirPin2 = _dirPin2;
delayTime = _delayTime1;
direction = _direction1;
togglePulse = LOW;
enable = 0;
pinMode(pulsePin1, OUTPUT); //motorOne pin 2
pinMode(dirPin1, OUTPUT); //motorOne pin 3
pinMode(pulsePin2, OUTPUT); //motorOne pin 5
pinMode(dirPin2, OUTPUT); //motorOne pin 6
}
void control(void){
currentTime = micros(); //saves the amount of microseconds since the program started within variable currentTime
digitalWrite(dirPin, direction); //
if(enable == 1){
if( (currentTime - deltaTime) > delayTime ){
pulseCount++;
// Each HIGH or LOW is a "pulse"
// But each step of the motor requires two "pulses"
if(pulseCount % 2 == 0){
stepCount++;
}
togglePulse = togglePulse == LOW ? HIGH : LOW;
digitalWrite(pulsePin, togglePulse);
deltaTime = currentTime;
}
}
}
void changeDirection(bool _direction){
direction = _direction;
}
unsigned long steps(void){
return stepCount;
}
void changeSpeed(unsigned long _speed){
delayTime = _speed;
}
private:
unsigned long delayTime, deltaTime, currentTime;
unsigned long pulseCount = 0;
unsigned long stepCount = 0;
int pulsePin, pulsePin1, pulsePin2, dirPin, dirPin1, dirPin2;
bool direction, togglePulse, enable;
};
stepperMotor stepperOne, stepperTwo; //declares variables stepperOne and Two as users of class function
void setup() {
stepperOne.init2(PUL1_PIN,DIR1_PIN,PUL2_PIN,DIR2_PIN,1000,HIGH); //turns on digital pins and sets to 1 second delay
stepperTwo.init(PUL3_PIN,DIR3_PIN,1000,HIGH); //turns on digital pins and sets 1 second delay
stepperOne.start();
stepperTwo.start();
}
void loop() {
stepperOne.control();
stepperTwo.control();
if(stepperOne.steps() == 2000){
stepperOne.changeDirection(LOW);
stepperOne.changeSpeed(600);
}
}