Limit switch input value not read in loop() while multistepper is running

Hi, this is a MY COMPLETE CODE in which my two motors need to align themself to a fixed limit switcher.
Inside the do...while the absolute positioning is increased step by step each iteration (i am using a tmc2209 driverwith a 1/8 of a step resolution) and then stopped by the if when the limit switcher is pressed.

#include <stdio.h>
#include <Stepper.h>
#include <ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int32.h>
#include <rosserial_arduino/Test.h>
#include <AccelStepper.h>
#include <MultiStepper.h>

ros::NodeHandle nh;
using rosserial_arduino::Test;
//rosrun rosserial_python serial_node.py /dev/ttyUSB0
long positions[2]; // Array of desired stepper absolute positions
const int dirPinR = 5;
const int stepPinR = 6;
const int enPinR = 7;
const int dirPinL = 11;
const int stepPinL = 10;
const int enPinL = 2;
const int sensor0degL = 9;
const int sensor0degR = 4;
int homedL = 0;
int homedR = 0;
int pos=0; //actual position
int destination = 999;
AccelStepper myRStepper(AccelStepper::DRIVER, stepPinR, dirPinR);
AccelStepper myLStepper(AccelStepper::DRIVER, stepPinL, dirPinL);
MultiStepper motors;

void callback(const Test::Request & req, Test::Response & res){
  String temp = req.input;
  nh.logerror(req.input);
  destination = atoi(req.input);
}
ros::ServiceServer<Test::Request, Test::Response> server("Test",&callback);
void setup() {
  Serial.begin(57600);
  myRStepper.setMaxSpeed(300);
  myLStepper.setMaxSpeed(300);
  pinMode(stepPinR, OUTPUT);
  pinMode(dirPinR, OUTPUT);
  pinMode(enPinR, OUTPUT);
  pinMode(sensor0degR, INPUT);
  pinMode(stepPinL, OUTPUT);
  pinMode(dirPinL, OUTPUT);
  pinMode(enPinL, OUTPUT);
  pinMode(sensor0degL, INPUT_PULLUP);
  pinMode(sensor0degR, INPUT_PULLUP);
  nh.initNode();
  nh.advertiseService(server);
  motors.addStepper(myRStepper);
  motors.addStepper(myLStepper);
  positions[0] = 0;
  positions[1] = 0;
  homedL = 0;
  homedR = 0;
  digitalWrite(enPinR, LOW);//activate R motor
  digitalWrite(enPinL, LOW);//activate L motor
}
void loop() {
  delay(500);
  if (destination == 1)/*GOAL UP*/{
    nh.loginfo("dentro up");
    if (pos == 0){/*currently zero*/
      positions[0] = positions[0]+96;
      positions[1] = positions[1]+96;
      motors.moveTo(positions);
      nh.loginfo("REQUESTED UP positions");
      motors.runSpeedToPosition();
    }
    else if (pos == 2){/*currently down*/
    positions[0] = positions[0]+192;
    positions[1] = positions[1]+192;
    motors.moveTo(positions);
    nh.loginfo("REQUESTED UP positions");
    motors.runSpeedToPosition();
    } 
  pos = destination;/*update the actual postion*/
  destination = 9;/*reset the request*/
  }
  else if (destination == 0)/*GOAL ZERO*/{
    nh.loginfo("dentro zero");
    if (pos == 2){ /*currently down*/
    nh.loginfo("REQUESTED ZERO positions");
    positions[0] = positions[0]+96;
    positions[1] = positions[1]+96;
    motors.moveTo(positions);
    motors.runSpeedToPosition();
    }
    else if (pos == 1){/*currently up*/
    nh.loginfo("REQUESTED ZERO positions");
    positions[0] = positions[0]-96;
    positions[1] = positions[1]-96;
    motors.moveTo(positions);
    motors.runSpeedToPosition();
    }
    pos = destination;/*update the actual postion*/
    destination = 9;/*reset the request*/
  }
  else if (destination == 2)/*GOAL DOWN*/{
    if (pos == 0){/*currently zero*/
    nh.loginfo("REQUESTED DOWN positions");
    positions[0] = positions[0]-96;
    positions[1] = positions[1]-96;
    motors.moveTo(positions);
    motors.runSpeedToPosition();
    }
    else if (pos == 1){/*currently up*/
    nh.loginfo("REQUESTED DOWN positions");
    positions[0] = positions[0]-192;
    positions[1] = positions[1]-192;
    motors.moveTo(positions);
    motors.runSpeedToPosition();
    }
    pos = destination;/*update the actual postion*/
    destination = 9;/*reset the request*/
  }

  else if (destination == 3)/*HOMING*/{
    nh.loginfo("STARTING HOMING PROCEDURE");
    homedL = 0;
    homedR = 0;
    do {
      if (homedR == 0) positions[0]=positions[0] + 8;
      if (homedL == 0) positions[1]=positions[1] + 8;

      if(digitalRead(sensor0degR) == LOW)/*R limit switcher pressed*/{
        nh.loginfo("R HOMED");
        digitalWrite(enPinR, HIGH);//deactivate R motor
        myRStepper.stop();
        homedR = 1;
      }
      if (digitalRead(sensor0degL) == LOW)/*L limit switcher pressed*/{
        nh.loginfo("L HOMED");
        digitalWrite(enPinL, HIGH);//deactivate L motor
        myLStepper.stop();
        homedL = 1;
      }
    motors.moveTo(positions);
    motors.run();
    } while (homedR == 0 || homedL == 0);
    pos = 0; /*the homing procedure was completed*/
    destination = 9;/*reset the request*/
    nh.loginfo("HOMING PROCEDURE COMPLETED");
    digitalWrite(enPinR, LOW);//activate R motor
    digitalWrite(enPinL, LOW);//activate L motor
  }
  else if (destination == 999) nh.logwarn ("HOMING REQUIRED");
  else if (pos == destination) nh.loginfo("THE ACTUAL POSITION WAS REQUESTED");/*No movement required*/
  else if (destination == 9) nh.loginfo("CORRECT MOVEMENT, READY FOR NEW");
  else nh.loginfo("WRONG REQUEST");
  nh.spinOnce();
}

