Probleme mit Schrittmotor und Accelstepper-lib

Müsste ich den Data Pin anpassen? :thinking:

Ja, ist ja nur Vorschlag.
Wen jedoch noch Speicher frei dann nehme das vom @MicroBahner

Danke Dir! Es funktioniert einwandfrei!

Freut mich. Wenn Du bei Speed größere Zahlen brauchst, kannst Du da einfach '%5d' statt %4d schreiben. Dann hast Du ein Feld für 5 Ziffern.

Juten Tach

Ich habe den Code nun in eine switch-case-Struktur umgeschrieben. Und ich hab da wieder ein Phänomen, das ich nicht verstehe. Wenn ich an den Potis für "Speed" oder "Ramp" drehe, wird cmd nicht auf 1, bzw. 2 gesetzt. Dies obwohl ich in die if-Anweisung reinkomme (getestet mit SerialPrint).
Weiss jemand warum?

#include <MobaTools.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 4);

const byte stepPin = 3;
const byte dirPin  = 2;
const int stepsPerRev = 400;   // Steps per Revolution ( example with 1/4 microsteps )
//const long  targetPos = 1600;         // stepper moves between 0 and targetpos
int enable = 10;

unsigned long Speed;
unsigned long Ramp;
unsigned long SpeedVal = A1;
unsigned long RampVal = A0;
long JoyStick = A2;
unsigned long Last = 0;
unsigned long LastR = 0;
long NewPos;
long NewPos2;
long OldPos = 0;
long OldPos2 = 0;
long Direction;
long SetZero = 5;
long GoToZero = 6;
long ActualStep;
long SetTarget = 7;
long GoToTarget = 8;
long TargetPosition;
char txtBuf[10];

unsigned long currentMillis;
unsigned long startMillis;
const unsigned long period = 1000;

int cmd = 0;


MoToStepper myStepper ( stepsPerRev, STEPDIR );
MoToTimer stepperPause;                    // Pause between stepper moves
bool stepperRunning;


void setup() {

  Serial.begin(9600);
  myStepper.attach( stepPin, dirPin );
  myStepper.setSpeed( 2000 );  // 60 Rev/Min ( if stepsPerRev is set correctly )
  myStepper.setRampLen( 50 );
  stepperRunning = true;

  pinMode(SpeedVal, INPUT);
  pinMode(SpeedVal, INPUT);
  pinMode(RampVal, INPUT);
  pinMode(JoyStick, INPUT);
  pinMode(enable, OUTPUT);
  pinMode(SetZero, INPUT_PULLUP);
  pinMode(GoToZero, INPUT_PULLUP);
  pinMode(SetTarget, INPUT_PULLUP);
  pinMode(GoToTarget, INPUT_PULLUP);

  digitalWrite(enable, LOW);
  delay(500);


  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();

  //display.setFont(&FreeMono9pt7b);
  display.setTextSize(1.5);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(0, 5);
  // Display static text
  display.println("Current:");
  display.setCursor(70, 5);
  display.println("Target:");
  display.setCursor(0, 40);
  display.println("Speed:");
  display.setCursor(70, 40);
  display.println("Ramp:");
  display.display();

  //startMillis = millis();
}


