choose the values of the variables

Hello, I'm new in programming and Arduino.
I use a Step motor, an Arduino MEGA 2560 and a SMCI47-S-2 Nanotec.
I would like to know how I can choose different values, like in my program,
I can choose right 'r' or left 'l' and now I would like to choose the stepNumber.
I would like to write in serial for exemple " 1000,r" to say 1000 step and direction right.

this is my code :

int pinBouton;
int stateButton;
int pinLedG = 10; //Led Green
int pinLedR = 11; //Led Red
int pin31 = 31; //Start/Reset
int pin32 = 32; //Step Motor
int pin36 = 36; //Clock
int e=0; //RETURN TO RIGHT AFTER HIT BUTTON
int i=0; //FOR RIGHT
int j=0; //FOR LEFT
int serialData = 0;// data and serial data are just variables to hold information.
int data = 0;
int displacement = 10 ; // mm //CHOOSE YOUR DISPLACEMENT TO THE LEFT IN millimeter
int stepNumberToRight = 1000; // Step number To Right
int stepNumberToLeft = 3000; //Step number To Left
bool endswitch=false;
int incomingByte;

// the setup routine runs once when you press reset:
void setup(){
{
Serial.begin(9600); //start talking with the computer. 9600 just tells the rate.
Serial.println("*** Program start ");
Serial.print("Name of this program is :");
Serial.println(" sketch_mar06a");
Serial.println("
TAPE on 'r' going to the RIGHT or TAPE on 'l' going to the LEFT ***");

// initialize the digital pin as an output.
pinMode(pinBouton, INPUT); //mode lecture pour le bouton
pinMode(pinLedG, OUTPUT); //mode écriture pour led1
pinMode(pinLedR, OUTPUT); //mode écriture pour led2
pinMode(pin31, OUTPUT); //mode ecriture pour pin31
pinMode(pin32, OUTPUT); //mode ecriture pour pin32
pinMode(pin36, OUTPUT); //mode ecriture pour pin36

attachInterrupt(digitalPinToInterrupt(3),stopmotor,FALLING);
}

}

void loop()
{
Serial.flush();
while (Serial.available() >0) //Serial.available will tell the board to look at the information being sent to it through the serial port.
{
incomingByte = Serial.read();
//Step motor going to RIGHT (r=114 en ASCII)
{if(incomingByte == 'r')
digitalWrite(pin31,LOW); //pin31 0V START_RESET
digitalWrite(pin31,LOW); //pin31 0V START_RESET
digitalWrite(pin32, HIGH); // RIGHT
digitalWrite(pinLedG,HIGH);

while (j<stepNumberToRight){
digitalWrite(pin31,HIGH); //pin31 +5V FRONT MONTANT
digitalWrite(pin36,LOW); //pin36 0V
delay(2);
digitalWrite(pin36,HIGH); //pin36 +5V
delay(2);
j=j+1;
//Serial.println("BOUCLE 4");

}
digitalWrite(pin31,LOW); //pin31 +5V START_RESET
Serial.println("BOUCLE 1 RIGHT");}

digitalWrite(pin31,HIGH);
delay(20); // temps d'attente si supérieur a 20 ça ne fonctionne pas
digitalWrite(pin31,LOW);

//Step motor going to LEFT (l=108 en ASCII)
{if(incomingByte == 'l')

{
digitalWrite(pin31,LOW); //pin31 0V START_RESET
digitalWrite(pin31,HIGH); //pin31 +5V START_RESET
digitalWrite(pin32, LOW); // LEFT
digitalWrite(pinLedG,HIGH);

while (i<stepNumberToLeft){
digitalWrite(pin31,HIGH); //pin31 +5V FRONT MONTANT
digitalWrite(pin36,LOW); //pin36 0V
delay(2);
digitalWrite(pin36,HIGH); //pin36 +5V
delay(2);
i=i+1;
//Serial.println("BOUCLE 3");
}

digitalWrite(pin31,LOW); //pin31 0V START_RESET
Serial.println("BOUCLE 0 LEFT");
}
}
//digitalWrite(pin31,HIGH);
//delay(20); // temps d'attente si supérieur a 20 ça ne fonctionne pas
//digitalWrite(pin31,LOW);
}
}

void stopmotor(){
i=stepNumberToLeft;
digitalWrite(pin31,HIGH);
delay(20); // temps d'attente si supérieur a 20 ça ne fonctionne pas
digitalWrite(pin31,LOW);
endswitch=true;

digitalWrite(pin32,HIGH); //RIGHT
while(e<500){ /// temps de retour de charriot vers la droite
digitalWrite(pin36,LOW); //pin36 0V
delay(2);
digitalWrite(pin36,HIGH); //pin36 +5V
delay(2);
e=e+1;
Serial.println("BOUCLE 5"); //BOUCLE 5
}
Serial.println("TOUCH");
Serial.println("BOUCLE 8"); //BOUCLE 8

}

If you want, you can answer in French or English :wink:
Thanks

I would like to write in serial for exemple " 1000,r" to say 1000 step and direction right.

So, what is the problem? If you have some option other than nothing selected as the line ending in the Serial Monitor application, the Serial Monitor application will supply an end-of packet marker.

You find and read Robin2's Serial Input Basics - Updated thread, and read and store the data until the end of packet marker arrives. When it does, you parse and use the stored data and erase the stored data.

Ok thank, I'm going to read this topic

I would to know "how I can choose the value to stepNumber myself in the part of Serial monitor" ?

valleevi:
I would to know "how I can choose the value to stepNumber myself in the part of Serial monitor" ?

Read the topic suggested to see how to receive and parse the data from the Serial monitor

Links ...

Serial Input Basics

Stepper Motor Basics
Simple Stepper Code

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

...R

Ok sorry,

I few modified my code, I try to erase information doesn't important.

I would like to write myself in the serial scope the number of stepNumber, because now I need to open the code and changed the number.
I don't arrive to use a other "Serial.read" like in line 50 when I choose Right or Left.
Do you think it's possible to have something like this "1000,r".

Configuration of my Serial Monitor " No line ending, 9600 baud"

There is surely lot of mistake in my code sorry. :frowning:

thank you for the time you give to answer my questions :slight_smile:

int pinBouton;
int stateButton;
int pinLedG = 10;   //Led Green
int pinLedR = 11;   //Led Red
int pin31 = 31;   //Start/Reset
int pin32 = 32; //Step Motor
int pin36 = 36;   //Clock
int e=0;      //RETURN TO RIGHT AFTER HIT BUTTON
int i=0;    //FOR RIGHT
int j=0;    //FOR LEFT
int serialData = 0;// data and serial data are just variables to hold information.
int data = 0;
int displacement = 10 ;   // mm        //CHOOSE YOUR DISPLACEMENT TO THE LEFT IN millimeter
int stepNumberToRight = 1000;  // Step number  To Right
int stepNumberToLeft = 1000;    //Step number To Left
bool endswitch=false;
int incomingByte;



// the setup routine runs once when you press reset:
void setup(){
{
Serial.begin(9600); //start talking with the computer. 9600 just tells the rate.
Serial.println("*** Program start ***");
Serial.print("Name of this program is :");
Serial.println(" sketch_mar06d");
Serial.println("*** TAPE on 'r' going to the RIGHT or TAPE on 'l' going to the LEFT ***");

 // initialize the digital pin as an output.
  pinMode(pinBouton, INPUT); //mode lecture pour le bouton
  pinMode(pinLedG, OUTPUT); //mode écriture pour led1
  pinMode(pinLedR, OUTPUT); //mode écriture pour led2
  pinMode(pin31, OUTPUT); //mode ecriture pour pin31
  pinMode(pin32, OUTPUT); //mode ecriture pour pin32
  pinMode(pin36, OUTPUT); //mode ecriture pour pin36

    attachInterrupt(digitalPinToInterrupt(3),stopmotor,FALLING);
}

}

void loop()
{
  Serial.flush();
      while (Serial.available() >0) //Serial.available will tell the board to look at the information being sent to it through the serial port.
 {
  incomingByte = Serial.read();
  //Step motor going to RIGHT (r=114 en ASCII)
  {if(incomingByte == 'r') 
                
                  {digitalWrite(pin32, HIGH); // RIGHT
                            
                                   while (j<stepNumberToRight){
                                  digitalWrite(pin31,HIGH);    //pin31 +5V FRONT MONTANT
                                  digitalWrite(pin36,LOW); //pin36 0V
                                  delay(2);
                                  digitalWrite(pin36,HIGH); //pin36 +5V
                                  delay(2);
                                   j=j+1;
                                  //Serial.println("BOUCLE 4");
                            
                        }

                  Serial.println("LOOP 1 RIGHT");}
  }

      digitalWrite(pin31,HIGH);
      delay(20);    // temps d'attente si supérieur a 20 ça ne fonctionne pas
      digitalWrite(pin31,LOW);

  //Step motor going to LEFT (l=108 en ASCII)
  {if(incomingByte == 'l') 

                {

                 digitalWrite(pin32, LOW); // LEFT

                                    while (i<stepNumberToLeft){
                                    digitalWrite(pin31,HIGH);    //pin31 +5V FRONT MONTANT
                                    digitalWrite(pin36,LOW); //pin36 0V
                                    delay(2);
                                    digitalWrite(pin36,HIGH); //pin36 +5V
                                    delay(2);
                                    i=i+1;
                                    Serial.println("BOUCLE 3");
                                    }
                                        
                 Serial.println("LOOP 0 LEFT");
                 }    
      }
 }
}

void stopmotor(){
  i=stepNumberToLeft;
      digitalWrite(pin31,HIGH);
      delay(20);    // temps d'attente si supérieur a 20 ça ne fonctionne pas
      digitalWrite(pin31,LOW);
                           endswitch=true;
 
                           digitalWrite(pin32,HIGH);   //RIGHT
                           while(e<500){  /// temps de retour de charriot vers la droite 
                           digitalWrite(pin36,LOW); //pin36 0V
                           delay(2);
                           digitalWrite(pin36,HIGH); //pin36 +5V
                           delay(2);
                           e=e+1;
                           //Serial.println("LOOP 5");         //BOUCLE 5   
                            }
 Serial.println("TOUCH");
 //Serial.println("LOOP 8");  //BOUCLE 8
  }

There is surely lot of mistake in my code sorry.

Before you try to extend the code, fix them.

There are useless { and } in setup(). Get rid of them.

Put EVERY { on a line BY ITSELF.
Put EVERY { on a line BY ITSELF.
Use Tools + Auto Format to fix the piss-poor indenting.

Do NOT use one letter global variable names.

void stopmotor(){
  i=stepNumberToLeft;
      digitalWrite(pin31,HIGH);
      delay(20);    //

You can NOT use delay() in an interrupt service routine.

That ISR needs a complete rewrite. It should stop the motor IMMEDIATELY.

Do you think it's possible to have something like this "1000,r".

How many times do you need to be told yes? Are you that insecure?

Configuration of my Serial Monitor " No line ending, 9600 baud"

Why?

/*  Step_Motor_
*/
int pinBouton;
int stateButton;
int pinLedG = 10;   //Led Green
int pinLedR = 11;   //Led Red
int pin31 = 31;   //Start/Reset
int pin32 = 32; //Step Motor
int pin36 = 36;   //Clock
int e = 0;    //RETURN TO RIGHT AFTER HIT BUTTON
int i = 0;  //FOR RIGHT
int j = 0;  //FOR LEFT
int serialData = 0;// data and serial data are just variables to hold information.
int data = 0;
int displacement = 10 ;   // mm        //CHOOSE YOUR DISPLACEMENT TO THE LEFT IN millimeter
int stepNumberToRight = 1000;  // Step number  To Right
int stepNumberToLeft = 1000;    //Step number To Left
bool endswitch = false;
int incomingByte;



// the setup routine runs once when you press reset:
void setup()
{
  Serial.begin(9600); //start talking with the computer. 9600 just tells the rate.
  Serial.println("*** Program start ***");
  Serial.print("Name of this program is :");
  Serial.println(" sketch_mar06d");
  Serial.println("*** TAPE on 'r' going to the RIGHT or TAPE on 'l' going to the LEFT ***");

  // initialize the digital pin as an output.
  pinMode(pinBouton, INPUT); //mode lecture pour le bouton
  pinMode(pinLedG, OUTPUT); //mode écriture pour led1
  pinMode(pinLedR, OUTPUT); //mode écriture pour led2
  pinMode(pin31, OUTPUT); //mode ecriture pour pin31
  pinMode(pin32, OUTPUT); //mode ecriture pour pin32
  pinMode(pin36, OUTPUT); //mode ecriture pour pin36

  attachInterrupt(digitalPinToInterrupt(3), stopmotor, FALLING);
}

void loop()
{
  Serial.flush();
  while (Serial.available() > 0) //Serial.available will tell the board to look at the information being sent to it through the serial port.
  {
    incomingByte = Serial.read();
    //Step motor going to RIGHT (r=114 en ASCII)
    { if (incomingByte == 'r')
      { digitalWrite(pin32, HIGH); // RIGHT
        while (j < stepNumberToRight) {
          digitalWrite(pin31, HIGH);   //pin31 +5V FRONT MONTANT
          digitalWrite(pin36, LOW); //pin36 0V
          delay(2);
          digitalWrite(pin36, HIGH); //pin36 +5V
          delay(2);
          j = j + 1;
          //Serial.println("BOUCLE 4");
        }
        Serial.println("LOOP 1 RIGHT");
      }
    }

    //digitalWrite(pin31, HIGH);
    //digitalWrite(pin31, LOW);

    //Step motor going to LEFT (l=108 en ASCII)
    { if (incomingByte == 'l')
      {
        digitalWrite(pin32, LOW); // LEFT
        while (i < stepNumberToLeft) {
          digitalWrite(pin31, HIGH);   //pin31 +5V FRONT MONTANT
          digitalWrite(pin36, LOW); //pin36 0V
          delay(2);
          digitalWrite(pin36, HIGH); //pin36 +5V
          delay(2);
          i = i + 1;
          //Serial.println("BOUCLE 3");
        }
        digitalWrite(pin31, LOW); //pin31 0V START_RESET
        Serial.println("LOOP 0 LEFT");
      }
    }
  }
}

void stopmotor()
{
  digitalWrite(pin31, HIGH);
  digitalWrite(pin31, LOW);
  endswitch = true;

  digitalWrite(pin32, HIGH);  //RIGHT
  while (e < 500) { /// temps de retour de charriot vers la droite
    digitalWrite(pin36, LOW); //pin36 0V
    delay(2);
    digitalWrite(pin36, HIGH); //pin36 +5V
    delay(2);
    e = e + 1;
    //Serial.println("LOOP 5");         //BOUCLE 5
  }
  Serial.println("TOUCH");
  //Serial.println("LOOP 8");  //BOUCLE 8
}

Now my code look like this.

I read your link above but I try and I don't arrive to chose my stepNumber.

valleevi:
I read your link above but I try and I don't arrive to chose my stepNumber.

Make the code for receiving serial data separate from the code for controlling the motor. Then you can test both parts separately.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

Your code in loop() might look like this

void loop() {
   recvWithEndMarker();
   parseData();
   moveMotor();
}

The code in the moveMotor() function would get its data from a variable that is updated from the Serial data.

...R

ok, some things like that? It is better to compartmentalize?
but now I can not write an information, when I write 'r' for example nothing happens I do not even see that I go through my loop "void moveMotor" because I do not see "void moveMotor RIGHT "on my screen.

/*  Step_Motor_
*/
int pinBouton;
int stateButton;
int pin31 = 31;   //Start/Reset
int pin32 = 32; //Step Motor
int pin36 = 36;   //Clock
int e = 0;    //RETURN TO RIGHT AFTER HIT BUTTON
int j = 0;  //
int serialData = 0;// data and serial data are just variables to hold information.
int data = 0;
int displacement = 10 ;   // mm        //CHOOSE YOUR DISPLACEMENT TO THE LEFT IN millimeter
int number;  // Step number
int incomingByte;

bool endswitch = false;
boolean newData = false;

const byte numChars = 32;
char receivedChars[numChars];   //an array to store the received data
char tempChars[numChars];   //temporary array for use when parsing

//variables to hold the parsed data
char messageFromPC[numChars] = {0};     //Direction in which the motor will go
int integerFromPC = 0;                  //Number of step the motor will do


// the setup routine runs once when you press reset:
void setup()
{
  Serial.begin(9600); //start talking with the computer. 9600 just tells the rate.
  Serial.println("*** Program start ***");
  Serial.print("Name of this program is :");
  Serial.println(" sketch_mar07d");
  Serial.println("***Start end finish your order with '<' and'>' ***");
  Serial.println("*** TAPE on 'r' going to the RIGHT or TAPE on 'l' going to the LEFT ***");

  // initialize the digital pin as an output.
  pinMode(pinBouton, INPUT); //mode lecture pour le bouton
  pinMode(pin31, OUTPUT); //mode ecriture pour pin31
  pinMode(pin32, OUTPUT); //mode ecriture pour pin32
  pinMode(pin36, OUTPUT); //mode ecriture pour pin36

  attachInterrupt(digitalPinToInterrupt(3), stopmotor, FALLING);
}
//============

void loop()
{
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();
    stepNumber();
    moveMotor();
    newData = false;
  }
}

//============

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}
//============

void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

}

//============

void showParsedData() {
  Serial.print("Message ");
  Serial.println(messageFromPC);
  Serial.print("Integer ");
  Serial.println(integerFromPC);
}

//============

void moveMotor()
{
  //Step motor going to RIGHT (r=114 en ASCII)
  if (messageFromPC == 'r')
  { digitalWrite(pin32, HIGH); // RIGHT
    Serial.println("void moveMotor RIGHT");
  }

  digitalWrite(pin31, HIGH);
  digitalWrite(pin31, LOW);

  //Step motor going to LEFT (l=108 en ASCII)
  if (messageFromPC == 'l')
  {
    digitalWrite(pin32, LOW); // LEFT
    digitalWrite(pin31, LOW); //pin31 0V START_RESET
    Serial.println("void moveMotor LEFT");
  }
}

//============

void stepNumber()
{
  while (j < number) {
    digitalWrite(pin31, HIGH);   //pin31 +5V FRONT MONTANT
    digitalWrite(pin36, LOW); //pin36 0V
    delay(2);
    digitalWrite(pin36, HIGH); //pin36 +5V
    delay(2);
    j = j + 1;
    Serial.println("void stepNumber");
  }
}
//============

void stopmotor()
{
  digitalWrite(pin31, HIGH);
  digitalWrite(pin31, LOW);
  endswitch = true;

  digitalWrite(pin32, HIGH);  //RIGHT
  while (e < 500) { /// temps de retour de charriot vers la droite
    digitalWrite(pin36, LOW); //pin36 0V
    delay(2);
    digitalWrite(pin36, HIGH); //pin36 +5V
    delay(2);
    e = e + 1;
    //Serial.println("LOOP 5");         //BOUCLE 5
  }
  Serial.println("TOUCH");
  //Serial.println("LOOP 8");  //BOUCLE 8
}

I assume you don't have compiler warnings set to 'all' in preferences. I do and if I compile your code I get this:

/Users/martin/Documents/Arduino/test/test.ino: In function 'void moveMotor()':
/Users/martin/Documents/Arduino/test/test.ino:123:24: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   if (messageFromPC == 'r')

You could try:

if (messageFromPC[0] == 'r')

Sorry, no I don't have this parameter. And what is different when you have compiler to 'all' ?

with this modification, now I see I go in void moveMotor thanks to my Serial.println"void moveMotor RIGHT".

this would like to say, when I write '<r,1000>' my program went to this loop.
But nothing happed, my loop for number of step doen't functionning for the moment.

Thank you very much :slight_smile:
It's functioning.

Now I doesn't understand why but, I already need to do <r,0> and after <l,1000> for example.
If I try to say in first going to Left my step motor go to right :/.

I know, I need to send information in pin 31, it's "Start/Reset" function, it's a rising front.

My code look like this now :

/*  Step_Motor_
*/
int pinBouton;
int stateButton;
int pin31 = 31;   //Start/Reset
int pin32 = 32; //Step Motor
int pin36 = 36;   //Clock
int e = 0;    //RETURN TO RIGHT AFTER HIT BUTTON
int j = 0;  //
int serialData = 0;// data and serial data are just variables to hold information.

bool endswitch = false;
boolean newData = false;

const byte numChars = 32;
char receivedChars[numChars];   //an array to store the received data
char tempChars[numChars];   //temporary array for use when parsing

//variables to hold the parsed data
char messageFromPC[numChars] = {0};     //Direction in which the motor will go
int integerFromPC = 0;                  //Number of step the motor will do


// the setup routine runs once when you press reset:
void setup(){
  Serial.begin(9600); //start talking with the computer. 9600 just tells the rate.
  Serial.println("*** Program start ***");
  Serial.print("Name of this program is :");
  Serial.println(" sketch_mar07g");
  Serial.println("***Start end finish your order with '<' and '>' ***");
  Serial.println("*** TAPE on 'r' going to the RIGHT or TAPE on 'l' going to the LEFT ***");
  Serial.println();

  // initialize the digital pin as an output.
  pinMode(pinBouton, INPUT); //mode lecture pour le bouton
  pinMode(pin31, OUTPUT); //mode ecriture pour pin31
  pinMode(pin32, OUTPUT); //mode ecriture pour pin32
  pinMode(pin36, OUTPUT); //mode ecriture pour pin36

  attachInterrupt(digitalPinToInterrupt(3), stopmotor, FALLING);
}

//============

void loop(){
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();
    stepNumber();
    moveMotor();
    newData = false;
  }
}