The problem is that the changing in value of the limit switchers are not always seen by my arduino nano approx. once every dozen time(in setup the pins are declared with pinMode () correctly) while if I remove the

motors.moveTo(positions);
motors.run();

commands, the changing of states are visible by my code.
I am a novice to .ino coding and i would appreciate any help.
Thanks in advance.

I suspect you need to do individual steps, one at a time with you own stepping code. That code you have will run past your sensors.

I think that I am already doing it, by giving 8 my motor do a single step do the resolution of the driver

Hi @macchiolinoo

welcome to the arduino-forum.
Well done to post code as a code-section.

Your comment is contradictionary to what the code does

without showing what you have really coded nobody can check it.

You should post your complete sketch. A complete sketch is a much better basis for analysing than a snippet.

Hello Stefan, thank you very much.
My apologies it was a mistake I forgot to edit out, please ignore the comment (I fixed it now in the original post).
I did not send the entire sketch since it was quite long, but if it is required, and it will help to solve my issues, I will do it first thing in the morning tomorrow.
As I mentioned, without the two lines which are running the motor the code works just fine.
What my function should do is the following:
start and move two motor which could be into different position and exit when both detect the signal from the sensor which could arrive in any order and also not together (i.e. if the right detects the sensor, the right should stop while the left should continue to move until the left signal would reach the board).
I would appreciate immensely any tips or advice since I need to complete asap this project

What pin mode are you using for the switches? If you are not using INPUT_PULLUP, have you provided a pull up or pull down resistor? for each switch?

Are the switches normally open or normally closed?

a7

