simple web server problem

i'm trying to run a simple webserver program on a duemilanova with an ethernet shield with a built in sdcard reader. i also want the program to do html redirects depending on where i have a potentiometer pointed. i.e., if the pot is between 0-400, and the user access one.htm, he will be redirected to square.htm. if the pot is between 401-800 and the user access one.htm, he will be redirected to triangle.htm.
simple enough, right?
however, when i put the if conditionals into the webserver program the .html pages don't load.
I will paste the code down below. this little problem has been consuming my entire weekend!

        #include <SPI.h>
        #include <SdFat.h>
            #include <SdFatUtil.h>
            #include <Ethernet.h>
        #include <LiquidCrystal.h>
            byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
            byte ip[] = { 192, 168, 1, 177 };
            char rootFileName[] = "index.htm";
            Server server(80);
        LiquidCrystal lcd(7, 6, 5, 8, 3, 2);
            /************ SDCARD STUFF ************/
            Sd2Card card;
            SdVolume volume;
            SdFile root;
            SdFile file;
            // store error strings in flash to save RAM
            #define error(s) error_P(PSTR(s))

            void error_P(const char* str) {
              PgmPrint("error: ");
              SerialPrintln_P(str);
              if (card.errorCode()) {
                PgmPrint("SD error: ");
                Serial.print(card.errorCode(), HEX);
                Serial.print(',');
                Serial.println(card.errorData(), HEX);
              }
              while(1);
            }

            void setup() {
              Serial.begin(9600);
        lcd.begin(16, 2);
        lcd.print("booting up...");
              PgmPrint("Free RAM: ");
              Serial.println(FreeRam());
             
              // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
              // breadboards.  use SPI_FULL_SPEED for better performance.
              pinMode(10, OUTPUT);                       // set the SS pin as an output (necessary!)
              digitalWrite(10, HIGH);                    // but turn off the W5100 chip!

              if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
             
              // initialize a FAT volume
              if (!volume.init(&card)) error("vol.init failed!");

              PgmPrint("Volume is FAT");
              Serial.println(volume.fatType(),DEC);
              Serial.println();
             
              if (!root.openRoot(&volume)) error("openRoot failed");

              // list file in root with date and size
              PgmPrintln("Files found in root:");
              root.ls(LS_DATE | LS_SIZE);
              Serial.println();
             
              // Recursive list of all directories
              PgmPrintln("Files found in all dirs:");
              root.ls(LS_R);
             
              Serial.println();
              PgmPrintln("Done");
             
              // Debugging complete, we start the server!
              Ethernet.begin(mac, ip);
              server.begin();
            }

            // How big our line buffer should be. 100 is plenty!
            #define BUFSIZ 100

            void loop()
            {

/////if pot is in x number range print to LCD and assign a different value to var/////////
      int pot = analogRead(0);
           int var;
        if ((pot > 0)&&(pot < 400)) {
           lcd.clear();
           lcd.print("FIRST CHAMBER");
           delay(40);
         var=0;
           
        }     

          if ((pot > 401)&&(pot < 800)) {
            lcd.clear();
            lcd.print("SECOND CHAMBER");
            delay(40);
           var=1;
          }
         
           if ((pot > 801)&&(pot < 1024)) {
             lcd.clear();
             lcd.print("THIRD CHAMBER");
             delay(40);
    var=2;
           }
           
           
/////////////webserver code i got from the adafruit forums/
              char clientline[BUFSIZ];
              char *filename;
              int index = 0;
              int image = 0;

              Client client = server.available();
             
              if (client) {                               
                // an http request ends with a blank line
                boolean current_line_is_blank = true;
               
                // reset the input buffer
                index = 0;
               
                while (client.connected()) {
                  if (client.available()) {
                    char c = client.read();
                   
                    // If it isn't a new line, add the character to the buffer
                    if (c != '\n' && c != '\r') {
                      clientline[index] = c;
                      index++;
                      // are we too big for the buffer? start tossing out data
                      if (index >= BUFSIZ)
                        index = BUFSIZ -1;
                     
                      // continue to read more data!
                      continue;
                    }
                   
                    // got a \n or \r new line, which means the string is done
                    clientline[index] = 0;
                    filename = 0;
                   
                    // Print it out for debugging
                    Serial.println(clientline);
                   
                    // Look for substring such as a request to get the root file
                    if (strstr(clientline, "GET / ") != 0) {
                      filename = rootFileName;
                    }
                    if (strstr(clientline, "GET /") != 0) {
                      // this time no space after the /, so a sub-file
                     
                      if (!filename) filename = clientline + 5; // look after the "GET /" (5 chars)
                      // a little trick, look for the " HTTP/1.1" string and
                      // turn the first character of the substring into a 0 to clear it out.
                      (strstr(clientline, " HTTP"))[0] = 0;
                     
                      // print the file we want
                      Serial.println(filename);

                      if (! file.open(&root, filename, O_READ)) {
                        client.println("HTTP/1.1 404 Not Found");
                        client.println("Content-Type: text/html");
                        client.println();
                        client.println("<h2>File Not Found!</h2>");
                        break;
                      }
                                                       
                      Serial.println("Opened!");
                     
                      client.println("HTTP/1.1 200 OK");
                      if (strstr(filename, ".htm") != 0)
                         client.println("Content-Type: text/html");
                      else if (strstr(filename, ".jpg") != 0)
                         client.println("Content-Type: image/jpeg");
                     else if (strstr(filename, ".gif") != 0)
                         client.println("Content-Type: image/gif");
                     else
                         client.println("Content-Type: text");

                      client.println();
                         
   
                      int16_t c;
                      while ((c = file.read()) >= 0) {
                          // uncomment the serial to debug (slow!)
                          //Serial.print((char)c);
                          client.print((char)c);
                      }
                      file.close();
                    } else {
                      // everything else is a 404
                      client.println("HTTP/1.1 404 Not Found");
                      client.println("Content-Type: text/html");
                      client.println();
                      client.println("<h2>File Not Found!</h2>");
                    }
               
//////int conditionals for html redirect...THIS DOESN'T WORK/////////                     
 if ((strstr(filename, "one.htm") !=0)&&(var==0)){  
      client.println("<head>");
      client.println("<meta HTTP-EQUIV='REFRESH' content='0; url=http://192.168.1.177/sq1.htm'>");
      client.println("</head>");

      }
      
 if ((strstr(filename, "one.htm") !=0)&&(var==1)){  
      client.println("<head>");
      client.println("<meta HTTP-EQUIV='REFRESH' content='0; url=http://192.168.1.177/tr1.htm'>");
      client.println("</head>");

  }
if ((strstr(filename, "one.htm") !=0)&&(var==2)){  
      client.println("<head>");
      client.println("<meta HTTP-EQUIV='REFRESH' content='0; url=http://192.168.1.177/cir1.htm'>");
      client.println("</head>");

      }
      
if ((strstr(filename, "two.htm") !=0)&&(var==0)){  
      client.println("<head>");
      client.println("<meta HTTP-EQUIV='REFRESH' content='0; url=http://192.168.1.177/sq2.htm'>");
      client.println("</head>");

      }      
///etc...had to delecte other conditionals for post size///////////////////////////

           break;
                  }
                }
                // give the web browser time to receive the data
                delay(1);
               
                client.stop();
              }
             
               
             
            }

