Strange problem with arduino code, upload success but not execute

i am writing to you because it is happening a strange problem to me that never happened before.

Basically I am able to upload successfully the code but it didn't start. What i found that the problem is in runTrack function when we call SetSpeed(). There is a variable that stuck all the code.

If i comment SetSpeed() everything is working fine.

Could you help me please? Thanks a lot

Header Code:

#define Xaxis A1
#define pinButton 3
#define limitSwitchLeft  9

#define OLED_RESET 4

#define X_dirPin 2
#define X_stepPin 5

#define Z_dirPin 4
#define Z_stepPin 7

#define motorInterfaceType 1

AccelStepper myStepper1(motorInterfaceType, X_stepPin, X_dirPin);
AccelStepper myStepper2(motorInterfaceType, Z_stepPin, Z_dirPin);

MultiStepper steppers;

Adafruit_SSD1306 display(OLED_RESET);

int rotationdirection = 0;
int menuPosition = 1;
int modeSelected = 0;
int prevmenuPosition = 0;
volatile bool turnDetected = false;
unsigned long lastClick = 0;
int flag = 0;
boolean confirmed = false;
long XstartPoint;
long YstartPoint;
long XendPoint;
long YendPoint;

int selected = 0;
long goToPosition[2];
long totalDistance = 0;

long speedValue = 0;
int motorSpeed = 0;
int timeInSec = 0;

Funzone header file Olehana Arabic

void stepperposition(int n)
{
  myStepper1.setMaxSpeed(3000);
  myStepper1.setSpeed(200);
  myStepper1.setMaxSpeed(3000);
  myStepper1.setSpeed(200);

  //  Serial.print("Posizione: ");
  //  Serial.println(myStepper1.currentPosition());
  //  Serial.print("Valore SwitchL: ");
  //  Serial.println(digitalRead(limitSwitchLeft));

  if (turnDetected)
  {
    turnDetected = false;  // do NOT repeat IF loop until new rotation detected
    if (n == 1)
    {
      /*
          Se la direzione di rotazione è -1, devo andare indietro con il mio carrellino
          All' inizio la mia posizione sarà 100, poi 500 e cos+ via fino arrivare a 0,
          ad ogni interazione con il joystick vado indietro con il mio carrellino di 500 step.
          Per fare gli ultimi passi, nel caso avessi una distanza non multiplo di 500
          faccio un ciclo while per arrivare fino in cima.
      */
      if (rotationdirection == -1)
      {
        long numero1 = myStepper1.currentPosition() + 500;
        //        Serial.println(numero1);
        if (numero1 <= 0 )
        {
          myStepper1.move(500);
          while (myStepper1.distanceToGo() != 0)
          {
            myStepper1.setSpeed(3000);
            myStepper1.runSpeed();
          }
        }
        else
        {
          while (myStepper1.currentPosition() != 0)
          {
            myStepper1.setSpeed(3000);
            myStepper1.runSpeed();
          }
        }
      }

      if (rotationdirection == 1)
      {
        /*
           Se la direzione di rotazione è 1, devo andare avanti con il mio carrellino
           All' inizio la mia posizione sarà 0, poi 500 e cos+ via, ad ogni interazione con il joystick
           vado avanti con il mio carrellino di 500 step.
           Per fare gli ultimi passi, nel caso avessi una distanza non multiplo di 500
           faccio un ciclo while per arrivare fino in fondo alla distanza.
        */

        long numero = abs(myStepper1.currentPosition() - 500);
        //        Serial.println(numero);
        if (numero < 85000 )
        {
          myStepper1.move(-500);
          while (myStepper1.distanceToGo() != 0)
          {
            myStepper1.setSpeed(-3000);
            myStepper1.runSpeed();
          }
        }
        else
        {
          while (abs(myStepper1.currentPosition()) != 85000)
          {
            myStepper1.setSpeed(-3000);
            myStepper1.runSpeed();

          }
        }
      }
    }

    if (n == 2) {
      if (rotationdirection == -1) {

        myStepper2.move(50);
        while (myStepper2.distanceToGo() != 0) {
          myStepper2.setSpeed(1000);
          myStepper2.runSpeed();
        }
      }
      if (rotationdirection == 1) {

        myStepper2.move(-50);
        while (myStepper2.distanceToGo() != 0) {
          myStepper2.setSpeed(-1000);
          myStepper2.runSpeed();
        }
      }
    }
  }
}