Hi, no I declared them as Input only and without the PU/PD resistors. I saldered the 5V from my nano board to the C pin of the switch and the NC ( this explains why I am checking for the Low value on the digitalRead) to a digital input pin

Without some kind of pull, the input will be floating when the switches are open.

Floating inputs are a Bad Thing, as they can read HIGH or LOW randomly.

I think this might explain the behaviour you observe. A quick way to confirm my diagnosis would be to immediately print the value you read from each switch as you read it, and see if it follows your activity on the switch or… does not.

You can still use INPUT_PULLUP if you have no suitable resistors, just wire the switch contacts between the input pin and ground, then read HIGH when the NC contacts are open, and see LOW when the NC contacts are closed.

HTH

a7

And this means: If you configure an IO-pin as input this input is so sensitive that even a 2 inches short wire will act as an antenna that catches up electromagnetic noise that is always present and make the IO-pin switch its state randomly between LOW and hIGH.

Depending on the amout of electromagnetic noise this could be once every few minutes or 50 times per second.

A pulldown or pullup-resistor reduces this sensitivity and enables reliable acting of the IO-pin. Using the microcontroller internal pullup-resistor has the advantage of no extra resistor required because you use the internal resistor build into the microcontroller.

Using a pullup-resistor inverts the logic levels of the IO-pin.
Pullup means

  • if switch is opened the IO-pin is pulled to +Vcc and sees a HIGH
  • if switch is closed the IO-pin is pulled to GND and sees a LOW

The picture shows what happends on the IO-pin if the IO-pin is configured as

pinMode(mySwitch, INPUT_PULLUP);

To make a code easier to understand you can use as many variables and constants as you want.

Here I recommend to use constants that say directly the state of the switch
as mentioned above

  • if switch is opened the IO-pin is pulled to +Vcc and sees a HIGH
const byte open = HIGH;

.
.

  • if switch is closed the IO-pin is pulled to GND and sees a LOW
const byte closed = LOW;

with these constants you can write if-conditions that directly describe what is happening

if (digitalRead(myLimitSwitch) == closed) {
  Serial.print("limit-switch found!");
}


if (digitalRead(myLimitSwitch) == open) {
  Serial.print("run motor");
}

self-explaining names.

1 Like

Dear @alto777 and @StefanL38
I uploaded the complete code into the topic (check for the edit version). In the meanwhile I tried it with the new input mode but it wont solve the issue.
I also tried a simple code

const byte open = HIGH;
const byte closed = LOW;
void setup() {
  Serial.begin(750);  
// put your setup code here, to run once:
  pinMode(4,INPUT_PULLUP);

  pinMode(9,INPUT_PULLUP);
}
void loop() {
  if (digitalRead(4) == open)
  Serial.print("4");
  if (digitalRead(9) == closed)
  Serial.print("9");
}

And the closing is not read, while if i used the pinMode(-,INPUT) i obtained much better results, all the closing are read but with different closing time(not really an issue for my program since I am interested on the rising edge of the signal).
If you can give me any tips, or if you spot any other mistake I would appreciate them. Thanks a lot for all your help so far

Please provide a schematic of your wiring.

really????

N.B. when homing your steppers, you must do that individually for each stepper, and not use the multistepper functionality to move the steppers.

I have thought about this too. It is better practice to do it one after the other.
But it should work. If one motor did not reach the target yet multistepper creates pulses for this particular motor

Below is a testcode to test your wiring.
This code uses debug-macros that work in this way

"dbgc" dbg as short for debug "c" short for change
This means only in case the value of the used variable / function had REALLY changed a message gets printed to the serial monitor

// 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 *

const byte open = HIGH;
const byte closed = LOW;

const byte sensor0degL = 9;
const byte sensor0degR = 4;

const byte OnBoard_LED = 13;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
  pinMode(sensor0degL, INPUT_PULLUP);
  pinMode(sensor0degR, INPUT_PULLUP);
}


