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