void changeSection()
{
  if (millis() - lastClick > 500)
    flag = flag + 1;
  else
    confirmed = true;

  lastClick = millis();


}

void Home()
{
  myStepper1.setMaxSpeed(3000);
  myStepper1.setSpeed(200);

  if (digitalRead(limitSwitchLeft) == 1)
  {
    display.clearDisplay();
    display.setCursor(10, 28);
    display.print("Homing");
    display.display();
  }
  while (digitalRead(limitSwitchLeft) == 1)
  {
    myStepper1.setSpeed(3000);
    myStepper1.runSpeed();
  }

  delay(20);
  myStepper1.setCurrentPosition(0);
  myStepper1.moveTo(-500);
  while (myStepper1.distanceToGo() != 0)
  {
    myStepper1.setSpeed(-1000);
    myStepper1.runSpeed();
  }
  myStepper1.setCurrentPosition(0);
  display.clearDisplay();
}


void joystickReading() {
  // Read new position
  int analog_in = analogRead(Xaxis);
  //Serial.println(analog_in);

  if (analog_in < 400 || analog_in > 600)
  {
    turnDetected = true;
    if (analog_in < 400)
    {
      rotationdirection = 1;
    } else if (analog_in > 600)
    {
      rotationdirection = -1;
    }
  }
  else
  {
    turnDetected = false;
    rotationdirection = 0;
  }
}

void updateMainMenu()                               //Updates the display data for the main menu
{

  display.clearDisplay();
  display.setCursor(28, 4);
  display.print("Camera Slider");
  display.setCursor(25, 20);                        //Set the display cursor position
  display.print("Track Object");                          //Set the display text
  display.setCursor(25, 30);
  display.print("Loop");

  //Stores cursor vertical position to show selected item
  //Serial.println(menuPosition);

  if (menuPosition == 1)
    selected = 20;
  else if (menuPosition == 2)
    selected = 30;

  display.setCursor(14, selected);                   //Set the display cursor position
  display.print(">");

  display.setCursor(14, 50);                   //Set the display cursor position
  display.print("Scelta:");
  display.print(menuPosition);
  display.display();                           //Output the display text
}

void chooseSpeed() {

  while (flag == 6) {

    joystickReading();
    if (turnDetected) {
      turnDetected = false;
      if (rotationdirection == -1) {
        speedValue = speedValue + 30;
      }
      if (rotationdirection == 1) {
        speedValue = speedValue - 30;
        if (speedValue < 0)
          speedValue = 0;
      }
    }

    display.clearDisplay();
    display.setCursor(30, 0);
    display.print("Speed");

    motorSpeed = speedValue / 80;
    display.setCursor(5, 16);
    display.print(motorSpeed);
    display.print("mm/s");

    totalDistance = abs(XendPoint - XstartPoint);

    timeInSec = (totalDistance / speedValue);

    display.setCursor(35, 32);
    display.print("Time");
    display.setCursor(8, 48);
    display.print(timeInSec);
    display.println("s");
    display.display();

  }
}
// Include Library
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <AccelStepper.h>
#include <MultiStepper.h>

#include "costanti.h"
#include "funzioni.h"


