Server.write function Blocking data From Ethernet Client

i am Running Stepper Motor using the Timer.on each interupt,one Signal is transfred to the Steper Controller pin.
so using this Technique i am moving the Motor with number of Steps and also making the M1_Status pin High.
when Step is Done.then i Stop the Timer and make the M1_Status pin Low.

Now EnableInterrupt function is interruped Beause the M1_Status pin is going low from High.
then i call the function (M1_Stop_Interrupt) and i use the Server.write("") function.

the problem

if say move the motor 100 steps, He will move and after that i also get response from server that Motoro is Stop.

if i send the command again then some time wortk and some time not.
this is very strange that some time he will work for 10 times and some time may be only 2 times
After that if i send the command from client to the Arduino(Server) then he is receiving nothing and he is behaving like he block the system.

the main issue is Server.Write function.
if i comment the Server.Write then every things work perfectly.

i create the client in vb.net.

here is the code for arduino

#include <EnableInterrupt.h>
#include <SPI.h>
#include <Ethernet.h>


#define M1_Status 10


byte directionPin = 13;
byte stepPin = 2;
boolean M1_Run = false;
long M1_Counter = -1;

String inputString = "";
int xIndexNum = 0;
String cmd;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);
boolean xRead  = false;

void M1_Stop_Interrupt() 
                          {server.write("Motor is Stop");
                          }
                          
 void setup(){

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(M1_Status,OUTPUT);
  digitalWrite(directionPin,LOW);
  digitalWrite(M1_Status,LOW);
  
  // this is used to interrupt the function when PIN goes High To low 
  enableInterrupt(M1_Status, M1_Stop_Interrupt, FALLING);
  
  
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP()); 
  

   //-------------------------------------------------------------------------------------------------
   //                          This Section Used the Timer 1                                         / 
   //                  Timer is Used to Provide the Steps Plus to the Motor 1                        /
  //--------------------------------------------------------------------------------------------------
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
          //Actual Timer Value 
  TCNT1  = 0;//initialize counter value to 0
        // set compare match register for 2hz increments
        //this is output compare Register
  OCR1A = 6;// output compare Register = (16*10^6) / (2000*1024) - 1 (must be <65536)
        // turn on CTC mode
       //Means Clear Timer on Compare Match
  TCCR1B |= (1 << WGM12);
       // Set CS12 and CS10 bits for 1024 prescaler
  //TCCR1B |= (1 << CS12) | (1 << CS10);
  TCCR1B |= (1 << CS12);
  TCCR1B |= (1 << CS10);
       // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  
  //-------------------------------------------------------------------------------------------------------
  
  
  
   Serial.begin(9600);
   Serial.println("Lets Start");
}//end setup


ISR(TIMER1_COMPA_vect){//timer1 interrupt 2Hz toggles pin 13 (LED)
 if (M1_Run == true &&  M1_Counter == -1) 
 {digitalWrite(stepPin,LOW);
  digitalWrite(stepPin,HIGH);
  digitalWrite(M1_Status,HIGH);}
 else if (M1_Run == false &&  M1_Counter >= 0) 
 {digitalWrite(stepPin,LOW);
  digitalWrite(stepPin,HIGH);
  digitalWrite(M1_Status,HIGH);
  M1_Counter -= 1;}
  else 
  {TCCR1B &= ~(1 << CS12);
  TCCR1B &= ~(1 << CS10);
    Serial.println("X");
  digitalWrite(M1_Status,LOW);}
  }


void loop(){
  xRead = false; 
  EthernetClient client = server.available();
   if (client) 
             {if (client.available()) 
                                     { 
                                       while (xRead== false)
                                                     {
                                                       char c = client.read(); 
                                                      if (c == '\n')
                                                                   {xRead = true;}
                                                      else 
                                                         {inputString += c;}
                                                      }
                                     }
                              Serial.println(inputString);
                             if (inputString.substring(0, 7) == "M1Steps")    
                                                 {inputString.remove(0,8);
                                                        cli();
                                                        xIndexNum = inputString.indexOf(',');
                                                        cmd = inputString.substring(0,xIndexNum);
                                                        M1_Run = false;
                                                        M1_Counter= cmd.toInt();
                                                        if(M1_Counter < 0)
                                                          {digitalWrite(directionPin,HIGH);
                                                          M1_Counter = (M1_Counter *(-1));}
                                                          else
                                                          {digitalWrite(directionPin,LOW);}
                                                          sei();
                                                          TCCR1B |= (1 << CS12);
                                                          TCCR1B |= (1 << CS10);
                                                        inputString = "";
                                                        cmd = "";}
                

                } // End if the Client is Available

   
} //End of the Loop

That seems a very complicated way to produce step pulses for a stepper motor. How many steps per second do you require?

I can't figure from your description what is the overall intention of the program. Please explain what you are trying to achieve rather than how you think it should be achieved.

...R

thank you for your reply
i need to run motor with 2000 Steps per Seconds.

Basically motor Need 6400 Steps to complete the 1 rotation.

i have three motor they are fully independent and i can stop any motor at any point.
even that i can also change the direction of one particular motor without disturbing the
other motors.

I achieve the goal.now i here i got one problem when i am trying to write the server.Write function.
then he block the full system.

    Serial.println("X");

In an ISR? Dream on.

void M1_Stop_Interrupt()
                          {server.write("Motor is Stop");
                          }

In an interrupt service routine? Dream on.

atifbuitms:
thank you for your reply
i need to run motor with 2000 Steps per Seconds.

...SNIP....

i have three motor they are fully independent

Does this mean that you actually need 6000 steps per second?

To the best of my knowledge this Simple Stepper Code will produce 2000 steps per second and more. The second example is the better one as it does not use delay().

As @PaulS has said, you should not use print() or write() functions inside an ISR.

If you are prepared to write an explanation of how your code works at the moment I will look at it again - but I am not going to spend time trying to figure out Timers and Interrupts from your code.

Also, your code is horribly formatted. Many people don't bother with spaces, but you have far too many and they are not consistently arranged. Use the AutoFormat tool and re-post your code.

There is a possibility that your program is too busy making pulses to have time to do other things.

...R