Ethernet Library is acting strangely!

I’m trying to make a Ethernet to DMX converter, and I’m doing pretty good.
It is working when I don’t use alot of the tags.
But then if I do, like in this code, then I get some strange garbage (look at the picture, and check the code)

#include <Ethernet.h>
/*
this code will get all the analog values from the pins
it will default the digital pins to output and allow you to turn them on or off 
added code from 
*/

// include pin definition library -----------------------------------------------
#include "pins_arduino.h"

// variable definitions ---------------------------------------------------------
int sig = 3;       // DMX signal pin - DO NOT CHANGE

// network configuration.  gateway and subnet are optional.
byte mac[] = { 0x00, 0x14, 0xA4, 0xD0, 0x8D, 0x52 };
byte ip[] = { 192, 168, 2, 113 };


byte DMX_Get[]={0,0,0,0,0};
byte Buffer[]={0,0,0,0,0};

Server server = Server(80);
unsigned long time;

// function definitions ---------------------------------------------------------
/* Sends a DMX byte out on a pin.  Assumes a 16 MHz clock.
 * Disables interrupts, which will disrupt the millis() function if used
 * too frequently. */
void shiftDmxOut(int pin, int theByte)
{
  int port_to_output[] = {
    NOT_A_PORT,
    NOT_A_PORT,
    _SFR_IO_ADDR(PORTB),
    _SFR_IO_ADDR(PORTC),
    _SFR_IO_ADDR(PORTD)
    };

    int portNumber = port_to_output[digitalPinToPort(pin)];
  int pinMask = digitalPinToBitMask(pin);

  // the first thing we do is to write te pin to high
  // it will be the mark between bytes. It may be also
  // high from before
  _SFR_BYTE(_SFR_IO8(portNumber)) |= pinMask;
  delayMicroseconds(10);

  // disable interrupts, otherwise the timer 0 overflow interrupt that
  // tracks milliseconds will make us delay longer than we want.
  cli();

  // DMX starts with a start-bit that must always be zero
  _SFR_BYTE(_SFR_IO8(portNumber)) &= ~pinMask;

  // we need a delay of 4us (then one bit is transfered)
  // this seems more stable then using delayMicroseconds
  asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");
  asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");

  asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");
  asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");

  asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");
  asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");

  for (int i = 0; i < 8; i++)
  {
    if (theByte & 01)
    {
      _SFR_BYTE(_SFR_IO8(portNumber)) |= pinMask;
    }
    else
    {
      _SFR_BYTE(_SFR_IO8(portNumber)) &= ~pinMask;
    }

    asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");
    asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");

    asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");
    asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");

    asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");
    asm("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n");

    theByte >>= 1;
  }

  // the last thing we do is to write the pin to high
  // it will be the mark between bytes. (this break is have to be between 8 us and 1 sec)
  _SFR_BYTE(_SFR_IO8(portNumber)) |= pinMask;

  // reenable interrupts.
  sei();
}

void setup()
{
  pinMode(sig, OUTPUT);       // data pin to dmx board 
  // initialize the ethernet device
  Ethernet.begin(mac, ip);  //, gateway, subnet);
  server.begin();
  Serial.begin(9600);
}


