unsigned long previousMillis = 0;
long interval = 10000;
void setup() {
pinMode(10, OUTPUT); //for some ethernet shield on the mega : disables the SD and enables the W5100
digitalWrite(10, HIGH);
Serial.begin(9600);
Ethernet.begin(mac);
When using the Leonardo, the board is not reset when you open Serial Monitor. This means that any output printed to Serial between the time you upload the sketch or power the board and when you open Serial Monitor is lost. That could include the very helpful Serial output in your setup() function.
The line:
while(!Serial);
causes the board to wait in that while loop until a serial connection is opened, which means you will see all the serial output from your program.
One thing to note is that if you had a sketch that you wanted to use without Serial Monitor open, you would not want to add that line, because the program would never run. When you're troubleshooting a problem this is useful, but later you might want to remove it.
If you look at the examples that come with the Ethernet library (File > Examples > Ethernet), there is some code that provides useful troubleshooting information.
Replace this line of your sketch:
Ethernet.begin(mac);
with this:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
Upload it to your Arduino board, then post what you see in Serial Monitor.