void setup() {

  myStepper1.setMaxSpeed(3000);
  myStepper1.setSpeed(200);
  myStepper2.setMaxSpeed(3000);
  myStepper2.setSpeed(200);
  steppers.addStepper(myStepper1);
  steppers.addStepper(myStepper2);

  pinMode(limitSwitchLeft, INPUT);

  pinMode(pinButton, INPUT_PULLUP);
  attachInterrupt (digitalPinToInterrupt(pinButton), changeSection, RISING); // SW connected to D3

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.setTextSize(1.5);
  display.setTextColor(WHITE);
  display.clearDisplay();
  display.print("Camera Slider");
  display.display();
  delay(2000);


  Home();
}

void loop() {

  updateMainMenu();
  confirmed = false;                                //Both used to confirm button push to select mode
  menuPosition = 1;  //Encoder starts from 0, first menu option


  while (!confirmed)                                        //While the user has not confirmed the selection
  {
    joystickReading();
    if (rotationdirection == 1 and (menuPosition - 1) > 0) {
      menuPosition = menuPosition - 1;
    } else if (rotationdirection == -1 and (menuPosition + 1) < 3) {
      menuPosition = menuPosition + 1;
    }

    if (menuPosition != prevmenuPosition)                       //Update the display if the encoder position has changed
    {
      updateMainMenu();
      prevmenuPosition = menuPosition;
    }

  }

  if (menuPosition == 1) {
    runTrack();
  } else if (menuPosition == 2)
    loopTracking();

}

