Strange WiFly Problem

OK, this one is confusing me.
I am writing a program to serve a webpage on the Arduino over the WiFly module.
I have written two programs, one with an LCD display, and a bit-bashing procedure to output to it, and one without.
Now I could understand if I’d done something stupid in these procedures to crash things, but I added two test lines of code to check what is happening and the InitialiseLCD function is working (as I get that serial confirmation on the terminal), but the program does not enter the loop procedure!
If I comment out the call to InitialiseLCD in setup, it works perfectly.
I really don’t understand this one.
Any help would be very gratefully received.
Due to space restrictions only the broken one is posted below:

#include "WiFly.h"
#include "Credentials.h"
#include <SoftwareSerial.h>

#define stringlength 23

//Pin for Relay Connection
int LED = 2;

//setup a web server from wifly library listening on port 80
Server server(80);

//setup variables for message parsing
String readstring = String(stringlength);
char secondtry[stringlength];
boolean current_state=false;
boolean validmessage=false;

//LCD Variable Setup
uint8_t _receivePin = 4;
uint8_t _transmitPin = 3;
long _baudRate = 2400;
int _bitPeriod = 1000000 / _baudRate;

void setup() {
  pinMode(LED,OUTPUT);
  pinMode(_receivePin, INPUT);        // sets the digital pin as input
  pinMode(_transmitPin, OUTPUT);       // sets the digital pin as output
  readstring="";

  WiFly.begin();

  if (!WiFly.join(ssid)) {
    while (1) {
      // Hang on failure.
    }
  }

  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  server.begin();
  InitialiseLCD();
  Serial.println("Initialised LCD");

}

void loop() {
  Serial.println("looped");
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
        for (int i = 0; i<=stringlength-1; i++){
          secondtry[i]=secondtry[i+1];
        }
        secondtry[stringlength-1]=c;

        // if we've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so we can send a reply
        if (c == '\n' && current_line_is_blank) {
          Serial.println("new HTTP request received");
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // Create a giant button on the screen
          client.print("<html>");
          client.print("<head>");
          client.print("<title>WiFi Socket</title>");
          client.print("</head>");
          client.print("<body style=\"background-color:#FFFFcc\">");
          client.print("<center>");
          client.print("
");
          client.print("<h1 style=\"font-size:7.5em; color:#003264; width:85%\" >Welcome to the WiFi Socket</h1>");
          //client.println("
");

          client.print("<form method=\"GET\">");
          //client.print(WiFly.ip());
          //client.print(""">");
          if (current_state == false)
          {
            client.print("<input type=\"hidden\" name=\"Set\" value=\"On\">");
            client.print("<h3 style=\"font-size:4.5em;color:#999999; width:80%\">The Socket is Currently Turned <font style=\"color:red\">Off</font></h3>");
            client.println("
");
            client.println("
");
            client.print("<button type=\"submit\" style=\"font-size:7em; width:80%;color:#777777\">Turn On</button>");
          }
          else
          {
            client.print("<input type=\"hidden\" name=\"Set\" value=\"Off\">");
            client.print("<h3 style=\"font-size:4.5em;color:#999999; width:80%\">The Socket is Currently Turned <font style=\"color:limegreen\">On</font></h3>");
            client.println("
");
            client.println("
");
            client.print("<button type=\"submit\" style=\"font-size:7em; width:80%;color:#777777\">Turn Off</button>");
          }
          client.print("</form>");
          client.println("
");
          client.print("</center>");
          client.print("</body>");
          client.print("</html>");
          break;
        }
        if (c == '\n') {
          //Serial.println("Non-Blank line received");
          readstring="";
          for (int i=0;i<=stringlength;i++){
            readstring.concat(secondtry[i]);
          }
          //Serial.println(readstring);
          if (validmessage==false){
            int questionmark = readstring.indexOf('?');
            if(readstring.substring(questionmark + 1 , questionmark + 7) == "Set=On")
            {
              digitalWrite(LED, HIGH);
              //Serial.println("Turned On Kettle");
              current_state=true;
              validmessage=true;
            }
            else if(readstring.substring(questionmark + 1 , questionmark + 8) == "Set=Off")
            {
              digitalWrite(LED, LOW);
              //Serial.println("Turned Off Kettle");
              current_state=false;
              validmessage=true;
            }    
          }

          // we're starting a new line
          current_line_is_blank = true;
        } 
        else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(100);
    client.stop();
    validmessage=false;
  }
}


void InitialiseLCD()
{
  Output(254);
  delay(10);
  Output(1);
  delay(200);
  Output(254);
  delay(10);
  Output(128);
  delay(50);
  SendWord("IP Address:     ");
  delay(50);
  Output(254);
  delay(10);
  Output(192);
  delay(50);
  char temp[16];
  strncpy(temp, WiFly.ip(), 16);
  for (int i = strlen(temp); i<=16; i++){
    temp[i] = *(" ");
  }
  SendWord(temp);  
}


// Section for sending serial data to PICAXE LCD using T2400 mode
void SendWord(char string[])
{
  int count = strlen(string);
  for (unsigned int i=0; i<count; i++){
    Output(string[i]);
    //Serial.println(string[i]);
    delay(5);
  } 
}

void Output(uint8_t b)
{
  if (_baudRate == 0)
    return;

  int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); // a digitalWrite is about 50 cycles
  byte mask;

  digitalWrite(_transmitPin, LOW);
  delayMicroseconds(bitDelay);

  for (mask = 0x01; mask; mask <<= 1) {
    if (b & mask){ // choose bit
      digitalWrite(_transmitPin,HIGH); // send 1
    }
    else{
      digitalWrite(_transmitPin,LOW); // send 1
    }
    delayMicroseconds(bitDelay);
  }

  digitalWrite(_transmitPin, HIGH);
  delayMicroseconds(bitDelay);
}

If I comment out the call to InitialiseLCD in setup, it works perfectly.

Commenting out that one (poorly named) function removes a lot of code, including a small amount of data from SRAM, getting you just under the available limit, would be my guess.

You have a lot of strings in loop that define the web page. Using a style sheet properly would reduce the size of the strings. If that is not an option, let the browser determine the display characteristics for now, and just define the content.

Look for FreeMemory() code to see how much SRAM (memory) you have available at key places in your code.

Thanks, I'll try that on Monday. Incidentally, why is my procedure (not a function as it does not return a variable) poorly named? I thought that was a fairly sensible name for it, given that it describes exactly what the procedure is supposed to do. Do you know if storing the webpage (or at least the non-dynamic parts of it) in the EEPROM would solve the problem you are suggesting? Just wondering if it's worth sticking it there instead. Thanks again for the help, I'll let you know how I get on.

Not the problem. I tried commenting out almost all of the HTML apart from the title and first H1 tag, and still no joy. Anyone else got any ideas?

Right, I have narrowed the problem down to the following code:

char temp[16];
  strncpy(temp, WiFly.ip(), 16);
  for (int i = strlen(temp); i<=16; i++){
    temp[i] = *(" ");
  }
  SendWord(temp);

Anyone know any reason why this would cause problems?
I’ve clearly done something stupid, and I expect it is a memory overflow issue.

I am an idiot! I had the loop going from 0 to 16, thereby overflowing the end of my 16 character string! Sorry for wasting everyone's time. :blush:

    temp[i] = *(" ");

should be

    temp[i] = ' ';

Thanks, I knew that looked funny, but couldn't figure out why it was throwing up a data conversion error.