//============

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}
//============

void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

}

//============

void showParsedData() {
  Serial.print(" Direction ");
  Serial.println(messageFromPC);
  Serial.print(" Numbers of step ");
  Serial.println(integerFromPC);
}

//============

void moveMotor(){
  //Step motor going to RIGHT (r=114 en ASCII)
  if (messageFromPC[0] == 'r')
  { digitalWrite(pin32, HIGH); // RIGHT
    //Serial.println("void moveMotor RIGHT");
  }

  digitalWrite(pin31, HIGH);
  digitalWrite(pin31, LOW);

  //Step motor going to LEFT (l=108 en ASCII)
  if (messageFromPC[0] == 'l')
  {
    digitalWrite(pin32, LOW); // LEFT
    digitalWrite(pin31, LOW); //pin31 0V START_RESET
    //Serial.println("voimoveMotor LEFT");
  }
}

//============

void stepNumber(){
  
  while (j < integerFromPC) {
    digitalWrite(pin31, HIGH);   //pin31 +5V FRONT MONTANT
    digitalWrite(pin36, LOW); //pin36 0V
    delay(2);
    digitalWrite(pin36, HIGH); //pin36 +5V
    delay(2);
    j = j + 1;
    //Serial.println("void stepNumber");
  }
}

//============

void stopmotor(){
  digitalWrite(pin31, HIGH);
  digitalWrite(pin31, LOW);
  endswitch = true;

  digitalWrite(pin32, HIGH);  //RIGHT
  while (e < 500) { /// temps de retour de charriot vers la droite
    digitalWrite(pin36, LOW); //pin36 0V
    delay(2);
    digitalWrite(pin36, HIGH); //pin36 +5V
    delay(2);
    e = e + 1;
    //Serial.println("LOOP 5");         //BOUCLE 5
  }
  Serial.println("TOUCH");
}

