Arduino Mega w/ Ethernet Shield sometimes Takes 7 min to become pingable?

Hey, I have an Arduino Mega with an Ethernet Shield. When I connect it with an ethernet wire directly to a PC or Mac, and ping it, it can sometimes take up to 8 minutes (normally less) for it to return pings. Until that point the computer can't find anything at that ip address. After I wait, and it finally becomes pingable, I can send it packets, and turn the arduino off and upload new code, and the arduino will be pingable within seconds again.

Why is it taking so long initially?

I'm running a modified version of the code at Ethernet - Arduino Reference

Any recommendations would be great.

If you post the code you are using, I'll give it a shot. Please post the version of IDE you are using. I have V0022 and V1.0.
My shield takes a couple seconds to get active, but that is it.
You are using a crossover cable when connecting to the computers directly, correct?

Sorry for the massive code block, but I'm on the way to work, and Im on a phone at the moment. This is the exact code we are using. If you think something I could change would make it ping able faster, please recommend it to me:

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};  //The media access control address for the ethernet shield
byte ip[] = {10, 0, 1, 2}; //the IP address for the ethernet shield

#define PORT            (1337)  //The port the ethernet recieves packets on
#define DEBUG           true    //Output running data to computer serial terminal

//Packet Definitions
#define PACKET_movement              (1)

//Avoid pins 5 and 6 for PWM as they have a known issues with PWM.
//While using the ethernet shield, be wary of pins 10 and 4:
////Note that because the W5100 (ethernet shield) and SD card share the SPI bus, only one can be active 
////at a time. If you are using both peripherals in your program, this should be taken care of by the 
////corresponding libraries. If you're not using one of the peripherals in your program, however, 
////you'll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and 
////write a high to it. For the W5100, set digital pin 10 as a high output.
#define ETHERNET_SHIELD_DISABLE      (10) //DONT TOUCH THIS PIN!!!! Set this pin HIGH to disable the ethernet shield.
#define SD_CARD_DISABLE              (4)  //Set this pin HIGH to disable the SD card
#define PIN_MOTOR_1_PWM_LEFT         (2)
#define PIN_MOTOR_1_PWM_RIGHT        (3)
#define PIN_MOTOR_1_ENABLE           (5)
#define PIN_MOTOR_2_PWM_LEFT         (8)
#define PIN_MOTOR_2_PWM_RIGHT        (9)
#define PIN_MOTOR_2_ENABLE           (7)
#define PIN_MOTOR_3_PWM_LEFT         (11)
#define PIN_MOTOR_3_PWM_RIGHT        (12)
#define PIN_MOTOR_3_ENABLE           (13)

//Movement Variables
#define MAX_MOVEMENT_VALUE           (127)     //Max value for X, Y, or Rotation sent from client computer
#define MAX_X_PWM                    (60)
#define MAX_Y_PWM                    (255 / 4) //Max Speed is 0, and not moving is 255
#define MAX_ROTATION_PWM             (30)

#define motor1Bias                   (1)
#define motor2Bias                   (1)
#define motor3Bias                   (1)

EthernetServer server = EthernetServer(PORT);

