i am trying to make an arduino pro mini sd card serial logger.The arduino should log the data that comes over the uart and write it to an sd card and if it is no data on the serial for 25 seconds to save automatically the data into the file.Each file are named diffrent every time they are saved(for example datalog0.txt, datalog1.txt and so on).I am creating also at the start of the sketch an config(config.txt) file where the number of the file present of the sd are stored and if the file already exist than a new one will be created, this option works good.Now, after the 25 seconds past and it is no more data over the serial the arduino dont close the datalog.txt file , so the next data will be saved in the same file, this is the problem i have, even in the config file the file number will be increased.I will attach the sketch and expect patient from you, ai am still newbie.I forgot to mention that it was working good for some time but not anymore, the only thing i did was to upload the bootloader from ide and since than is not working anymore.
Regards.
//#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>
#include <SoftwareSerial.h>
int i;
int incomingByte = 0;
//int data[512];
File myFile;
unsigned char bytes,flag_again;
unsigned int fileNo,timer_count;
String s_fileNo,filename,filename_master;
char comanda;
void Timer1_InIt()
{
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12) | (1 << CS10);
//TIMSK1 |= (1 << OCIE1A);
}
ISR(TIMER1_COMPA_vect)
{
timer_count++;
if(timer_count > 25)
{
timer_count = 0;
TIMSK1 &= ~(1 << OCIE1A);
myFile.close();
fileNo++;
myFile = SD.open("config.txt",FILE_WRITE);
myFile.seek(0);
myFile.print(fileNo);
myFile.close();
//software_Reset() ; //reset the sketch
}
}
SoftwareSerial mySerial(4, 5); // RX, TX
void setup()
{
cli();
Timer1_InIt();
sei();
mySerial.begin(9600);
Serial.begin(10752,SERIAL_8E2);
//mySerial.print("Initializing SD card...");
if (!SD.begin(10))
{
//mySerial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("config.txt",FILE_READ);
// mySerial.println("Config file created");
if(!myFile.available())
bytes = 0;
else
{
bytes = myFile.available();
for(i = 0; i< bytes ; i++)
s_fileNo += (char)myFile.read();
}
myFile.close();
fileNo = s_fileNo.toInt();
// mySerial.print("file no:");
//mySerial.println(fileNo);
filename = "datalog";
filename += fileNo;
filename += ".txt";
myFile = SD.open(filename,FILE_WRITE);
if (myFile)
{
//mySerial.print(filename);
//mySerial.println(" File Open...");
}
}
void loop()
{
if(!myFile)
myFile = SD.open(filename, FILE_WRITE);
while(Serial.available())
{
incomingByte = Serial.read();
/*if(incomingByte == 0 || incomingByte < 15 )
{
Serial.print('0');
myFile.print(0,HEX);
}*/
//mySerial.println(incomingByte,HEX);
myFile.println(incomingByte,HEX);
// myFile.println(incomingByte);
timer_count = 0;
flag_again = 1;
}
if(!Serial.available() && flag_again)
{
TIMSK1 |= (1 << OCIE1A);
flag_again = 0;
}
}
filename
is never changed, so it wouldn't even work properly.