Can't use serial commands + joystick for stepper control

Hello guys for my project i want to control a stepper motor using serial commands and using a joystick. The problem is that when using both, the control command (:set:pos) doesn't work. The other commands like :get:pos or :set:speed do work. Now when i comment my joystick control (almost at the end of the code ) then my commands :set:pos does work. How is this possible?

Code:

#include <AccelStepper.h>
#include <EEPROM.h>

// Define pin connections
const int dirPinX = 2;
const int stepPinX = 3;
const int dirPinY = 4;
const int stepPinY = 5;
const int joyPinX = A0; // Analog pin for joystick X-axis
const int joyPinY = A1; // Analog pin for joystick Y-axis

// Define motor interface type
#define motorInterfaceType 1

// Creates instances for X and Y axes
AccelStepper stepperX(motorInterfaceType, stepPinX, dirPinX);
AccelStepper stepperY(motorInterfaceType, stepPinY, dirPinY);

// Variables to store target positions for X and Y axes
int targetPositionX = 0;
int targetPositionY = 0;

// Variable to store the default speed
int defaultSpeed = 1000;

// Variable to store the original default speed
int originalDefaultSpeed = defaultSpeed;

// Define joystick deadzone
const int joystickDeadzone = 500; // Adjust as needed

// Define joystick neutral zone
const int joystickNeutralZone = 100; // Adjust as needed

void setup() {
  // Initialize Serial communication
  Serial.begin(9600);
  while (!Serial) {}

  // Initialize the stepper motors
  stepperX.setMaxSpeed(defaultSpeed);
  stepperX.setAcceleration(2000);
  stepperX.setSpeed(200);

  stepperY.setMaxSpeed(defaultSpeed);
  stepperY.setAcceleration(2000);
  stepperY.setSpeed(200);

  Serial.println("\nArduino is ready to receive commands.");
}

void loop() {
  // Check for serial commands
  if (Serial.available() > 0) {
    // Read the incoming command from Serial
    String command = Serial.readStringUntil('\n');

    // Convert command to lowercase
    command.toLowerCase();

    // Print the received command
    Serial.println("Received command: " + command);

    if (command.startsWith(":set:pos:x")) {
      // Extract X coordinate from the command
      int spaceIndex = command.indexOf(' ');
      if (spaceIndex != -1) {
        String xPosString = command.substring(spaceIndex + 1);
        int xPos = xPosString.toInt();

        // Print the target position
        Serial.print("Going to position X: ");
        Serial.println(xPos);

        // Set the target position for the X axis
        targetPositionX = xPos;

        // Move the X axis to the target position
        stepperX.moveTo(targetPositionX);
      }
    } else if (command.startsWith(":set:pos:y")) {
      // Extract Y coordinate from the command
      int spaceIndex = command.indexOf(' ');
      if (spaceIndex != -1) {
        String yPosString = command.substring(spaceIndex + 1);
        int yPos = yPosString.toInt();

        // Print the target position
        Serial.print("Going to position Y: ");
        Serial.println(yPos);

        // Set the target position for the Y axis
        targetPositionY = yPos;

        // Move the Y axis to the target position
        stepperY.moveTo(targetPositionY);
      }
    } else if (command.startsWith(":set:pos")) {
      // Extract X and Y coordinates from the command
      int commaIndex = command.indexOf(',');
      if (commaIndex != -1) {
        String posString = command.substring(command.indexOf(' ') + 1);
        int commaIndex = posString.indexOf(',');
        if (commaIndex != -1) {
          String xPosString = posString.substring(0, commaIndex);
          String yPosString = posString.substring(commaIndex + 1);

          int xPos = xPosString.toInt();
          int yPos = yPosString.toInt();

          // Print the target position
          Serial.print("Going to position X: ");
          Serial.print(xPos);
          Serial.print("\tY: ");
          Serial.println(yPos);

          // Set the target positions for X and Y axes
          targetPositionX = xPos;
          targetPositionY = yPos;

          // Move the X and Y axes to the target positions
          stepperX.moveTo(targetPositionX);
          stepperY.moveTo(targetPositionY);
        }
      }
    } else if (command.startsWith(":set:speed") || command.startsWith(":set:spe")) {
      // Extract speed value from the command
      int spaceIndex = command.indexOf(' ');
      if (spaceIndex != -1) {
        String speedString = command.substring(spaceIndex + 1);
        if (speedString.startsWith("def") || speedString.startsWith("default")) {
          defaultSpeed = originalDefaultSpeed;
          stepperX.setMaxSpeed(defaultSpeed);
          stepperY.setMaxSpeed(defaultSpeed);
          Serial.println("Default speed set to: " + String(defaultSpeed));
        } else {
          int speedValue = speedString.toInt();

          // Check if the speed value is within the allowed range (100 to 2000)
          if (speedValue >= 100 && speedValue <= 2000) {
            defaultSpeed = speedValue;
            stepperX.setMaxSpeed(defaultSpeed);
            stepperY.setMaxSpeed(defaultSpeed);
            Serial.println("Default speed set to: " + String(defaultSpeed));
          } else {
            Serial.println("Invalid speed value. Please enter a value between 100 and 2000.");
          }
        }
      }
    } else if (command.startsWith(":get:pos?") || command.startsWith(":get:position?")) {
      // Display the current positions of the motors
      Serial.print("Current position X: ");
      Serial.print(stepperX.currentPosition());
      Serial.print("\tCurrent position Y: ");
      Serial.println(stepperY.currentPosition());
    } else if (command.startsWith(":get:speed?") || command.startsWith(":get:spe?")) {
      // Display the current default speed of the motors
      Serial.print("Default speed: ");
      Serial.println(defaultSpeed);
    } else {
      // Invalid command format
      Serial.println("Invalid command format.");
    }
    
  }
/*
// Read joystick input
  int joyX = analogRead(joyPinX);
  int joyY = analogRead(joyPinY);

  // Apply deadzone to joystick input
  if (abs(joyX - 512) < joystickDeadzone) {
    joyX = 512;
  }
  if (abs(joyY - 512) < joystickDeadzone) {
    joyY = 512;
  }

  // Check if joystick is in neutral position
  if (abs(joyX - 512) < joystickNeutralZone && abs(joyY - 512) < joystickNeutralZone) {
    // Stop the stepper motors
    stepperX.setSpeed(0);
    stepperY.setSpeed(0);
  } else {
    // Map joystick input to motor movement for X axis
    int xPos_joy = map(joyX, 0, 1023, -5000, 5000); // Map analog input (0-1023) to motor position (0-2000)
    stepperX.moveTo(xPos_joy);

    // Map joystick input to motor movement for Y axis
    int yPos_joy = map(joyY, 0, 1023, -5000, 5000); // Map analog input (0-1023) to motor position (0-2000)
    stepperY.moveTo(yPos_joy);
  }
*/
  // Run the motors for X and Y axes
  stepperX.run();
  stepperY.run();

  // Save the current positions and default speed to EEPROM
  //EEPROM.put(EEPROM_ADDRESS_X, stepperX.currentPosition());
  //EEPROM.put(EEPROM_ADDRESS_Y, stepperY.currentPosition());
  //EEPROM.put(EEPROM_ADDRESS_SPEED, defaultSpeed);
}

Welcome to the forum

You started a topic in the Uncategorised category of the forum

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

@Delta_G thank you very much. It was indeed the problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.