void setup() {
  Ethernet.begin(mac, ip);  //Initialize the ethernet device
  server.begin();           //Start listening for clients on the Ethernet

  if (DEBUG) {
    Serial.begin(9600); //Start serial communication to computer
    Serial.println("Serial has begun");
  }

  //Setup Pins
//  pinMode(SD_CARD_DISABLE, OUTPUT);    //Must set this PIN high since we aren't using the SD Card and it
//  digitalWrite(SD_CARD_DISABLE, HIGH); //shares a serial port with the Ethernet Shield.
  pinMode(PIN_MOTOR_1_PWM_LEFT, OUTPUT);
  pinMode(PIN_MOTOR_1_PWM_RIGHT, OUTPUT);
  pinMode(PIN_MOTOR_1_ENABLE, OUTPUT);
  pinMode(PIN_MOTOR_2_PWM_LEFT, OUTPUT);
  pinMode(PIN_MOTOR_2_PWM_RIGHT, OUTPUT);
  pinMode(PIN_MOTOR_2_ENABLE, OUTPUT);
  pinMode(PIN_MOTOR_3_PWM_LEFT, OUTPUT);
  pinMode(PIN_MOTOR_3_PWM_RIGHT, OUTPUT);
  pinMode(PIN_MOTOR_3_ENABLE, OUTPUT);

  //Enable all H-Bridges
  digitalWrite(PIN_MOTOR_1_ENABLE, LOW);
  digitalWrite(PIN_MOTOR_2_ENABLE, LOW);
  digitalWrite(PIN_MOTOR_3_ENABLE, LOW);

  //Outputing a solid 1 to the H-Bridges turns wheel off
  analogWrite(PIN_MOTOR_1_PWM_LEFT, 255);
  analogWrite(PIN_MOTOR_1_PWM_RIGHT, 255);
  analogWrite(PIN_MOTOR_2_PWM_LEFT, 255);
  analogWrite(PIN_MOTOR_2_PWM_RIGHT, 255);
  analogWrite(PIN_MOTOR_3_PWM_LEFT, 255);
  analogWrite(PIN_MOTOR_3_PWM_RIGHT, 255);
  
  //################################################################
  //Just for testing purposes. Normally should be commented out.
  //################################################################
  digitalWrite(PIN_MOTOR_1_ENABLE, HIGH);
  digitalWrite(PIN_MOTOR_2_ENABLE, HIGH);
  digitalWrite(PIN_MOTOR_3_ENABLE, HIGH);
  analogWrite(PIN_MOTOR_1_PWM_LEFT, 200);
  analogWrite(PIN_MOTOR_1_PWM_RIGHT, 255);
  analogWrite(PIN_MOTOR_2_PWM_LEFT, 200);
  analogWrite(PIN_MOTOR_2_PWM_RIGHT, 255);
  analogWrite(PIN_MOTOR_3_PWM_LEFT, 200);
  analogWrite(PIN_MOTOR_3_PWM_RIGHT, 255);
  //################################################################
}

void loop() {
  EthernetClient client = server.available();
  if (client) { //Is there a client connected?
    if (client.connected()) { //If we have a connection
      if (client.available()) {

        int packetType = client.read();
        handlePacket(client, packetType);

      }
    }
  } 
}

void handlePacket(EthernetClient client, int packetType) {
  switch (packetType) {
  case PACKET_movement:
    HandlePacket_movement(client);
    break;
  }
}

void HandlePacket_movement(EthernetClient client) {
  //prog_char is a signed 8 bit int.
  //we are recieving a values from -127 to +127
  prog_char xFromServer = client.read();
  prog_char yFromServer = client.read();
  prog_char rotationFromServer = client.read();
  
  //Make values relative to set MAX values so as not to overheat H-Bridges
  int X = (MAX_X_PWM * xFromServer) / MAX_MOVEMENT_VALUE;
  int Y = (MAX_Y_PWM * yFromServer) / MAX_MOVEMENT_VALUE;
  int rotation = (MAX_ROTATION_PWM * rotationFromServer) / MAX_MOVEMENT_VALUE;
  
  int totalMotor1 = 0;
  int totalMotor2 = 0;
  int totalMotor3 = 0;

  //X Calculations
  totalMotor1 += X * 1.2;
  totalMotor2 += -X;
  totalMotor3 += -X;

  //Y Calculations
  //Motors 2 and 3 are used for going in the Y direction
  totalMotor2 += Y;
  totalMotor3 += -Y;
  
  //Rotation Calculations
  //All motors are used for rotation
  totalMotor1 += rotation;
  totalMotor2 += rotation;
  totalMotor3 += rotation;
  
  //Set wheel biases
  totalMotor1 = totalMotor1 * motor1Bias;
  totalMotor2 = totalMotor2 * motor2Bias;
  totalMotor3 = totalMotor3 * motor3Bias;
  
  //Motor 1
  serial_print("Motor 1: ");
  serial_println(totalMotor1);
  if (totalMotor1 > 0) {
    totalMotor1 = 255 - totalMotor1; //The H-Bridges we use have an active low PWM so lets take that into account
    digitalWrite(PIN_MOTOR_1_ENABLE, HIGH);
    analogWrite(PIN_MOTOR_1_PWM_RIGHT, totalMotor1);
    analogWrite(PIN_MOTOR_1_PWM_LEFT, 255);
  } else if (totalMotor1 < 0) {
    totalMotor1 = 255 + totalMotor1;
    digitalWrite(PIN_MOTOR_1_ENABLE, HIGH);
    analogWrite(PIN_MOTOR_1_PWM_RIGHT, 255);
    analogWrite(PIN_MOTOR_1_PWM_LEFT, totalMotor1);
  } else {
    analogWrite(PIN_MOTOR_1_PWM_RIGHT, 255);
    analogWrite(PIN_MOTOR_1_PWM_LEFT, 255);
    digitalWrite(PIN_MOTOR_1_ENABLE, LOW);
  }
  
  //Motor 2
  serial_print("Motor 2: ");
  serial_println(totalMotor2);
  if (totalMotor2 > 0) {
    totalMotor2 = 255 - totalMotor2;
    digitalWrite(PIN_MOTOR_2_ENABLE, HIGH);
    analogWrite(PIN_MOTOR_2_PWM_RIGHT, totalMotor2);
    analogWrite(PIN_MOTOR_2_PWM_LEFT, 255);
  } else if (totalMotor2 < 0) {
    totalMotor2 = 255 + totalMotor2;
    digitalWrite(PIN_MOTOR_2_ENABLE, HIGH);
    analogWrite(PIN_MOTOR_2_PWM_RIGHT, 255);
    analogWrite(PIN_MOTOR_2_PWM_LEFT, totalMotor2);
  } else {
    digitalWrite(PIN_MOTOR_2_ENABLE, LOW);
    analogWrite(PIN_MOTOR_2_PWM_RIGHT, 255);
    analogWrite(PIN_MOTOR_2_PWM_LEFT, 255);
  }
  
  //Motor 3
  serial_print("Motor 3: ");
  serial_println(totalMotor3);
  if (totalMotor3 > 0) {
    totalMotor3 = 255 - totalMotor3;
    digitalWrite(PIN_MOTOR_3_ENABLE, HIGH);
    analogWrite(PIN_MOTOR_3_PWM_RIGHT, totalMotor3);
    analogWrite(PIN_MOTOR_3_PWM_LEFT, 255);
  } else if (totalMotor3 < 0) {
    totalMotor3 = 255 + totalMotor3;
    digitalWrite(PIN_MOTOR_3_ENABLE, HIGH);
    analogWrite(PIN_MOTOR_3_PWM_RIGHT, 255);
    analogWrite(PIN_MOTOR_3_PWM_LEFT, totalMotor3);
  } else {
    digitalWrite(PIN_MOTOR_3_ENABLE, LOW);
    analogWrite(PIN_MOTOR_3_PWM_RIGHT, 255);
    analogWrite(PIN_MOTOR_3_PWM_LEFT, 255);
  }

}

