0

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;
    }

}
  • 1
    Doing these kinds of thing inside the ISR is asking for problems. Just store the timestamp (millis) of the last time you received serial data, then if the time difference between the current time and that timestamp is more that 25000ms create a new file. – Gerben Jul 21 '16 at 18:07
  • Thank you very much for your answer,can i ask you to give an example to my sketch?i am not very experienced Regards. – user24095 Jul 21 '16 at 18:09
  • Also filename is never changed, so it wouldn't even work properly. – Gerben Jul 23 '16 at 14:48
1

In untested (pseudo-)code

unsigned long lastSerialInput = millis();


void loop()
{

    if(!myFile)
        myFile = SD.open(filename, FILE_WRITE);


    while(Serial.available())
    {
        incomingByte = Serial.read();
        myFile.println(incomingByte,HEX);

        lastSerialInput = millis();
    }

    // check for 25 seconds of inactivity
    if( myFile && (millis()-lastSerialInput > 25000 )
    {
        myFile.close();
        fileNo++;
        filename = "datalog";
        filename += fileNo;
        filename += ".txt";

        //save new fileNo to config.txt (though I'd personally find the file with the largest number in setup instead of relying on a config.txt file)
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.