Faster Code ?

I am trying to control 74595 Shift Registers via Ethernet..
I am sending commands with my visual basic application that sends bytes, Ex. 255000255000255000255000 to control 8 shifts
After buffering the command in a String I Split this code and convert it to integer, then use ShiftOut method .

After testing my code I can see that the code execution time on Arduino takes 7 - 9 milliseconds.
I need to make this faster as possible.

Here's my code :

// EZE Control v0.1 
// By Kim
// include needed libs
#include <SPI.h>
#include <MyEthernet.h> // Thanks for David A. Mellis & Tom Igoe for this lib ..

// setup network
byte DefaultMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte DefaultIP[] = {10,0,0,151 };

Server server(23);
Client client = 0; 
boolean DebugMode = false;
String AccessPassword = "1234";
byte clientID;
boolean logged = false;
boolean connectFlag = 0;

// PIN Configrations
int connectedLedPin = 2;
int ErrorLedPin = 3;
int latchPin = 9;
int clockPin = 8;
int dataPin = 7;

void setup() {
  if (DebugMode){Serial.begin(9600);}
  pinMode(connectedLedPin, OUTPUT);
  pinMode(ErrorLedPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(10, OUTPUT);
  startServer();

}

void startServer() {
  Ethernet.begin(DefaultMac, DefaultIP);
  server.begin();
}

void loop()
{
  if (server.available() && !connectFlag) {
    connectFlag = 1;
    client = server.available();
    clientID = client.id();
  } 
  else if (server.available()) {
    Client badclient = server.available();
    byte tempid = badclient.id();
    if ( tempid != clientID) {
      badclient.println("E1"); // only one connection
      badclient.stop();
    }
  }
  if (client.connected() && client.available()) getReceivedData();
  if (connectFlag && !client.connected()) {
    client.stop();
    logged = false;
    connectFlag = 0;
    digitalWrite(connectedLedPin,LOW);
  }
  if (!logged){
    digitalWrite(connectedLedPin,HIGH);
    delay(250);
    digitalWrite(connectedLedPin,LOW);
    delay(250);
    if (DebugMode){Serial.println("Waiting");}
  }
}
void getReceivedData(){
  unsigned long time = millis();
  Client client = server.available(); 
  String Command;
  char c;
  int isEnd = 0;
  while (client.available() > 0 && isEnd == 0 ){
    c = client.read();
    if (c != 13){
      Command.concat(c);
    }else{
      isEnd = 1;
    }
  }
  if (DebugMode){
    Serial.print("Command: [");
    Serial.print(Command);
    Serial.print("] Lenth : ");
    Serial.println(Command.length());
  }
  switch (logged) {
  case true:
    // got a new command
    digitalWrite(latchPin, LOW);
    // shift out the bits:
    for (int i = 0;i < 8;i++){
      shiftOut(dataPin, clockPin, MSBFIRST,Command.substring(i*3,(i*3)+3).toInt());
    } 
    digitalWrite(latchPin, HIGH);
    client.println(millis() - time);
    break;        
  case false:
    if (Command.equals(AccessPassword)) {
      logged = true;
      client.println("R"); // Ready
      digitalWrite(connectedLedPin,HIGH);
    }
    else{
      client.println("E2"); // Wrong Access
      client.stop();
    }
    break;
  }
  Command = ""; // empty buffer
}

Thank all, Appreciate it ( :

After testing my code I can see that the code execution time on Arduino takes 7 - 9 milliseconds.
I need to make this faster as possible.

Sending data as a string and storing that in an String and extracting substrings from that and converting those to ints is not really compatible with speed.

What part of the code is taking 7 to 9 milliseconds? The whole receive, parse, shiftOut cycle?

How long is this calculation taking?

Command.substring(i3,(i3)+3).toInt()

That time is with turned off too?

if (DebugMode){Serial.begin(9600);}

For the actual shifting out you could use this instead:

digitalWrite(latchPin, LOW);
// shift out the bits:
SPI.transfer (Command.substring(i3,(i3)+3).toInt()); // connect to arduino pins SCK & MOSI instead of 7 & 8
digitalWrite(latchPin, HIGH);

PaulS:

After testing my code I can see that the code execution time on Arduino takes 7 - 9 milliseconds.
I need to make this faster as possible.

Sending data as a string and storing that in an String and extracting substrings from that and converting those to ints is not really compatible with speed.

What part of the code is taking 7 to 9 milliseconds? The whole receive, parse, shiftOut cycle?

The whole receive, I start counting by the first line (unsigned long time = millis()), And print the millis back to my visual basic app. (client.println(millis() - time))..
If there's a tweek for all code not just the receive function that give me more speed.. this will be great..
I used String function coz I am a new to the Arduino and I didn't understand the char & array functions yet ...

CrossRoads:
How long is this calculation taking?

Command.substring(i3,(i3)+3).toInt()

That time is with turned off too?

if (DebugMode){Serial.begin(9600);}

For the actual shifting out you could use this instead:

digitalWrite(latchPin, LOW);
// shift out the bits:
SPI.transfer (Command.substring(i3,(i3)+3).toInt()); // connect to arduino pins SCK & MOSI instead of 7 & 8
digitalWrite(latchPin, HIGH);

I already attached a Ethernet Shield to arduino, This shield used the SPI pins .. right ?

I already attached a Ethernet Shield to arduino, This shield used the SPI pins .. right ?

Yes, but so does the SD card on the shield. You can have multiple devices using SPI. You just need a different select pin to activate each one.

I tested the code without the shiftOut lines, I remarked them.. i got less 1 - 2 millis ..
The delay is not from the shiftOut process, Its from buffering & substring the commands .. is there's a better way ?

Thank you

Its from buffering & substring the commands .. is there's a better way ?

You really need to separate the time into three parts - collecting the data, parsing the data, and using the data. You've already isolated the third part, using the data, and determined that that part is not the slow bit. Now, the question is whether collecting the data or parsing the data is the slow part. I'm betting on the collection, via ethernet as strings, as the slow part, rather than the parsing of the data.

There's not going to be much you can do about that. The ethernet shield is not a particularly fast way of transferring data.

Ok, I used the Ethernet coz i want to control my device little away from the computer.. lets say about 100+ meters, The maximum USB cable length for full speed is 5 meters (From http://www.usb.org/about/faq/ans5 ), So I used Ethernet.. is there's a better way than Ethernet ?

Thank you

is there's a better way than Ethernet ?

There's nothing wrong with ethernet. It just isn't particularly fast. XBees and binary data would be faster.