Go Down

Topic: SD Card won't create multiple file (Read 484 times) previous topic - next topic

Van_03

Hello Guys. I am working on a project that creates a variable filename of "s+lognum.txt" where accelerometer data  will be stored  every 28 ms when a '0' is sent to the device. for e.g if lognum=1, filename would be "s1.txt" Then if the device receives a '1' it would open the same filename ("s1.txt") you created from the previous, reads stored data and sends it to the other arduino every 20 ms thru HC12. If the device receives another '0' a new file would be created now this time "s2.txt" must be  created and so on and so forth. However, I can't figure out why it won't create another file or even open it. Here is my code below. Any input will be helpful. Thanks!



Code: [Select]

#include <SoftwareSerial.h>
#include <SD.h>
#include<SPI.h>
const int chipSelect = 10;
File gFile;
SoftwareSerial HC12(2, 3); //RX, TX
int state = 0;
byte incomingByte;
int f=1; // stopper for mode 2 loop
int g=1; //stopper for mode 0 loop
int p=1;
int lognum=1;
int m=0;
char filename[16]={'\0'}; ///variable file name
void setup()

{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12

  Serial.print("Initializing SD card... ");
  pinMode(SS, OUTPUT);

    if (!SD.begin(chipSelect)) // checks SD Card status
   {
     Serial.println("SD card not found... Try again");
 
     while (1) ;
   }
 
   Serial.println("Data logging initiated");
   delay(1000);
   HC12.print("OK");  //checks device if on
}


void loop()
{
 
while (HC12.available()>0)              //waits for HC-12 to have data
  {               
    incomingByte = HC12.read();      // Store each incoming byte from HC-12
 
  }

///-------------------------------------------------------------------------------------------------//

 if(incomingByte=='0') // MODE 0 start trigger from android device for data logging.
    {
     
     if ((lognum%2)==1){
     
      Serial.print(lognum);
      Serial.print(m);
       sprintf(filename,"s%d.txt",lognum-m); 
     lognum++;
     m++;
  }
      String dataString = "";
      for (int analogPin = 0; analogPin < 3; analogPin++)
      {
         int sensor = analogRead(analogPin);
            dataString += String(sensor);
          if (analogPin ==0) {
              dataString += "X";
            }
          if (analogPin == 1) {
              dataString += "Y";
            }
          if (analogPin == 2) {
              dataString += "Z";
            }
           
       }

     gFile = SD.open(filename, FILE_WRITE); // writes /logs data to sd card
   
   
     if (gFile)
       {
           gFile.println(dataString);
           Serial.println(dataString);     // checks data saved
           gFile.close();
       }
      else
       {
        Serial.println("error opening file");
       }
     
      delay(28); // may vary for generation of 20 samples  estimation
       f=1;
       g=1;
       p=1;
       
    }
if (incomingByte=='1') // MODE 1  reads and send stored data
  {
    if ((lognum%2)==0){
      Serial.print(lognum);
      Serial.print(m);
     sprintf(filename,"s%d.txt",lognum-m);
     lognum++;
    }
    gFile = SD.open(filename, FILE_READ);
   
   if ((gFile) &&(f==1))
   {
       Serial.print("yes"); // checks whether the file exists
       Serial.println();
       delay(1000);
      while (gFile.available())
      {
         char reading[10] = ""; // store read data per line in the txt file
         char temp = gFile.read();
         int idx = 0;
         bool somethingWasRead = false; // indicated when a line is read
         
            while (temp != -1 && temp != 13 && temp != 10)
         {
         
          reading[idx] = temp;
          temp = gFile.read();
          idx++;
          somethingWasRead = true;
         
         }
         
         reading[idx++]='\0';
       
       
        if (somethingWasRead)
         {
         
          HC12.print(reading);
          Serial.println(reading);
         
          delay(20);
       
         
         }
  }
  gFile.close();
  gFile.flush();

  f=0; 
  p=1;

  }
 }
}

Go Up