Hello, I don't understand why when my program want in attachInterrupt doesn't stop,
for example when I say <l,5000> and my carriage hit the button at 2000, the step motor did 500 step to the right but after it finished the last 3000 step, and I hit my structure. :confused:

/*  Step_Motor_
*/
int pinBouton;
int stateButton;
int pin31 = 31;   //Start/Reset
int pin32 = 32; //Step Motor
int pin36 = 36;   //Clock
int e = 0;    //RETURN TO RIGHT AFTER HIT BUTTON
int j = 0;  //
int serialData = 0;// data and serial data are just variables to hold information.

bool endswitch = false;
boolean newData = false;

const byte numChars = 32;
char receivedChars[numChars];   //an array to store the received data
char tempChars[numChars];   //temporary array for use when parsing

//variables to hold the parsed data
char messageFromPC[numChars] = {0};     //Direction in which the motor will go
int integerFromPC = 0;                  //Number of step the motor will do


// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600); //start talking with the computer. 9600 just tells the rate.
  Serial.println("*** Program start ***");
  Serial.print("Name of this program is :");
  Serial.println(" sketch_mar07g");
  Serial.println("***Start end finish your order with '<' and '>' ***");
  Serial.println("*** TAPE on 'r' going to the RIGHT or TAPE on 'l' going to the LEFT ***");
  Serial.println();
  Serial.println("***Enter your order like this ' < 'r' or 'l' ',' 'number of step you want' ****");
  Serial.println("Example : <r,1000> ");
  Serial.println();

  // initialize the digital pin as an output.
  pinMode(pinBouton, INPUT); //mode lecture pour le bouton
  pinMode(pin31, OUTPUT); //mode ecriture pour pin31
  pinMode(pin32, OUTPUT); //mode ecriture pour pin32
  pinMode(pin36, OUTPUT); //mode ecriture pour pin36

  attachInterrupt(digitalPinToInterrupt(3), stopmotor, FALLING);
}

