So I want to connect a DM542T to Arduino Leonardo and use a Potentiometer to control speed and a joystick to control direction. My pin connections are:
Potentiometer A0
Joystick A1
DM542T (Pul+) D5
DM542T (Dir+) D6
Motor (blue) B-
Motor (red) B+
Motor (green) A-
Motor (black) A+
My code is:
#define Pulse 5 // Pulse pin
#define Dir 6 // Direction pin
#define PotPin A0 // Potentiometer pin
#define JoyPin A1 // Joystick X-axis pin
int stepsPerTurn = 200; // Number of steps per revolution
long previousMicros = 0; // Variable to store the last time pulse was sent
void setup() {
pinMode(Pulse, OUTPUT);
pinMode(Dir, OUTPUT);
Serial.begin(9600); // For debugging
}
void loop() {
int potValue = analogRead(PotPin); // Read the potentiometer value
int joyValue = analogRead(JoyPin); // Read the joystick X-axis value
// Map the potentiometer value to a delay value in microseconds
long delayMicros = map(potValue, 0, 1023, 1000, 10000);
// Determine the direction based on joystick value
if (joyValue < 512) {
digitalWrite(Dir, LOW); // Set direction to LOW
} else {
digitalWrite(Dir, HIGH); // Set direction to HIGH
}
// Generate pulses to drive the stepper motor
unsigned long currentMicros = micros();
if (currentMicros - previousMicros >= delayMicros) {
previousMicros = currentMicros;
// Generate pulse
digitalWrite(Pulse, HIGH);
delayMicroseconds(10); // Pulse width
digitalWrite(Pulse, LOW);
}
// Debugging: Print values to the Serial Monitor
Serial.print("PotValue: ");
Serial.print(potValue);
Serial.print(" JoyValue: ");
Serial.print(joyValue);
Serial.print(" delayMicros: ");
Serial.println(delayMicros);
}
However, nothing is working. I'm using a S-400-36 to power the DM452T and Nema 23. The motor is locked and I cannot turn it physically, but I can turn it manually when it's not powered. Any help would be greatly appreciated