question regarding.flush() command

Hello,

I am using my arduino to record data at a 20 hz rate. I am questioning the use of the .flush command versus a .close and then reopening the file at the start of my void loop(). Then again I may be missing something regarding both the .flush and .close commands.

I also have a question about resetting the arduino. I found a blog where the blogger used the following

void(*resetFunc)()=0;

then when they wanted to reset the arduino they would write

resetFunc();

Is this correct?

#include<Adafruit_CC3000.h>
#include<Adafruit_GPS.h>
#include<SoftwareSerial.h>
#include<SPI.h>
#include<SD.h>

File dataFile;

SoftwareSerial mySerial(8,7);
Adafruit_GPS GPS(&mySerial);
byte n=0;
byte g=0;

void setup(){
  
  Serial.begin(19200);
  GPS.begin(9600);
  pinMode(10,OUTPUT);
 
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   
  GPS.sendCommand(PGCMD_ANTENNA);
  
  char filename[15];
  strcpy(filename,"Sen_1_00.TXT"); // File name example. Sensor_number_LogNumber--> Sensor=Sen, number= 1-5, LogNumber= 1-99
  for (uint8_t i = 1; i < 100; i++) {
    filename[6] = '0' + i/10;
    filename[7] = '0' + i%10;
    if(i==99){
      g=1;
    }
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }
  }
  
  delay(1000);
  mySerial.println(PMTK_Q_RELEASE);
  while(n==0){
    char c = GPS.read();
    if (GPS.newNMEAreceived()) {  
      if (!GPS.parse(GPS.lastNMEA()))
        n=0;
    }
    if (millis()%3000 < 500) { 
      if(GPS.hour!=0 && GPS.day!=0){
        dataFile=SD.open(filename,FILE_WRITE);
        if(dataFile){
          while(Serial.available()>0){
            char in=Serial.read();
            dataFile.print(in);
          }
          dataFile.print("\nTime: ");
          dataFile.print(GPS.hour, DEC); dataFile.print(':');
          dataFile.print(GPS.minute, DEC); dataFile.print(':');
          dataFile.print(GPS.seconds, DEC); dataFile.print('.');
          dataFile.println(GPS.milliseconds);
          dataFile.print("Date: ");
          dataFile.print(GPS.day, DEC); dataFile.print('/');
          dataFile.print(GPS.month, DEC); dataFile.print("/20");
          dataFile.println(GPS.year, DEC);
          dataFile.println("Time, Solar Radiation, U Velocity, V Velocity, W Velocity, Units, Speed of Sound, Sonic Temp, Status");
          dataFile.flush();
          n=5;
        }
      }
    }
  }
}

void(*resetFunc)(void)=0;

void loop(){
  if(Serial.available()>0){
    if(n==5){
      unsigned long time = millis();
      if (time > 21600000){            //reset after 6 hours
        resetFunc();
      }
      int Sensor1= analogRead(0);
      if(dataFile){
        dataFile.print(time);
        dataFile.print(",");
        dataFile.print(Sensor1);
        dataFile.print(",");
        while(Serial.available()>0){
          char in=Serial.read();
          dataFile.print(in,DEC);
        }
        dataFile.flush(); 
      }
      if(!dataFile){
        n=4;
      }
    }
  }
  if(g==1){
    // Send error message saying that max file number has been reached
    while(g==1){
      g=1;
    }
  }
  if(n==4){
    resetFunc();
  }
}

Thank you

mschramm:

Where to begin???

  for (uint8_t i = 1; i < 100; i++) {
    filename[6] = '0' + i/10;
    filename[7] = '0' + i%10;
    if(i==99){
      g=1;
    }

When this loop ends, the 7th and 8th characters in filename will both be 9, i will be 99 and g will be 1…every time you try to create a new file! I presume that’s not what you want. I would keep a record of the last file number used and increment this every time you want to create a new filename. Something like this:

 int i = 0; //put this before setup()
create new filename...
    i++;  //numbering starts at 01 and goes up to 99.
    if(i==100){
      g=1;
      i = 0; // max exceeded, so reset counter
             // warn max exceeded and file not created, here
    }
    else{  
      filename[6] = '0' + i/10;
      filename[7] = '0' + i%10;
     // create new file here
    }

A properly written program never needs to reset itself. And if the program should crash it won't be able to reset itself unless you enable the Watch Dog Timer.

I presume xxx.flush() waits until all the data is written to the file. is that what you want?

...R

You need to check the doc's/code for your SD lib. Close should flush the tx/out going buffer before closing it. But some authors are stupid enough to think that flush should throw away the input buffer.

By the way the term is NOT command it is method or function Google the wrong term you get bad results.

Mark

I thought that "break" exits the for loop at what ever value "i" was at, at that time. I could be wrong.

The way I was going to write the sd log part of this code was to use xxx.close. In the main loop I would open and close the file for every iteration. Would this be a good idea?