void loop() {
  BlinkHeartBeatLED(OnBoard_LED,250);

  dbgc("01",digitalRead(sensor0degL) );
  dbgc("02",digitalRead(sensor0degR) );  
}



// helper-functions
void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );  
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}



void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}

WOKWI-Simulation

You don't have to do it one after the other - you can do it in parallel. But not with multiStepper functions. Multistepper is meant for a coordinated move of two steppers to reach a target at the same time. But that's the opposite what is needed with homing. Homing is very individual for each stepper.

Not really. Your driver creates microsteps, and every motors.run(); call only creates one (micro)step at most during one do-while cycle. But you set the target ahead for 8 microsteps every cycle. That does not make sense.
But as already mentioned, you should not use multiStepper calls for homing anyway. ( But the misunderstanding with microsteps is the same when using AccelStepper calls )

basically what your code is doing is:

 do {
    increase the value of variables 
    positions[0] by 8
    positions[1] by 8

    // this increasing is done REGARDLESS of 
    // if a SINGLE step has been really executed
    // such a while-loop repeats 10000 times per second
    // this means before your stepper-motor has done 
    // only a SINGLE microstep 
    // variable positions[0] and variable positions[1]
    // has counted uo to value 1000
    // after a few microsteps has counted up to 3000
    // increasing, increasing, increasing very very fast

    // so what is happening is that you tell multistepper to rotate to
    // step no 1000, 3000, 5000, 7000, 9000, 11000 .......
    motors.moveTo(positions);
    motors.run();
    } while (homedR == 0 || homedL == 0);


// referenceing works this way    

// do a SINGLE step or if you like to 8 microsteps
// check limit-switch 
// if limit-switch changed to "pressed" stop
// if limit-switch has not changed to "pressed" do another single-step

// but not with increasing a target-position 
// simply be DIRECTLY creating a 
// SINGLE-step-pulse

@MicroBahner do the MobaTools include a reference example yet?
Yes they do.
I made a version with more comments and complete in english
This is the code

/*  example for controlling a bipolar steppermotor
    with 4 buttons and a potentiometer (for adjusting the speed)
    in this example three different parts of the MobaTools are used
    MotoStepper, MoToButtons, MoToTimeBase

    each of the 4 button makes the steppermotor drive to a pre-defined position
    in case the button is pressed

    a double-press (double-click) of a button 1/2 activates ENDLESS rotation
    double-click of button 1 => endless rotation forward
    double-click of button 2 => endless rotation backward
    double-click of button 3 => stop
    looong press of button 4 => initiate reference

    in this example referenceing is coded as a blocking function
    this means if referencing is active the code can do nothing else but the referencing
    until referencing has finished successfully

    The internal LED is switched on if reference-switch is active
*/


#define MAX8BUTTONS // spart Speicher, da nur 4 myButtons benötigt werden
#include <MobaTools.h>

const int STEPS_REVOLUTION = 800;
//create stepper-object ( 800 steps / revolution = 1/4 Microstep )
MoToStepper myStepper( STEPS_REVOLUTION, STEPDIR );
const byte dirPin = 5;
const byte stepPin = 6;
const byte enaPin = 7;

// create buttons ( the code expects that the buttons are wired IO-Pin---button----GND )
enum { button1, button2, button3, button4 } ; // assign the button-names the index-numbers 0,1,2,3
const byte buttonPins[] = {A1, A2, A3, A4 }; // must be defined as byte that the calculation works
const byte buttonNr = sizeof(buttonPins);
const long buttonPos[] = { 1600, 3200, 6400, 7600 };
MoToButtons myButtons( buttonPins, buttonNr, 20, 500 );

// reference / limit-switch
const byte refPin = A5;         // IO-pin for limit-switch
const byte atRefpoint = LOW;   // if limit-switch is triggered the logic level is LOW

MoToTimebase speedIntervall;      // time-interval for reading speed potentiometer

const byte potiPin = A0;        //potentiometer for speed
int vspeed = 0;                 //Stepperspeed in rev/min*10 
int oldSpeed = 0;               // nescessary for detecting speed-changes