void loop() {


  NewPos = analogRead(SpeedVal);                              // read the Speed Value

  if ((NewPos - OldPos > 10) || (OldPos - NewPos > 10)) {     // only true if position of potentiometer has changed
    Serial.println("NewPos:");
    Serial.println(NewPos);
    Serial.println("OldPos");
    Serial.println(OldPos);
    cmd = 1;
    OldPos = NewPos;

  }

  NewPos2 = analogRead(RampVal);                              // read the Ramp value
  if ((NewPos2 - OldPos2 > 10) || (OldPos2 - NewPos2 > 10)) { // only true if position of potentiometer has changed

    cmd = 2;
    OldPos2 = NewPos2;

  }

  Direction = analogRead(JoyStick);                           //read the X axis of the joystick

  if (Direction <= 500) {                                   // Clockwise
    cmd = 3;
  }
  else if (Direction >= 520) {                              // Counterclockwise
    cmd = 4;
  }
  else if (Direction > 500 && Direction < 520) {            // stop the stepper
    cmd = 5;
  }

  if ((digitalRead(SetZero)) == LOW) {
    cmd = 6;
  }

  if ((digitalRead(SetTarget)) == LOW) {
    cmd = 7;
  }

  if ((digitalRead(GoToZero)) == LOW) {
    cmd = 8;
  }

  if ((digitalRead(GoToTarget)) == LOW) {
    cmd = 9;
  }

  Serial.println("cmd");
  Serial.println(cmd);

  switch (cmd)
  {

    case 1:                                   // read the Speed Value from the potentiometer
      Speed = (analogRead(SpeedVal)) * 2;
      myStepper.setSpeed(Speed);
      Serial.println("Speed:");
      Serial.println(Speed);
      break;

    case 2:                                   // read the Ramp Value from the potentiometer
      Ramp = (analogRead(RampVal)) * 2;
      myStepper.setRampLen(Ramp);
      break;

    case 3:                                   // move the stepper CW infinitively (Freerun Mode)
      myStepper.setSpeed(2000);
      //myStepper.setRampLen(50);
      myStepper.rotate(1);
      break;

    case 4:                                   // move the stepper CCW infinitively (Freerun Mode)
      myStepper.setSpeed(2000);
      //myStepper.setRampLen(50);
      myStepper.rotate(-1);
      break;


    case 5:                                   // stop the stepper
      myStepper.stop();
      break;

    case 6:                                   // set actual position to zero
      myStepper.setZero(0);
      break;

    case 7:                                   // set target position
      TargetPosition = myStepper.read();
      break;

    case 8:                                   // move to zero position
      myStepper.write(0);
      //myStepper.setSpeed(Speed);
      //myStepper.setRampLen(Ramp);
      while (myStepper.moving());
      break;

    case 9:                                   // move to target position
      myStepper.write(TargetPosition);
      //myStepper.setSpeed(Speed);
      //myStepper.setRampLen(Ramp);
      while (myStepper.moving());
      break;

  }
  if (Speed != Last) {

    myStepper.setSpeed(Speed);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 50);
    display.println(Speed);
    display.display();
    Last = Speed;
  }
  if (Ramp != LastR) {
    myStepper.setRampLen(Ramp);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(70, 50);
    sprintf(txtBuf, "%4d", Ramp );
    display.print(txtBuf);
    display.display();
    LastR = Ramp;
  }

  currentMillis = millis();

  if (currentMillis - startMillis >= period) {


    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 20);
    long CurrentStep = myStepper.read();
    display.println(CurrentStep);
    display.setCursor(70, 20);
    display.println(TargetPosition);
    display.display();
    startMillis = currentMillis;
  }

}

Benutze zuerst mal die automatische Formatierung mit Tastenkombination Strg-T
um alle Einrückungen in der üblichen Weise darzustellen.

Du solltest dir angewöhnen Strg-T alle paar Code-Zeilen zu drücken
Damit der Code immer gleich richtig formatiert ist.
Dabei werden dir schon der eine oder andere Fehler auffallen.

Also Code formatieren dann über das Bleistift-Symbol dein Posting # 45 editieren
den vorhanden Code herauslöschen und denn korrekt formatierten Code einsetzen

Ich sehe in deinem Code kein Switch-case

och blöd, hab den alten Code gepostet. warte..

Wenn du genau nachvollziehen willst was dein Code macht dann musst du dir das sehr viel ausführlicher und mit Werten ausgeben machen

Hier die entsprechende Code-Version.
Das hat jetzt 10 Minuten gedauert. Also kurz.

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <MobaTools.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 4);

const byte stepPin = 3;
const byte dirPin  = 2;
const int stepsPerRev = 400;   // Steps per Revolution ( example with 1/4 microsteps )
//const long  targetPos = 1600;         // stepper moves between 0 and targetpos
int enable = 10;

unsigned long Speed;
unsigned long Ramp;
unsigned long SpeedVal = A1;
unsigned long RampVal = A0;
long JoyStick = A2;
unsigned long Last = 0;
unsigned long LastR = 0;
long NewPos;
long NewPos2;
long OldPos = 0;
long OldPos2 = 0;
long Direction;
long SetZero = 5;
long GoToZero = 6;
long ActualStep;
long SetTarget = 7;
long GoToTarget = 8;
long TargetPosition;
char txtBuf[10];

unsigned long currentMillis;
unsigned long startMillis;
const unsigned long period = 1000;

int cmd = 0;