void runTrack() {
  flag = 0;

  if (flag == 0) {

    display.clearDisplay();

    display.setCursor(10, 28);
    display.print("Inizio");
    display.display();
    speedValue = 2000;
    delay(500);
    flag = flag + 1;
  }

  if (flag == 1) {
    //Move Xaxis start position
    display.clearDisplay();
    display.setCursor(10, 28);
    display.println("Set X In");
    display.display();

    while (flag == 1) {
      joystickReading();
      stepperposition(1);
    }
    XstartPoint = myStepper1.currentPosition();
  }
  if (flag == 2) {
    //Move Yaxis start position
    display.clearDisplay();
    display.setCursor(10, 28);
    display.println("Set Y In");
    display.display();

    while (flag == 2) {
      joystickReading();
      stepperposition(2);
    }

    myStepper2.setCurrentPosition(0);
    YstartPoint = myStepper2.currentPosition();
  }
  if (flag == 3) {
    //Move Xaxis end position

    display.clearDisplay();
    display.setCursor(10, 28);
    display.println("Set X Out");
    display.display();

    while (flag == 3) {
      joystickReading();
      stepperposition(1);
    }
    XendPoint = myStepper1.currentPosition();
  }
  if (flag == 4) {
    //Move Yaxis end position

    display.clearDisplay();
    display.setCursor(10, 28);
    display.println("Set Y Out");
    display.display();

    while (flag == 4) {
      joystickReading();
      stepperposition(2);
    }
    YendPoint = myStepper2.currentPosition();
    display.clearDisplay();

    //RItorna alla home facendo una specie di preview

    goToPosition[0] = XstartPoint;
    goToPosition[1] = YstartPoint;

    display.clearDisplay();
    display.setCursor(8, 28);
    display.println("Preview");
    display.display();

    myStepper1.setMaxSpeed(3000);

    steppers.moveTo(goToPosition);
    steppers.runSpeedToPosition();

  }
  if (flag == 5) {
    display.clearDisplay();
    display.setCursor(8, 28);
    display.println("Speed");
    display.display();
    delay(250);
    flag = flag + 1;

  }
  if (flag == 6)
  {
    display.clearDisplay();
    chooseSpeed();
  }

  if (flag == 7)
  {
    display.clearDisplay();
    display.setCursor(30, 27);
    display.println("Start");
    display.display();
    flag = flag + 1;
  }

  if (flag == 8) {
    // Ora faccio procedo con lo slider vero e proprio

    display.clearDisplay();
    display.setCursor(20, 27);
    display.print("Running");
    display.display();

    goToPosition[0] = XendPoint;
    goToPosition[1] = YendPoint;

    myStepper1.setMaxSpeed(speedValue);

    steppers.moveTo(goToPosition);
    steppers.runSpeedToPosition();

    flag = flag + 1;
  }
  if (flag == 9) {
    // Stampero sull' oled che ho finito.
    display.clearDisplay();
    display.setCursor(24, 26);
    display.println("Finish");
    display.display();
    flag = flag + 1;
  }
  if (flag == 10) {
    //Torno alla home
    display.clearDisplay();

    Home();
    flag = 0;
  }

}
void loopTracking() {
  flag = 0;



  if (flag == 0) {

    display.clearDisplay();
    display.print("Inizio");
    display.display();
    speedValue = 2000;
    flag = flag + 1;
  }
  if (flag == 1) {
    //Move Xaxis start position
    display.clearDisplay();
    display.setCursor(10, 28);
    display.println("Set X In");
    display.display();

    while (flag == 1) {
      joystickReading();
      stepperposition(1);
    }
    XstartPoint = myStepper1.currentPosition();
  }
  if (flag == 2) {
    //Move Yaxis start position
    display.clearDisplay();
    display.setCursor(10, 28);
    display.println("Set Y In");
    display.display();

    while (flag == 2) {
      joystickReading();
      stepperposition(2);
    }

    myStepper2.setCurrentPosition(0);
    YstartPoint = myStepper2.currentPosition();
  }
  if (flag == 3) {
    //Move Xaxis end position

    display.clearDisplay();
    display.setCursor(10, 28);
    display.println("Set X Out");
    display.display();

    while (flag == 3) {
      joystickReading();
      stepperposition(1);
    }
    XendPoint = myStepper1.currentPosition();
  }
  if (flag == 4) {
    //Move Yaxis end position

    display.clearDisplay();
    display.setCursor(10, 28);
    display.println("Set Y Out");
    display.display();

    while (flag == 4) {
      joystickReading();
      stepperposition(2);
    }
    YendPoint = myStepper2.currentPosition();
    display.clearDisplay();

    //RItorna alla home facendo una specie di preview

    goToPosition[0] = XstartPoint;
    goToPosition[1] = YstartPoint;

    display.clearDisplay();
    display.setCursor(8, 28);
    display.println("Preview");
    display.display();

    myStepper1.setMaxSpeed(3000);

    steppers.moveTo(goToPosition);
    steppers.runSpeedToPosition();

  }
  if (flag == 5) {
    display.clearDisplay();
    display.setCursor(8, 28);
    display.println("Speed");
    display.display();
    flag = flag + 1;

  }
  if (flag == 6)
  {
    display.clearDisplay();
    chooseSpeed();
  }

  if (flag == 7)
  {
    display.clearDisplay();
    display.setCursor(30, 27);
    display.println("Start");
    display.display();
    flag = flag + 1;
  }

  if (flag == 8) {
    // Ora faccio procedo con lo slider vero e proprio
    confirmed = false;
    display.clearDisplay();
    display.setCursor(20, 27);
    display.print("Running Loop");
    display.setCursor(20, 37);
    display.print(confirmed);
    display.display();

    while (!confirmed) {
      goToPosition[0] = XendPoint;
      goToPosition[1] = YendPoint;

      myStepper1.setMaxSpeed(speedValue);
      steppers.moveTo(goToPosition);
      steppers.runSpeedToPosition();

      goToPosition[0] = XstartPoint;
      goToPosition[1] = YstartPoint;

      steppers.moveTo(goToPosition);
      steppers.runSpeedToPosition();
    }
  }
  if (flag == 9) {
    // Stampero sull' oled che ho finito.
    display.clearDisplay();
    display.setCursor(24, 26);
    display.println("Finish");
    display.display();
    flag = flag + 1;
  }
  if (flag == 10) {
    //Torno alla home
    display.clearDisplay();

    Home();
    flag = 0;
  }
}

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

Post all the code. The issue is likely not where you are looking…

I have not checked in details but there is potential for infinite loop. How do you exit the likes of


while (flag == 1) {
      joystickReading();
      stepperposition(1);
    }