void toRefPoint() {
  // drive stepper to reference-point then set position to 0  
  Serial.println("drive fast towards reference-point");
  // driving fast to reference ...
  if ( digitalRead( refPin ) != atRefpoint ) {
    // ... onyl in case steppermotor is not already at reference-point
    myStepper.setSpeedSteps( 20000, 100 );
    myStepper.rotate(-1);
    while ( digitalRead( refPin ) != atRefpoint );
  }
  Serial.println("limit-switch triggered STOP");
  digitalWrite( LED_BUILTIN, digitalRead( refPin ) );
  // limit-switch reached stop
  myStepper.rotate(0);
  while ( myStepper.moving() );     // wait until deccelerating has finished;

  // drive slowly without acceleration until limit-switch changes state
  myStepper.setSpeedSteps( 1000 );
  myStepper.setRampLen(0); // de-activate acceleration through value zero

  // remark myStepper.rotate( 1 ) must be called only a SINGLE time
  // the MobaTools will create a infinite train of step-pulses
  // in the background until you execute function myStepper.rotate(0) or myStepper.stop()
  Serial.println("driving away from limit-switch until limit-switch is NOT triggered anymore");
  myStepper.rotate( 1 );

  // wait for limit-switch to change state
  while ( digitalRead( refPin ) == atRefpoint );

  digitalWrite( LED_BUILTIN, digitalRead( refPin ) );
  Serial.println("limit-switch unpressed => reference reached");

  myStepper.rotate(0);

  while (myStepper.moving() );
  myStepper.setZero();
  myStepper.setSpeed( 200 );

  // re-actiavte acceleration
  myStepper.setRampLen( 100 ); // number of steps for used for acceleration 100 Steps
  oldSpeed = 0;  // set to zero to make sure value of potentiometer is used
  Serial.println("end of referencing");
}

void setup() {
  Serial.begin(115200);
  while (!Serial);
  myStepper.attach( stepPin, dirPin );
  myStepper.attachEnable( enaPin, 10, LOW ); // activate Enable Pin ( LOW=active )
  myStepper.setSpeed( 200 );
  vspeed = 200;
  myStepper.setRampLen( 100 ); // number of steps for used for acceleration 100 Steps
  speedIntervall.setBasetime( 100 );  // set timer-intervall to 100 millisecons
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(refPin, INPUT_PULLUP );
  toRefPoint();
}

void loop() {
  myButtons.processButtons(); // reading in myButtons and check for pressed / released, double-press, long-press

  digitalWrite( LED_BUILTIN, digitalRead( refPin ) );

  // read in speed from potentiometer once every 100 milliseconds
  if ( speedIntervall.tick() ) {
    // if 100 milliseconds have passed by ( see function-call  speedIntervall.setBasetime(); in setup()
    vspeed = map((analogRead(potiPin)), 0, 1023, 20, 1800);  // map values to 2 ... 180 rev/Min
    //min speed =2 and max speed =180 rpm
    if ( abs( oldSpeed - vspeed ) > 5 ) {
      myStepper.setSpeed( vspeed );
      oldSpeed = vspeed;
    }
  }

  // initiate referencing
  if ( myButtons.longPress( button4 ) ) toRefPoint();

  //
  for ( byte tastIx = 0; tastIx < buttonNr; tastIx++ ) {
    // check myButtons for Click/double-click
    byte clickTyp = myButtons.clicked(tastIx);
    if ( clickTyp == SINGLECLICK ) {
      //button single-press
      Serial.print("rotate to Pos ");
      Serial.println( buttonPos[tastIx] );
      myStepper.writeSteps(buttonPos[tastIx]);
    }
    else if ( clickTyp == DOUBLECLICK ) {
      // button double-click detected
      switch ( tastIx ) {

        case button1:
          Serial.println("double-press of button 1 rotate endless forward");
          myStepper.rotate(1);
          break;

        case button2:
          Serial.println("double-press of button 2 rotate endless backwar");
          myStepper.rotate(-1);
          break;

        case button3:
          Serial.println("double-press of button 3 STOP!");
          myStepper.rotate(0);
          break;

        default:
          break;
      }
    }
  }
}