//============

void loop() {
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();
    stepNumber();
    moveMotor();
    showParsedData();
    newData = false;
  }
}

//============

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}
//============

void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

}

//============

void showParsedData() {
  Serial.print(" Direction : 'r' for Right and 'l' for Left : ");
  Serial.println(messageFromPC);
  Serial.print(" Numbers of step is : ");
  Serial.println(integerFromPC);
}

//============

void moveMotor() {
  //Step motor going to RIGHT (r=114 en ASCII)
  if (messageFromPC[0] == 'r')
  { digitalWrite(pin32, HIGH); // RIGHT
    //Serial.println("void moveMotor RIGHT");
  }

  digitalWrite(pin31, HIGH);
  digitalWrite(pin31, LOW);

  //Step motor going to LEFT (l=108 en ASCII)
  if (messageFromPC[0] == 'l')
  { digitalWrite(pin32, LOW); // LEFT
    digitalWrite(pin31, LOW); //pin31 0V START_RESET
    //Serial.println("voimoveMotor LEFT");
  }
}

//============

void stepNumber() {

  while (j < integerFromPC) {
    digitalWrite(pin31, HIGH);   //pin31 +5V FRONT MONTANT
    digitalWrite(pin36, LOW); //pin36 0V
    delay(2);
    digitalWrite(pin36, HIGH); //pin36 +5V
    delay(2);
    j = j + 1;
    //Serial.println("void stepNumber");
  }
}

//============

void stopmotor() {
  digitalWrite(pin31, HIGH);
  digitalWrite(pin31, LOW);
  endswitch = true;

  digitalWrite(pin32, HIGH);  //RIGHT
  while (e < 500) { /// temps de retour de charriot vers la droite
    digitalWrite(pin36, LOW); //pin36 0V
    delay(2);
    digitalWrite(pin36, HIGH); //pin36 +5V
    delay(2);
    e = e + 1;
    //Serial.println("LOOP 5");         //BOUCLE 5
  }
  Serial.println("TOUCH");
}

You've got delay AND serial prints in an interrupt service routine.

Don't do either.

You think there is an other solution to do the same action ?
I just want if my carriage hit the button during the step motor going on left , it change immediately of direction "go to right" during 500 step and after stop. It's finish.

pinBouton is never assigned a value.

Pin 3 is never configured.

my pin 3 is in same connection of my ground