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