You can use the Serial class at the same time as the ethernet shield.

Add Serial.print() statements to output pot, var, filename, and everything written to the client.

Show the output that you get.

Explain what "this doesn't work" means. Is it that the code in those blocks does not get output, or that the html code doesn't cause a redirect like you think should happen?

Hi and thanks for your help!

When I type in Serial.print(var);, the var does not print into the serial monitor. The SD card and all its contents do print out and when I try to open an htm file off the sdcard the serial monitor refreshes but the page doesn't open in my browser....it just hangs. (A simple webserver does work...i am connected to the right network and everything)

I am having both the problems you mentioned. I do not get a serial output of the variable 'var' in my monitor and I am unable to open an .htm file. Something appears to be stuck somewhere.

Here is my output in the serial monitor. I should be getting a 0 a 1 or 2 somewhere, right? I put Serial.print(var); right before the conditionals that do the html redirects...(if I put it right after my 'pot' conditionals I will get one number that doesn't repeat and once again a hang...also the Serial monitor doesn't refresh when I try to load a page)

Free RAM: 151
Volume is FAT16

Files found in root:
TWO.HTM 2011-01-07 15:02:02 86
CIR1.HTM 2011-01-07 14:57:18 82
CIR2.HTM 2011-01-07 14:57:32 82
CIR3.HTM 2011-01-07 14:57:46 84
ONE.HTM 2011-01-07 15:01:46 77
SQ1.HTM 2011-01-07 14:58:22 81
SQ2.HTM 2011-01-07 14:58:36 81
SQ3.HTM 2011-01-07 14:58:50 83
THREE.HTM 2011-01-07 15:02:16 86
TR1.HTM 2011-01-07 14:59:02 83
TR2.HTM 2011-01-07 14:59:16 83
TR3.HTM 2011-01-07 14:59:30 85

Files found in all dirs:
TWO.HTM
CIR1.HTM
CIR2.HTM
CIR3.HTM
ONE.HTM
SQ1.HTM
SQ2.HTM
SQ3.HTM
THREE.HTM
TR1.HTM
TR2.HTM
TR3.HTM

Done

I'm beginning to suspect that you are running out of memory. The output that you showed simply proves that the SD card can be read from. So, knowing that that works, comment out all that stuff.

The LCD doesn't appear to be doing anything useful. So, get rid of it, for the time being.

This should significantly reduce the amount of SRAM being used.

Does this have any impact on the behavior of the program?

[edit]Didn't notice this at first. Free RAM: 151
You are definitely running out of SRAM.[/edit]

Shoot that worked! Thanks! But it's really crucial for this program that I use the LCD! Any ideas what I can do? With all the LCD stuff commented out it works fine. I've never run into this problem. So am I using too many libraries or something? The strange thing is, is that this code worked fine in the past.

Now that you have code that works, start putting the stuff you commented out back in. Put the LCD stuff back first. Does it still work? Great.

Then, put more stuff back, if you need to.

If not, you might need to use a different kind of LCD that doesn't have as big a library driving it, or use smaller arrays (make BUFSIZ smaller), or use PROGMEM (that's a search term), or all of the above.

Any ideas what I can do?

Maybe you can upgrade to a bigger ATMega chip. What's installed now?

Another possibility is to move some of those constant character strings into PROGMEM: you may be chewing up lots of RAM without realizing it. Of course, with all the features and libraries you're using, you could also be low on flash...

Switched the duemilanova to a mega...working so far! Just getting to be a more expensive project! Ugh! Thanks for all the help!