MoToStepper myStepper ( stepsPerRev, STEPDIR );
MoToTimer stepperPause;                    // Pause between stepper moves
bool stepperRunning;


void setup() {

  Serial.begin(115200);
  myStepper.attach( stepPin, dirPin );
  myStepper.setSpeed( 2000 );  // 60 Rev/Min ( if stepsPerRev is set correctly )
  myStepper.setRampLen( 50 );
  stepperRunning = true;

  pinMode(SpeedVal, INPUT);
  pinMode(SpeedVal, INPUT);
  pinMode(RampVal, INPUT);
  pinMode(JoyStick, INPUT);
  pinMode(enable, OUTPUT);
  pinMode(SetZero, INPUT_PULLUP);
  pinMode(GoToZero, INPUT_PULLUP);
  pinMode(SetTarget, INPUT_PULLUP);
  pinMode(GoToTarget, INPUT_PULLUP);

  digitalWrite(enable, LOW);
  delay(500);


  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();

  //display.setFont(&FreeMono9pt7b);
  display.setTextSize(1.5);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(0, 5);
  // Display static text
  display.println("Current:");
  display.setCursor(70, 5);
  display.println("Target:");
  display.setCursor(0, 40);
  display.println("Speed:");
  display.setCursor(70, 40);
  display.println("Ramp:");
  display.display();

  //startMillis = millis();
}


void loop() {


  NewPos = analogRead(SpeedVal);                              // read the Speed Value

  if ((NewPos - OldPos > 10) || (OldPos - NewPos > 10)) {     // only true if position of potentiometer has changed
    //Serial.println("NewPos:");
    //Serial.println(NewPos);
    //Serial.println("OldPos");
    //Serial.println(OldPos);
    dbgc("01", NewPos);
    cmd = 1;
    OldPos = NewPos;
  }
  dbgc("011", cmd);

  NewPos2 = analogRead(RampVal);                              // read the Ramp value
  if ((NewPos2 - OldPos2 > 10) || (OldPos2 - NewPos2 > 10)) { // only true if position of potentiometer has changed
    dbgc("02", NewPos2);
    cmd = 2;
    OldPos2 = NewPos2;
  }
  dbgc("021", cmd);

  Direction = analogRead(JoyStick);                           //read the X axis of the joystick

  if (Direction <= 500) {                                   // Clockwise
    cmd = 3;
    dbgc("03", cmd);
  }
  else if (Direction >= 520) {                              // Counterclockwise
    cmd = 4;
    dbgc("04", cmd);
  }
  else if (Direction > 500 && Direction < 520) {            // stop the stepper
    cmd = 5;
    dbgc("05", cmd);
  }

  if ((digitalRead(SetZero)) == LOW) {
    cmd = 6;
    dbgc("06", cmd);
  }

  if ((digitalRead(SetTarget)) == LOW) {
    cmd = 7;
    dbgc("07", cmd);
  }

  if ((digitalRead(GoToZero)) == LOW) {
    cmd = 8;
    dbgc("08", cmd);
  }

  if ((digitalRead(GoToTarget)) == LOW) {
    cmd = 9;
    dbgc("09", cmd);
  }

  Serial.println("cmd");
  Serial.println(cmd);

  switch (cmd) {

    case 1:                                   // read the Speed Value from the potentiometer
      Speed = (analogRead(SpeedVal)) * 2;
      dbgc("C1",Speed);
      myStepper.setSpeed(Speed);
      Serial.println("Speed:");
      Serial.println(Speed);
      break;

    case 2:                                   // read the Ramp Value from the potentiometer
      Ramp = (analogRead(RampVal)) * 2;
      dbgc("C2",Ramp);
      myStepper.setRampLen(Ramp);
      break;

    case 3:                                   // move the stepper CW infinitively (Freerun Mode)
      myStepper.setSpeed(2000);
      //myStepper.setRampLen(50);
      myStepper.rotate(1);
      break;

    case 4:                                   // move the stepper CCW infinitively (Freerun Mode)
      myStepper.setSpeed(2000);
      //myStepper.setRampLen(50);
      myStepper.rotate(-1);
      break;


    case 5:                                   // stop the stepper
      myStepper.stop();
      break;

    case 6:                                   // set actual position to zero
      myStepper.setZero(0);
      break;

    case 7:                                   // set target position
      TargetPosition = myStepper.read();
      break;

    case 8:                                   // move to zero position
      myStepper.write(0);
      //myStepper.setSpeed(Speed);
      //myStepper.setRampLen(Ramp);
      while (myStepper.moving());
      break;

    case 9:                                   // move to target position
      myStepper.write(TargetPosition);
      //myStepper.setSpeed(Speed);
      //myStepper.setRampLen(Ramp);
      while (myStepper.moving());
      break;

  }
  
  if (Speed != Last) {
    myStepper.setSpeed(Speed);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 50);
    display.println(Speed);
    display.display();
    Last = Speed;
  }
  dbgc("update 1",Speed);
  
  if (Ramp != LastR) {
    myStepper.setRampLen(Ramp);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(70, 50);
    sprintf(txtBuf, "%4d", Ramp );
    display.print(txtBuf);
    display.display();
    LastR = Ramp;
  }
  dbgc("update 2",Ramp);

  currentMillis = millis();

  if (currentMillis - startMillis >= period) {
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 20);
    long CurrentStep = myStepper.read();
    display.println(CurrentStep);
    display.setCursor(70, 20);
    display.println(TargetPosition);
    display.display();
    startMillis = currentMillis;
  }
}