and this is the WOKWI-link

Thanks a lot for your advice.
I implemented this version and I saw an improvement of the results with almost every closing being recognized.
See below the modifications:

#include <stdio.h>
#include <Stepper.h>
#include <ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int32.h>
#include <rosserial_arduino/Test.h>
#include <AccelStepper.h>
#include <MultiStepper.h>

ros::NodeHandle nh;
using rosserial_arduino::Test;
//rosrun rosserial_python serial_node.py /dev/ttyUSB0
long positions[2]; // Array of desired stepper absolute positions
const int dirPinR = 5;
const int stepPinR = 6;
const int enPinR = 7;
const int dirPinL = 11;
const int stepPinL = 10;
const int enPinL = 2;
const int sensor0degL = 9;
const int sensor0degR = 4;
int homedL = 0;
int homedR = 0;
int pos=0; //actual position
int destination = 999;
AccelStepper myRStepper(AccelStepper::DRIVER, stepPinR, dirPinR);
AccelStepper myLStepper(AccelStepper::DRIVER, stepPinL, dirPinL);
MultiStepper motors;

void callback(const Test::Request & req, Test::Response & res){
  String temp = req.input;
  nh.logerror(req.input);
  destination = atoi(req.input);
}
ros::ServiceServer<Test::Request, Test::Response> server("Test",&callback);
void setup() {
  Serial.begin(57600);
  myRStepper.setMaxSpeed(300);
  myLStepper.setMaxSpeed(300);
  pinMode(stepPinR, OUTPUT);
  pinMode(dirPinR, OUTPUT);
  pinMode(enPinR, OUTPUT);
  pinMode(sensor0degR, INPUT);
  pinMode(stepPinL, OUTPUT);
  pinMode(dirPinL, OUTPUT);
  pinMode(enPinL, OUTPUT);
  pinMode(sensor0degL, INPUT);
  pinMode(sensor0degR, INPUT);
  nh.initNode();
  nh.advertiseService(server);
  motors.addStepper(myRStepper);
  motors.addStepper(myLStepper);
  positions[0] = 0;
  positions[1] = 0;
  homedL = 0;
  homedR = 0;
  digitalWrite(enPinR, LOW);//activate R motor
  digitalWrite(enPinL, LOW);//activate L motor
}
void loop() {
  delay(500);
  if (destination == 1)/*GOAL UP*/{
    nh.loginfo("dentro up");
    if (pos == 0){/*currently zero*/
      positions[0] = positions[0]+96;
      positions[1] = positions[1]+96;
      motors.moveTo(positions);
      nh.loginfo("REQUESTED UP positions");
      motors.runSpeedToPosition();
    }
    else if (pos == 2){/*currently down*/
    positions[0] = positions[0]+192;
    positions[1] = positions[1]+192;
    motors.moveTo(positions);
    nh.loginfo("REQUESTED UP positions");
    motors.runSpeedToPosition();
    } 
  pos = destination;/*update the actual postion*/
  destination = 9;/*reset the request*/
  }
  else if (destination == 0)/*GOAL ZERO*/{
    nh.loginfo("dentro zero");
    if (pos == 2){ /*currently down*/
    nh.loginfo("REQUESTED ZERO positions");
    positions[0] = positions[0]+96;
    positions[1] = positions[1]+96;
    motors.moveTo(positions);
    motors.runSpeedToPosition();
    }
    else if (pos == 1){/*currently up*/
    nh.loginfo("REQUESTED ZERO positions");
    positions[0] = positions[0]-96;
    positions[1] = positions[1]-96;
    motors.moveTo(positions);
    motors.runSpeedToPosition();
    }
    pos = destination;/*update the actual postion*/
    destination = 9;/*reset the request*/
  }
  else if (destination == 2)/*GOAL DOWN*/{
    if (pos == 0){/*currently zero*/
    nh.loginfo("REQUESTED DOWN positions");
    positions[0] = positions[0]-96;
    positions[1] = positions[1]-96;
    motors.moveTo(positions);
    motors.runSpeedToPosition();
    }
    else if (pos == 1){/*currently up*/
    nh.loginfo("REQUESTED DOWN positions");
    positions[0] = positions[0]-192;
    positions[1] = positions[1]-192;
    motors.moveTo(positions);
    motors.runSpeedToPosition();
    }
    pos = destination;/*update the actual postion*/
    destination = 9;/*reset the request*/
  }

  else if (destination == 3)/*HOMING*/{
    nh.loginfo("STARTING HOMING PROCEDURE");
    homedL = 0;
    homedR = 0;
    do {
      if (homedR == 0) positions[0]=positions[0] + 1;
      if (homedL == 0) positions[1]=positions[1] + 1;

      if(digitalRead(sensor0degR) == LOW)/*R limit switcher pressed*/{
        nh.loginfo("R HOMED");
        digitalWrite(enPinR, HIGH);//deactivate R motor
        myRStepper.stop();
        homedR = 1;
      }
      if (digitalRead(sensor0degL) == LOW)/*L limit switcher pressed*/{
        nh.loginfo("L HOMED");
        digitalWrite(enPinL, HIGH);//deactivate L motor
        myLStepper.stop();
        homedL = 1;
      }
    myRStepper.move(positions[0]);
    myRStepper.run();
    myLStepper.move(positions[1]);
    myLStepper.run();

    } while (homedR == 0 || homedL == 0);
    pos = 0; /*the homing procedure was completed*/
    destination = 9;/*reset the request*/
    nh.loginfo("HOMING PROCEDURE COMPLETED");
    digitalWrite(enPinR, LOW);//activate R motor
    digitalWrite(enPinL, LOW);//activate L motor
  }
  else if (destination == 999) nh.logwarn ("HOMING REQUIRED");
  else if (pos == destination) nh.loginfo("THE ACTUAL POSITION WAS REQUESTED");/*No movement required*/
  else if (destination == 9) nh.loginfo("CORRECT MOVEMENT, READY FOR NEW");
  else nh.loginfo("WRONG REQUEST");
  nh.spinOnce();
}