void serial_println(char string[]) {
  if (DEBUG) {
    Serial.println(string);
  }
}

void serial_println(int num) {
  if (DEBUG) {
    Serial.println(num);
  }
}

void serial_print(char string[]) {
  if (DEBUG) {
    Serial.print(string);
  }
}

void serial_print(int num) {
  if (DEBUG) {
    Serial.print(num);
  }
}

No problem on the large code. I'm gonna cut out most of it anyway.
This is Arduino IDE V1.0? That is what this is compiled on. Pingable in 2 seconds.
Check/change the ip/gateway/subnet settings!

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};  //The media access control address for the ethernet shield
IPAddress ip(10,0,1,2); //the IP address for the ethernet shield
byte gateway[] = { 10,0,1,1 };
byte subnet[] = { 255,255,255,0 };

#define PORT            80  //The port the ethernet recieves packets on
#define DEBUG           true    //Output running data to computer serial terminal


EthernetServer server = EthernetServer(PORT);

void setup() {
  Ethernet.begin(mac, ip, subnet, gateway);  //Initialize the ethernet device
  delay(2000);
  server.begin();           //Start listening for clients on the Ethernet

  if (DEBUG) {
    Serial.begin(9600); //Start serial communication to computer
    Serial.println("Serial has begun");
  }

}

void loop()
{
}

Edit: This code doesn't do anything but respond to a ping and the serial output.

I'm running it on Arduino 1.0.

What would you recommend I change those settings too? Should i be matching them with something on the windows side?

Edit: did you set your computers IP to the gateway IP? Could that be the problem?

Those network settings are important.
The subnet must match the subnet on your localnet. That is normally 255.255.255.0.
The gateway must be in the localnet range. On most routers, this will be 192.168.0.1.
The ip must be in the localnet range of the gateway. It must be unique. In this case with that gateway and subnet, the ip must be something between 192.168.0.2 and 192.168.0.254.

Check your Windows computer. What ip, gateway, and subnet is it assigned?

I don't have access to the computer at the moment. We haven't been setting a gateway though. The computer is directly connected to the arduino. If that's the case what should the gateway be set to?

Both gateways should be the same, as should the subnets, on the Windows computer and the ethernet shield.

The ip on the shield must not be the gateway ip or the Windows computer ip. Insure the ethernet shield ip has the first three numbers of the Windows ip address, but a different 4th (last) number, just not the same last number as the gateway. In this case it doesn't make a difference about the gateway ip, but when you connect these back to a router, it will!

Thank you very much! Ill see if that fixes the problem when I get home.