Danke für die Formatierung zum ausführlicheren Debugging! Ich sehe nun, dass ich doch in den switch reinkomme, aber das offenbar "Ramp" und "Speed" nicht verändert werden

Nimmmal hier noch das Serial.print heraus

  }

  Serial.println("cmd"); <<<===== auskommentieren
  Serial.println(cmd);

  switch (cmd) {

Das macht den ganzen nutzen von dbgc zunichte
Es soll ja immer nur dann etwas ausgegeben werden wenn sich etwas geändert hat.
Das macht dbgc

Habe ich gemacht. Speed und Ramp ändern sich noch immer nicht. Warum verstehe ich echt nicht :see_no_evil:

Erst mal die Frage: benutzt du IDE 2.2.1 oder IDE 1.8.19?

Dann poste doch mal was im seriellen Monitor ausgegeben wird.

Ich benutze 1.8.19. Wie Du unten siehst, geht er in den case 1 (Speed) rein. Aber er ändert nichts am Wert Speed.

"01" NewPos changed from 521 to 532
"011" cmd changed from 5 to 1
"021" cmd changed from 5 to 1
"011" cmd changed from 1 to 5
"021" cmd changed from 1 to 5
"01" NewPos changed from 532 to 543
"011" cmd changed from 5 to 1
"021" cmd changed from 5 to 1
"011" cmd changed from 1 to 5
"021" cmd changed from 1 to 5
"01" NewPos changed from 543 to 554
"011" cmd changed from 5 to 1
"021" cmd changed from 5 to 1
"011" cmd changed from 1 to 5
"021" cmd changed from 1 to 5
"01" NewPos changed from 554 to 565
"011" cmd changed from 5 to 1
"021" cmd changed from 5 to 1
"011" cmd changed from 1 to 5
"021" cmd changed from 1 to 5
"01" NewPos changed from 565 to 576
"011" cmd changed from 5 to 1
"021" cmd changed from 5 to 1
"011" cmd changed from 1 to 5
"021" cmd changed from 1 to 5

Hier ist eine Version mit folgenden Änderungen

  if ((digitalRead(GoToTarget)) == LOW) {
    cmd = 9;
    dbgc("09", cmd);
  }

  //Serial.println("cmd");
  //Serial.println(cmd);

  dbgi("vor switch", cmd, 1000); // neu einmal pro Sekunde Wert von cmd ausgeben

  switch (cmd) {
    case 9:                                   // move to target position
      myStepper.write(TargetPosition);
      //myStepper.setSpeed(Speed);
      //myStepper.setRampLen(Ramp);
      while (myStepper.moving());
      break;

    default:  // neu 02.01.24 17:16 default-case
      dbgi("unknown cmd",cmd,500);
      break;
  }

  if (Speed != Last) {
    myStepper.setSpeed(Speed);
    display.setTextColor(WHITE, BLACK);

hier der gesamte Sketch

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <MobaTools.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 4);

const byte stepPin = 3;
const byte dirPin  = 2;
const int stepsPerRev = 400;   // Steps per Revolution ( example with 1/4 microsteps )
//const long  targetPos = 1600;         // stepper moves between 0 and targetpos
int enable = 10;

unsigned long Speed;
unsigned long Ramp;
unsigned long SpeedVal = A1;
unsigned long RampVal = A0;
long JoyStick = A2;
unsigned long Last = 0;
unsigned long LastR = 0;
long NewPos;
long NewPos2;
long OldPos = 0;
long OldPos2 = 0;
long Direction;
long SetZero = 5;
long GoToZero = 6;
long ActualStep;
long SetTarget = 7;
long GoToTarget = 8;
long TargetPosition;
char txtBuf[10];

unsigned long currentMillis;
unsigned long startMillis;
const unsigned long period = 1000;

int cmd = 0;


MoToStepper myStepper ( stepsPerRev, STEPDIR );
MoToTimer stepperPause;                    // Pause between stepper moves
bool stepperRunning;


void setup() {

  Serial.begin(115200);
  myStepper.attach( stepPin, dirPin );
  myStepper.setSpeed( 2000 );  // 60 Rev/Min ( if stepsPerRev is set correctly )
  myStepper.setRampLen( 50 );
  stepperRunning = true;

  pinMode(SpeedVal, INPUT);
  pinMode(SpeedVal, INPUT);
  pinMode(RampVal, INPUT);
  pinMode(JoyStick, INPUT);
  pinMode(enable, OUTPUT);
  pinMode(SetZero, INPUT_PULLUP);
  pinMode(GoToZero, INPUT_PULLUP);
  pinMode(SetTarget, INPUT_PULLUP);
  pinMode(GoToTarget, INPUT_PULLUP);

  digitalWrite(enable, LOW);
  delay(500);


  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();

  //display.setFont(&FreeMono9pt7b);
  display.setTextSize(1.5);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(0, 5);
  // Display static text
  display.println("Current:");
  display.setCursor(70, 5);
  display.println("Target:");
  display.setCursor(0, 40);
  display.println("Speed:");
  display.setCursor(70, 40);
  display.println("Ramp:");
  display.display();

  //startMillis = millis();
}


void loop() {


  NewPos = analogRead(SpeedVal);                              // read the Speed Value

  if ((NewPos - OldPos > 10) || (OldPos - NewPos > 10)) {     // only true if position of potentiometer has changed
    //Serial.println("NewPos:");
    //Serial.println(NewPos);
    //Serial.println("OldPos");
    //Serial.println(OldPos);
    dbgc("01", NewPos);
    cmd = 1;
    OldPos = NewPos;
    dbgc("011", cmd);
  }

  NewPos2 = analogRead(RampVal);                              // read the Ramp value
  if ((NewPos2 - OldPos2 > 10) || (OldPos2 - NewPos2 > 10)) { // only true if position of potentiometer has changed
    dbgc("02", NewPos2);
    cmd = 2;
    OldPos2 = NewPos2;
    dbgc("021", cmd);
  }

  Direction = analogRead(JoyStick);                           //read the X axis of the joystick

  if (Direction <= 500) {                                   // Clockwise
    cmd = 3;
    dbgc("03", cmd);
  }
  else if (Direction >= 520) {                              // Counterclockwise
    cmd = 4;
    dbgc("04", cmd);
  }
  else if (Direction > 500 && Direction < 520) {            // stop the stepper
    cmd = 5;
    dbgc("05", cmd);
  }

  if ((digitalRead(SetZero)) == LOW) {
    cmd = 6;
    dbgc("06", cmd);
  }

  if ((digitalRead(SetTarget)) == LOW) {
    cmd = 7;
    dbgc("07", cmd);
  }

  if ((digitalRead(GoToZero)) == LOW) {
    cmd = 8;
    dbgc("08", cmd);
  }

  if ((digitalRead(GoToTarget)) == LOW) {
    cmd = 9;
    dbgc("09", cmd);
  }

  //Serial.println("cmd");
  //Serial.println(cmd);

  dbgi("vor switch", cmd, 1000); // neu einmal pro Sekunde Wert von cmd ausgeben

  switch (cmd) {

    case 1:                                   // read the Speed Value from the potentiometer
      Speed = (analogRead(SpeedVal)) * 2;
      dbgc("C1", Speed);
      myStepper.setSpeed(Speed);
      Serial.println("Speed:");
      Serial.println(Speed);
      break;

    case 2:                                   // read the Ramp Value from the potentiometer
      Ramp = (analogRead(RampVal)) * 2;
      dbgc("C2", Ramp);
      myStepper.setRampLen(Ramp);
      break;

    case 3:                                   // move the stepper CW infinitively (Freerun Mode)
      myStepper.setSpeed(2000);
      //myStepper.setRampLen(50);
      myStepper.rotate(1);
      break;

    case 4:                                   // move the stepper CCW infinitively (Freerun Mode)
      myStepper.setSpeed(2000);
      //myStepper.setRampLen(50);
      myStepper.rotate(-1);
      break;


    case 5:                                   // stop the stepper
      myStepper.stop();
      break;

    case 6:                                   // set actual position to zero
      myStepper.setZero(0);
      break;

    case 7:                                   // set target position
      TargetPosition = myStepper.read();
      break;

    case 8:                                   // move to zero position
      myStepper.write(0);
      //myStepper.setSpeed(Speed);
      //myStepper.setRampLen(Ramp);
      while (myStepper.moving());
      break;

    case 9:                                   // move to target position
      myStepper.write(TargetPosition);
      //myStepper.setSpeed(Speed);
      //myStepper.setRampLen(Ramp);
      while (myStepper.moving());
      break;

    default:  // neu 02.01.24 17:16 default-case
      dbgi("unknown cmd",cmd,500);
      break;
  }

  if (Speed != Last) {
    myStepper.setSpeed(Speed);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 50);
    display.println(Speed);
    display.display();
    Last = Speed;
    dbgc("update 1", Speed);
  }

  if (Ramp != LastR) {
    myStepper.setRampLen(Ramp);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(70, 50);
    sprintf(txtBuf, "%4d", Ramp );
    display.print(txtBuf);
    display.display();
    LastR = Ramp;
    dbgc("update 2", Ramp);
  }

  currentMillis = millis();

  if (currentMillis - startMillis >= period) {
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 20);
    long CurrentStep = myStepper.read();
    display.println(CurrentStep);
    display.setCursor(70, 20);
    display.println(TargetPosition);
    display.display();
    startMillis = currentMillis;
  }
}