Thanks again!

dear @macchiolinoo

you should always post complete sketches.
It will be easier to see how the modification is related to the rest
other users can take the code as a whole as their on base

'almost' is not a good result ....
There are still some problems.

move() is a relative move. if yo use move, you should simply use e.g.
move(1);
And you should not call the move and run commands at all when the limit switch of that stepper is reached.

Your new code-version does not solve the main problem of increasing the target-position with each iteration of your do-while-loop.

Your code does
run to pos 1
run to pos 2
run to pos 3
....
run to pos 100
first step-pulse created
run to pos 101
run to pos 102
run to pos 103
....
run to pos 200
second step-pulse created
run to pos 201
run to pos 202
run to pos 203
....
third step-pulse created
which means you are at pos 3 while your command to run to position did
RUN AWAY to already 300
and will have RUN AWAY to a 100000 until the limit switch is pressed

I highly recommend that you change to the MobaTools-library because the MobaTools-library gives you much more freedom to execute different code than AccelStepper / Multistepper

If you describe what your overall project is
it can be seen if parallel and synchronised movement of your two stepper-motors is a must or not

This code works only because your limit-switch is closed long enough that the stop-condition is still met

I think adding the MultStepper functionality to MoToStepper should not be too complicated. Since it only works without acceleration/deceleration, only the different speeds for the steppers need to be calculated. With only limited changes to Multistepper it could work with MoToStepper.
I am currently considering adding a corresponding class to MobaTools.