Hello everyone,
I'm new to the Arduino world and am currently trying my hand at my first project.
I hope I have categorized my topic correctly here.
Otherwise, I apologize in advance.
I have bought a linear axis and 2 stepper motors + drivers and am trying to get the whole thing running. The aim is for the carriage of the linear axis to move back and forth between two end stops. I want to be able to control the speed of both motors via the serial interface and show the current speed on the small LCD display.
With the help of chat gpt I have already made good progress with the code, but now I'm stuck and don't know what to do.
The problem is that I can set the speed of the motors, but they only change in the range from 0 to 100. For example, there is no change in speed when I set motor 1 (at the top of the carriage) to 100 or 2000. However, I can see a difference when I set the motor from 20 to 100.
The display and the serial monitor show me the correct speed I have entered. (also above 100)
Here is some information about my hardware:
Board: Arduino UNO + Shield
Driver: 2x DM542T
Stepper:
1x NEMA23 23HS45 - 4204S (4.2A; 3Nm)
1x NEMA23 n.a. (Chinese) should create a holding torque of 1.2Nm at 24VDC and 2A
Voltage source: 600W, adjustable from 0-48V
Enclosed you can see my setup:
and the following code
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <LiquidCrystal_I2C.h>
#define PIN_TASTER1 7
#define PIN_TASTER2 6
bool button1 = false;
bool button1_old = false;
bool button2 = false;
bool button2_old = false;
int speed1 = 0;
int speed2 = 0; // Startgeschwindigkeit des Motors 2 ist negativ
long timeOld = 0;
int buttonLock = 0; //millis
// Schrittmotoren-Pins
#define MOTOR1_STEP_PIN 9
#define MOTOR1_DIR_PIN 8
#define MOTOR2_STEP_PIN 11
#define MOTOR2_DIR_PIN 10
// Maximale Geschwindigkeiten für die Motoren
int maxSpeed1 =2000;
int maxSpeed2 =2000;
// Erstelle Objekte für die Motoren
AccelStepper stepper1(AccelStepper::DRIVER, MOTOR1_STEP_PIN, MOTOR1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, MOTOR2_STEP_PIN, MOTOR2_DIR_PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(19200); // Initialisiere die serielle Kommunikation für Debugging-Ausgaben
pinMode(PIN_TASTER1, INPUT_PULLUP);
pinMode(PIN_TASTER2, INPUT_PULLUP);
// Initialisierung der Motoren
stepper1.setMaxSpeed(maxSpeed1);
stepper1.setSpeed(speed1);
stepper1.setAcceleration(200); // Beschleunigung für Motor 1
stepper2.setMaxSpeed(maxSpeed2); // Setze die maximale Geschwindigkeit des zweiten Motors
stepper2.setSpeed(speed2);
stepper2.setAcceleration(200); // Beschleunigung für Motor 2
timeOld = millis() - buttonLock; //millis
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.noCursor(); // Deaktiviere den Cursor
lcd.noBlink(); // Deaktiviere das Blinken
lcd.setCursor(3, 0);
lcd.print("Test");
lcd.setCursor(2, 1);
lcd.print("1-2-3");
}
void loop() {
bool speedChanged = false; // Flag, das anzeigt, ob sich die Geschwindigkeit geändert hat
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
if (input.startsWith("S1:")) {
speed1 = input.substring(3).toInt();
if (speed1 > maxSpeed1) {
Serial.print("Maximalgeschwindigkeit von Motor 1 überschritten! Eingegebene Geschwindigkeit wurde auf ");
Serial.print(maxSpeed1);
Serial.println(" begrenzt.");
speed1 = maxSpeed1;
}
stepper1.setSpeed(speed1);
Serial.print("Speed 1 set to: ");
Serial.println(speed1);
speedChanged = true;
} else if (input.startsWith("S2:")) {
speed2 = input.substring(3).toInt();
if (speed2 > maxSpeed2) {
Serial.print("Maximalgeschwindigkeit von Motor 2 überschritten! Eingegebene Geschwindigkeit wurde auf ");
Serial.print(maxSpeed2);
Serial.println(" begrenzt.");
speed2 = maxSpeed2;
}
stepper2.setSpeed(speed2);
Serial.print("Speed 2 set to: ");
Serial.println(speed2);
speedChanged = true;
// Beachte Drehrichtung des 2. Motors bei manueller Eingabe
}
}
// Taster 1 gedrückt: Motor 2 Geschwindigkeit positiv
if (digitalRead(PIN_TASTER1) == LOW) {
speed2 = -abs(speed2); // Stelle sicher, dass die Geschwindigkeit von Motor 2 positiv ist
stepper2.setSpeed(speed2);
Serial.println("Taster1 wird gedrückt\n");
speedChanged = true;
}
// Taster 2 gedrückt: Motor 2 Geschwindigkeit negativ
if (digitalRead(PIN_TASTER2) == LOW) {
speed2 = abs(speed2); // Stelle sicher, dass die Geschwindigkeit von Motor 2 negativ ist
stepper2.setSpeed(speed2);
Serial.println("Taster2 wird gedrückt\n");
speedChanged = true;
}
// Bewegung der Motoren
stepper1.runSpeed();
//Serial.print(speed1);
stepper2.runSpeed();
// Überprüfe, ob sich die Geschwindigkeit geändert hat und aktualisiere die Anzeige entsprechend
if (speedChanged) {
lcd.clear(); // Lösche den alten Text
lcd.setCursor(0, 0);
lcd.print("Speed 1: ");
lcd.print(speed1);
lcd.setCursor(0, 1);
lcd.print("Speed 2: ");
lcd.print(speed2); // Anzeige der tatsächlichen Geschwindigkeit von Motor 2
Serial.println("Geschwindigkeiten aktualisiert");
}
delay(10);
}
I have already written another code that only controls one motor at a time to check whether the hardware is connected correctly.
In my second simple code, the motors rotate at different speeds as desired.
I therefore think that everything is connected correctly on the hardware side.
Many thanks for your support