Jetzt komme ich laut dem Serial nicht mehr in cmd 1 und 2 rein. An Speed und Ramp ändert sich immer noch nix

Lieber Laurin,

poste das was im seriellen Monitor ausgegeben wird.
Und zwar

IMMER

Ok, sorry. Das kommt raus, wenn ich nach dem startup am Poti drehe:

"01" NewPos changed from 0 to 161
"011" cmd changed from 0 to 1
"02" NewPos2 changed from 0 to 138
"021" cmd changed from 0 to 2
"05" cmd changed from 0 to 5
"vor switch" cmd=5
"vor switch" cmd=5
"vor switch" cmd=5
"vor switch" cmd=5
"01" NewPos changed from 161 to 150
"01" NewPos changed from 150 to 139
"01" NewPos changed from 139 to 128
"01" NewPos changed from 128 to 117
"01" NewPos changed from 117 to 106
"01" NewPos changed from 106 to 95
"01" NewPos changed from 95 to 84
"vor switch" cmd=5
"01" NewPos changed from 84 to 73
"01" NewPos changed from 73 to 53
"01" NewPos changed from 53 to 42
"01" NewPos changed from 42 to 31
"01" NewPos changed from 31 to 20
"01" NewPos changed from 20 to 9
"vor switch" cmd=5
"vor switch" cmd=5
"vor switch" cmd=5
"vor switch" cmd=5
"vor switch" cmd=5

unpräzise Beschreibung. Laut Ausgabe hast du an beiden Potis gedreht.
So und jetzt kannst du nachvollziehen was der Code macht.

Wie die Ausgabe schon sagt:
"vor switch" cmd=5
und was ist cmd=5??

Wo wird cmd=5 gesetzt?

Die ganzen vielen ifs gehören normalerweise in die einzelnen cases. So ist das noch keine Statemaschine. In den cases wartet man auf eine Veränderung. Wenn die Auftritt, dann wird eine aktion ausgelöst und man wechselt in einen anderen case (aka state). Schau dir mal Arduino: Programmiertechniken/Statemaschine – Wikibooks, Sammlung freier Lehr-, Sach- und Fachbücher an.