void loop()
{
  Client client = server.available();
  if (client) {
    
    server.print("HTTP/1.0 200 OK\r\nServer: arduino\r\nContent-Type: text/html\r\n\r\n");
    server.print("<HTML><HEAD><TITLE>");
    server.print("Arduino Web-Server - ");
    server.print(millis());//to make sure that we habve a new page
    server.print("</TITLE>");
    server.print("<script type=\"text/JavaScript\"><!--function timedRefresh(timeoutPeriod){setTimeout(\"location.reload(true);\",timeoutPeriod);}//--></script>");
    server.print("</HEAD><BODY>");
    capturevariable(client);//dump the headers and recover any values passed in from the form
    
    server.print("Pan:");
    server.print("<form name='pan1' method='get'><input name='p' type='hidden' value='0'><input type='submit' value='0 degrees'></form>");
    server.print("<form name='pan2' method='get'><input name='p' type='hidden' value='128'><input type='submit' value='180 degrees'></form>");
    server.print("<form name='pan3' method='get'><input name='p' type='hidden' value='255'><input type='submit' value='360 degrees'></form>
");    
    server.print("Tilt:");
    server.print("<form name='tilt1' method='get'><input name='t' type='hidden' value='0'><input type='submit' value='0 degrees'></form>");
    server.print("<form name='tilt2' method='get'><input name='t' type='hidden' value='128'><input type='submit' value='180 degrees'></form>");
    server.print("<form name='tilt3' method='get'><input name='t' type='hidden' value='255'><input type='submit' value='360 degrees'></form>
");        

    server.print("</BODY></HTML>");
    client.stop();//close the connection with the client
    delay(100);//pause wait  for a new connection
  }   

  /***** sending the dmx signal *****/

  // sending the break (the break can be between 88us and 1sec)
  digitalWrite(sig, LOW);
  delay(10);

  // sending the start byte
  shiftDmxOut(sig, 0);
        
  shiftDmxOut(sig, DMX_Get[0]); // Pan value
  shiftDmxOut(sig, DMX_Get[1]); // Tilt value
  shiftDmxOut(sig, 0); // Shutter open
  shiftDmxOut(sig, 0); // Clear gobo
  shiftDmxOut(sig, 0); // Color fade
   
  for (int count = 1; count <= 507; count++)
  {
    shiftDmxOut(sig, 0);
  }
  /***** sending the dmx signal end *****/         
}


void capturevariable(Client client){
  //we get a string like:GET /?p=255&t=100&o=255 we enter here after the ? or the &
  byte count = 0;
  int count_all = 0;
   
  char c = client.read();  
  server.print(c);
  while ((c!= -1) && (count < 64) && (c != '?')){
    c = client.read();
    server.print(c); // Debug
    count++;
  }
     
    count = 0;
    count_all = 0;
    
    while ((c != -1) && (count_all < 64) && (c != 'H')){
      c = client.read();
      server.print(c); // Debug
      count_all++;
      
      if(c=='p'){
        Serial.print("P=");
        c = client.read();//skip the =
        server.print(c); // Debug
        
        count = 0;
        while ((c != -1) && (count < 4) && (c != '&') && (c != ' ')){
          if (c != '&') { 
            c = client.read();
            server.print("<b>"); // Debug
            server.print(c); // Debug
            server.print("</b>"); // Debug
            Buffer[count] = c-48;
            count++;
          }
        }
        if (count == 2) {
          DMX_Get[0] = Buffer[0];
        }
        if (count == 3) {
          DMX_Get[0] = Buffer[1];
          DMX_Get[0] = DMX_Get[0] + (Buffer[0] * 10);
        }
        if (count == 4) {
          DMX_Get[0] = Buffer[2];
          DMX_Get[0] = DMX_Get[0] + (Buffer[1] * 10);
          DMX_Get[0] = DMX_Get[0] + (Buffer[0] * 100);
        }
        Serial.print(DMX_Get[0], DEC);
      }
      
      if(c=='t'){
        Serial.print("T=");
        c = client.read();//skip the =
        server.print(c); // Debug
        
        count = 0;
        while ((c != -1) && (count < 4) && (c != '&') && (c != ' ')){
          if (c != '&') {
            c = client.read();
            server.print("<b>"); // Debug
            server.print(c); // Debug
            server.print("</b>"); // Debug
            Buffer[count] = c-48;
            count++;
          }
        }
        if (count == 2) {
          DMX_Get[1] = Buffer[0];
        }
        if (count == 3) {
          DMX_Get[1] = Buffer[1];
          DMX_Get[1] = DMX_Get[1] + (Buffer[0] * 10);
        }
        if (count == 4) {
          DMX_Get[1] = Buffer[2];
          DMX_Get[1] = DMX_Get[1] + (Buffer[1] * 10);
          DMX_Get[1] = DMX_Get[1] + (Buffer[0] * 100);
        }
        Serial.print(DMX_Get[1], DEC);
      }      
    }
    
    while ((c != -1)){
      c = client.read();
    }
}

You're probably running out of RAM. Look into using PROGMEM to store your strings.

I'd also suggest generating your forms from a function that only requires the common code to be included once.

--Phil.

Why should it be the RAM? Isn't the string it sends thru the server.print command saved in the Flash? You know, the string between the ""

server.print("STRING");

Isn't the string it sends thru the server.print command saved in the Flash?

Yes the string is stored in flash, but when the sketch gets executed all the strings get copied into RAM first unless they are marked as PROGMEM.

--Phil.

Oh, ok. Can you use PROGMEM on functions, or isn't it only useable on variables?

You can only use PROGMEM on constants. Functions are already in PROGMEM, since they are part of your program, and PROGMEM is short for PROGram MEMory.

Then why aren't the Strings in the Ethernet Print constants already constants?

Because of the way C is defined; string contants in C are writeable. It didn't matter in 1979 on a PDP-11, but on a Harvard architecture microcontroller, it makes a big difference.

Ok the. But now I've successfully made a function, so it works! But why?