Reading .txt file won't work with code which is unrelated

Good evening, this is my first post.
I've been doing a lot of work with Arduino recently using the ethernet shield, by I've encountered a problem that I'm not able to overcome yet.

My arduino will not read the .txt file using this code :

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


#include <SD.h>

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
byte mac[] = { 
 0x90, 0xA2, 0xDA, 0x0D, 0x03, 0xB2 }; //physical mac address
byte ip[] = { 
 192, 168, 1, 177 }; // ip in lan
byte gateway[] = { 
 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 
 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port


String readString;

char requested[100];
int i = 0;

int untilLunch1;
int untilLunch2;
int untilLunch3;
int untilLunch4;
int untilLunch5;

int nbCharOk;
int compteur = 0;
int compt = 0;
int verif = 0;
int hour;
int minute;

char hourChar[2];
char minuteChar[2];


File htmlFile;


void setup()
{
 // pinMode(6, OUTPUT); //pin selected to control
 //start Ethernet
 Ethernet.begin(mac, ip, gateway, subnet);
 server.begin();
 //the pin for the servo co
 //enable serial data print
 Serial.begin(9600);
 pinMode(10, OUTPUT);
 Serial.println("test SD load html file 1.0"); // so I can keep track of what is loaded


 if(!SD.begin(4))
 {
   Serial.println("Erreur de chargement de la carte");
 }
 else Serial.println("Carte Mini SD chargée correctement");
}
void loop () 
{
 // Create a client connection
 EthernetClient client = server.available();
 //Serial.println(millis());
 if (client) {
   Serial.println("Client repéré");
   while (client.connected()) {
     if (client.available()) {
       char c = client.read();

       requested[i] = c;
       i = i + 1;

       //Serial.println(requested);

       //Serial.println(c);
       //read char by char HTTP request
       if (readString.length() < 100) {

         //store characters to string
         readString += c;
         //Serial.println(readString);

       }

       //if HTTP request has ended
       if (c == '\n') {

         ///////////////
         Serial.println(readString); //print to serial monitor for debuging

         char mainPage[15] = "GET / HTTP/1.1";
         char hourSent[100] = "GET /?hour=";
         char doseSent[15] = "GET /?n1=";
         char testNoModification[2] = "N";
         
         
         // comparaison avec requête "heure"
         nbCharOk = 0;
         for (compteur = 5; compteur < 10; compteur++)
         {
          if(hourSent[compteur] == requested[compteur])
          {
           nbCharOk = nbCharOk + 1; 
          }
           
         }
         Serial.println(nbCharOk);
         client.println("<p></p>");
         if(nbCharOk == 5)
         {
          hourChar[0] = requested[11];
          hourChar[1] = requested[12];
          minuteChar[0] = requested[13];
          minuteChar[1] = requested[14];
          
          int Dizaine = hourChar[0] - '0';
          int Unit = hourChar[1] - '0';
          hour = Dizaine * 10 + Unit;
          
          Dizaine = minuteChar[0] - '0';
          Unit = minuteChar[1] - '0';
          minute = Dizaine * 10 + Unit;
          
          Serial.println(minute);
          Serial.println(hour);
         }
         nbCharOk = 0;

         if(true)
         {

           Serial.println("HTLML FILE REQUEST CONFIRMED");
           // open the file. note that only one file can be open at a time,
           // so you have to close this one before opening another.
           File dataFile = SD.open("datalog.txt");

           // if the file is available, write to it:
           if (dataFile) {
             while (dataFile.available()) {
               //Serial.write(dataFile.read());
               client.write(dataFile.read());
             }
             
           }
           
           else Serial.println("Bug");
           dataFile.close();  
           Serial.println("Etape 3");

           
         }
         // comparaison avec requête doses et traitement requête
          for (compt = 4; compt <8; compt++)
          {
            if(requested[compt] == doseSent[compt])
            {
              nbCharOk = nbCharOk + 1;
            }
          }
          Serial.println(compt);
          
          Serial.println(nbCharOk);
          if (nbCharOk == 4)
          {
           Serial.println("NB DOSE TIME"); 
           // Instructions pour s'assurer qu'il y a modifications
           nbCharOk = 0;
           Serial.println(requested);
           for (verif = 3; verif <94; verif = verif + 6)
           {
             Serial.println(verif);
             Serial.println(requested[verif]);
             if(requested[verif] == testNoModification[0])
             {
              nbCharOk = nbCharOk + 1; 
             }
           }
           // si nbCharOk = 15, alors on a envoyé un formulaire vide
           if (nbCharOk != 15)
           {
             Serial.println("Ordre reçu, traitement ...");
           }
          }




         delay(1);
         //stopping client
         client.stop();
         //clearing string for next read


         readString="";
         i = 0;

       }
     }
   }
 }


}

Ok, let me precise something, it works if I remove the Bolded code, which is completely unrelated to the SD Card !
This code :

// comparaison avec requête doses et traitement requête
          for (compt = 4; compt <8; compt++)
          {
            if(requested[compt] == doseSent[compt])
            {
              nbCharOk = nbCharOk + 1;
            }
          }
          Serial.println(compt);
          
          Serial.println(nbCharOk);
          if (nbCharOk == 4)
          {
           Serial.println("NB DOSE TIME"); 
           // Instructions pour s'assurer qu'il y a modifications
           nbCharOk = 0;
           Serial.println(requested);
           for (verif = 3; verif <94; verif = verif + 6)
           {
             Serial.println(verif);
             Serial.println(requested[verif]);
             if(requested[verif] == testNoModification[0])
             {
              nbCharOk = nbCharOk + 1; 
             }
           }
           // si nbCharOk = 15, alors on a envoyé un formulaire vide
           if (nbCharOk != 15)
           {
             Serial.println("Ordre reçu, traitement ...");
           }
          }

I would like to precise that I tried putting it in a function that was not used in the entire code, and it still buggs.
I am using a Arduino Uno.

Thank you for your time and reading me, I am completely blocked, and I don't know what is the problem.
If you want me to add/modify/remove something in this post, tell me, this is my first post, so it won't be a perfect one ^^.

Please use [ code ] tags. The forum has eaten one of your array indexes [ i ] and made all the text italic.

Done ! :slight_smile:

Your problem is likely low SRAM. When I build your program with 1.6.4 I get this warning.

Low memory available, stability problems may occur.

When you remove code the SRAM use is reduced slightly.

Try using the F() macro with literal strings in print/println statements. This causes the strings to be stored in flash instead of SRAM.

Change statements of this type

Serial.println("test SD load html file 1.0");

To this

Serial.println(F("test SD load html file 1.0"));

Your program may still be unstable since use of

String readString;

may fragment the heap.

Hi

If you are using a 2KB SRAM system then having both an ethernetclient and SD card file instantiated at the same time will likely cause SRAM problems. Just opening an SD card takes 512 bytes for the SD card input/output buffer.

On my Arduino system at http://www.2wg.co.nz/ I do dump text files onto web pages but my system has 8KB of SRAM and works just fine.

Cheers

Catweazle NZ

First, thank you guys for the help, the F() Macro litterally saved the day, it seems to work.
CatweazleNZ, I think that I'll just update the arduino if this type of problem happens again.

For now, it seems to work fine, but since it is not finished, problems can still occur, but I think that I have the answer to continue now, thank you guys, sincerley, I was clue-less. :smiley: