Trouble with Logging Data

Hi All,

First of all, I apologize for my messy code. I'm honestly kind of embarrassed about it, which is why I took so long to post it on here. However, my brain is now fried and I am making no progress after about 7 hours of research (I'm not the best researcher...) and debugging, so this is my call of desperation.

I'm doing a project for work that includes gathering a bunch of data from various sensors and I want to put it on an SD card. However, when I include the "myFile = SD.open(filename, FILE_WRITE);" line, my code doesn't seem to even go through the loop.

I'm probably missing something simple, but my programming skills are weak at best.

My whole code is too large to include here, but I've included it as an attachment.

Here's the main loop:

void loop() 
{
  
  if (Serial.available() > 0) 
  {
    incomingByte = Serial.read(); // read the incoming byte:
    
     //after 10000 cycles, the test stops     
    while (testDuration < 10000)
    {
      // run the simple timer
      timer.run();
      
      //i counts how many cycles pass
      if (i < 55)  
      {
        if (fileFlag == true)
        {
          cycleSeq();//Run the whole cycle
          Serial.println(fileFlag);
        }
        if (fileFlag == false)//create the file and write data to it
        {
          getFileName();
         // createFileName();
         myFile = SD.open(filename, FILE_WRITE);//Open the file to write something to it
         myFile.println("pumpOn , valveOpen , vent1Open, vent2Open , valve1Open ,  valve2Open , outValveOpen ,  inValveOpen , heaterOn"); //print header to file
         myFile.close();
         
          Serial.println(fileFlag);
          delay (500);
          fileFlag = true;
        }
      }
      //after 55 cycles, run thermal cycle
      if (i >= 55)
      {
        cooldownWarmup();
      }
        
    
      //shows values in serial monitor
      valueCase();
      
      //stop test
        if (incomingByte == 's')
      {
        Serial.print ("stop");
        testDuration = 10000;
      }
     }      
  }  
}

Thanks to anyone willing to take a look.

ValveTestLogger141216.ino (19 KB)

I'm doing a project for work that includes gathering a bunch of data from various sensors and I want to put it on an SD card. However, when I include the "myFile = SD.open(filename, FILE_WRITE);" line, my code doesn't seem to even go through the loop.

The SD library is probably the one that gives folks much trouble. It takes SRAM (25% on an UNO) and is sometimes difficult to integrate with other libraries. Because of this, I "off load" SD logging to a dedicated 328 using serial. Everything serial is recorded, so you can do comma delimited, use text, or block the data anyway you like.
SD data logger

Ray

I'll have to look into that, thanks. I realized that I was indeed running out of space in my code, mostly because of an inefficient use of memory. I'm now working on storing my larger string variables in flash memory using PROGMEM, so hopefully that should help a bit.

It could be memory. SD reserves over 700 bytes of ram and it uses an additional 160 or so on the stack.

It's worth checking to see how much free memory you have. This is one way to do that. If you have only 300 bytes free at the point where you call open it will probably fail.

It may not a free memory problem.

If it is, there are ways to conserve RAM that aren't too difficult. Putting all of those literals in your serial print statements in flash memory by using the F() macro is one way. Using SdFat instead of SD will save you another chunk. It really depends on how close your sketch is to fitting.