UDP and Ethernet Comms Together [SOLVED]

This is a strange problem.
I have written a program to connect to the internet over ethernet and download a webpage.
I am trying to add functionality to set the time using an NTP beforehand.
When I add in the functions to handle the NTP stuff, the program keeps resetting after the Serial link is established.
The functions are not being called, they are just sitting there.
If I comment them out, it works, but if I uncomment them it start resetting again.
Here is the code with the comments in place (working).

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 modified 9 Apr 2012
 by David A. Mellis
 
 */

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0x90, 0xA2, 0xDA, 0x0E, 0x9C, 0xCA };
IPAddress server(217,154,230,218); // www.towerbridge.org.uk
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

EthernetUDP Udp;
unsigned int localPort = 8888;  // local port to listen for UDP packets


char message[1000];

const int timeZone = 1;     // Central European Time

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 Serial.println("Serial up and running");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  Serial.print("IP number assigned by DHCP is ");
  Serial.println(Ethernet.localIP());
  // give the Ethernet shield a second to initialize:
  delay(1000);
  populateMessage();
  decodeMessage();
}

void loop()
{
 

}


void populateMessage(){
 while(!lookForMessage()){
   delay(2000);
 }
 Serial.println("Message was:");
 for (int i=0;i<1000;i++){
   Serial.print(message[i]);
 }
 Serial.println();   
}

boolean lookForMessage(){
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /TBE/EN/BridgeLiftTimes/ HTTP/1.1");
    client.println("Host: www.towerbridge.org.uk");
    client.println();
  } 
  else {
    // If you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  delay(500);
    // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    while (client.read()!='<'){
      ;
    }
    if (client.read()=='t' && client.read()=='r' && client.read()=='>'){
      for (int i=0;i<1000;i++){
        message[i]=client.read();
      }
      Serial.println("Message Received, disconnecting");
      client.stop();
      client.flush();
      return(true);
    }
  }
  
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  return(false);
}


void decodeMessage(){
  
}

/*
void getTime(){   
  Serial.println("waiting for time sync");
  setSyncProvider(getNtpTime);
  setSyncInterval(5);
  while (timeStatus() == timeNotSet){
    ;
  }
  setSyncInterval(43200);
  Serial.println("Time Set");
  digitalClockDisplay();
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

//-------- NTP code ----------//

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  Serial.println("No NTP Response :-(");
  return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:                 
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}
*/

Any help really greatly appreciated!
Many thanks

Oh and if I comment out the

populateMessage();
decodeMessage();

In setup, it works as well, just getting the time. It seems to be when I want to have a Udp and an Ethernet connection.

To narrow this down further, I've even moved theUdp.begin(localport)and the Udp.stop()into the getNtpTime routine just in case it was the setting up of the Udp connection that was the issue I still get the resetting issue even if the call to getTime() is commented out, so no Udp code is running, but is being compiled. So confused! :~

So just to be really clear, this works:

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4 };
IPAddress server(217,154,230,218); // www.towerbridge.org.uk
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov

const int timeZone = 1;     // Central European Time
//const int timeZone = -5;  // Eastern Standard Time (USA)
//const int timeZone = -4;  // Eastern Daylight Time (USA)
//const int timeZone = -8;  // Pacific Standard Time (USA)
//const int timeZone = -7;  // Pacific Daylight Time (USA)

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
EthernetUDP Udp;
unsigned int localPort = 8888;  // local port to listen for UDP packets

char message[1000];


void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.println("Serial up and running");
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("IP number assigned by DHCP is ");
  Serial.println(Ethernet.localIP());

//  getTime();

  populateMessage();
//  decodeMessage();
}

void loop()
{
 

}


void populateMessage(){
 while(!lookForMessage()){
   delay(2000);
 }
 Serial.println("Message was:");
 for (int i=0;i<1000;i++){
   Serial.print(message[i]);
 }
 Serial.println();   
}

boolean lookForMessage(){
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /TBE/EN/BridgeLiftTimes/ HTTP/1.1");
    client.println("Host: www.towerbridge.org.uk");
    client.println();
  } 
  else {
    // If you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  delay(500);
    // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    while (client.read()!='<'){
      ;
    }
    if (client.read()=='t' && client.read()=='r' && client.read()=='>'){
      for (int i=0;i<1000;i++){
        message[i]=client.read();
      }
      Serial.println("Message Received, disconnecting");
      client.stop();
      client.flush();
      return(true);
    }
  }
  
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  return(false);
}


void decodeMessage(){
  
}

/*
void getTime(){   
  Serial.println("waiting for time sync");
  setSyncProvider(getNtpTime);
  setSyncInterval(5);
  while (timeStatus() == timeNotSet){
    ;
  }
  setSyncInterval(43200);
  Serial.println("Time Set");
  digitalClockDisplay();
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

//-------- NTP code ----------//

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  Udp.begin(localPort);
  delay(1000);
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      Udp.stop();
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  Serial.println("No NTP Response :-(");
  Udp.stop();
  return 0; // return 0 if unable to get the time
  
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:                 
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}
*/

And this works:

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4 };
IPAddress server(217,154,230,218); // www.towerbridge.org.uk
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov

const int timeZone = 1;     // Central European Time
//const int timeZone = -5;  // Eastern Standard Time (USA)
//const int timeZone = -4;  // Eastern Daylight Time (USA)
//const int timeZone = -8;  // Pacific Standard Time (USA)
//const int timeZone = -7;  // Pacific Daylight Time (USA)

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
EthernetUDP Udp;
unsigned int localPort = 8888;  // local port to listen for UDP packets

char message[1000];


void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.println("Serial up and running");
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("IP number assigned by DHCP is ");
  Serial.println(Ethernet.localIP());

  getTime();

//  populateMessage();
//  decodeMessage();
}

void loop()
{
 

}


void populateMessage(){
 while(!lookForMessage()){
   delay(2000);
 }
 Serial.println("Message was:");
 for (int i=0;i<1000;i++){
   Serial.print(message[i]);
 }
 Serial.println();   
}

boolean lookForMessage(){
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /TBE/EN/BridgeLiftTimes/ HTTP/1.1");
    client.println("Host: www.towerbridge.org.uk");
    client.println();
  } 
  else {
    // If you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  delay(500);
    // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    while (client.read()!='<'){
      ;
    }
    if (client.read()=='t' && client.read()=='r' && client.read()=='>'){
      for (int i=0;i<1000;i++){
        message[i]=client.read();
      }
      Serial.println("Message Received, disconnecting");
      client.stop();
      client.flush();
      return(true);
    }
  }
  
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  return(false);
}


void decodeMessage(){
  
}


void getTime(){   
  Serial.println("waiting for time sync");
  setSyncProvider(getNtpTime);
  setSyncInterval(5);
  while (timeStatus() == timeNotSet){
    ;
  }
  setSyncInterval(43200);
  Serial.println("Time Set");
  digitalClockDisplay();
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

//-------- NTP code ----------//

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  Udp.begin(localPort);
  delay(1000);
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      Udp.stop();
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  Serial.println("No NTP Response :-(");
  Udp.stop();
  return 0; // return 0 if unable to get the time
  
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:                 
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

But the above with the populateMessage() function uncommented doesn’t!
Even if I comment out the call to getTime()!
:~
Please help!

Maybe you are running out of SRAM. Which Arduino are you using?

This array is using a considerable amount of your SRAM.

char message[1000];

You STAR! That's it. Reduced the message array to 100 and all plays nice. I'll have to do some clever parsing, but that works! MANY MANY THANKS XD XD XD