Data Logger + RF Transmission Project

Project Description:
I wish to do a data logger circuit which can measure voltage of a circuit every 5 minutes. The data (voltage and time) will be save in a SD card. When a pc sends a command character to the circuit, the circuit will send back whatever data stored in the circuit (in other words "download"). The communication part of the system and pc is through Radio Frequency. FYI, I am using 2 arduino uno, 2 sets 433Mhz RF module,RTC3231, and a SD Card Module.

My idea is:
ISR-> Read voltage and time, then store into SD card. (Every 5min)
Main Loop -> Waiting to receive command "k". If a character "k" receive, all the data in text file(inside SD card) will transmit to pc site.
I am new to arduino. Can i get some advise in order to proceed? Please correct me if i'm wrong. Thank you.

Current Status and Problem Facing:
I am able to read and store data (voltage and time) in a text file inside the SD card. However, when a character "k" sent from pc, there is a chance of crashing with the ISR timer while sending/downloading the data. The circuit will stop sending the data to the pc when ISR trigger. After the ISR, the circuit will not continue the sending process. I suspect that after ISR, the circuit start the main loop again.

Below is my code (Updated):

#include <avr/io.h>
#include <avr/interrupt.h>
#include <Wire.h>
#include <VirtualWire.h>
#include <SoftI2C.h>
#include <DS3232RTC.h>
#include <SD.h>
#include <stdlib.h>

File myFile;
SoftI2C i2c(A4, A5);
DS3232RTC rtc(i2c);


#define LEDPIN 13
  // you must download and install the VirtualWire.h to your hardware/libraries folder
#undef int
#undef abs
#undef double
#undef float
#undef round



int seconds = 0;
int j = 0;
int storage [60];
int tempvoltage;
int tempyea;
int tempmont;
int tempda;
int temphou;
int tempminu;
int tempsecon;
int k = 0; 
char verify[2];
int precision1;
int precision2;
int precision3;
int mult;
float charf1;
char tempdata1[2];
char* temppointer1;
char* tempstore1;
char buffer[50];
String tempstring;
 
void setup()
{
    
     Serial.begin(9600);	  // Debugging only
    vw_set_ptt_inverted(true);    // Required for RX Link Module
    vw_setup(2000);                   // Bits per sec
    vw_set_rx_pin(6);           // We will be receiving on pin 23 (Mega) ie the RX pin from the module connects to this pin. 
    vw_rx_start();                      // Start the receiver 
    vw_set_tx_pin(3);     
  
    Serial.print("Initializing SD card...");

   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  
  
 
    pinMode(LEDPIN, OUTPUT);
 
    // initialize Timer1
    cli();          // disable global interrupts
    TCCR0A = 0;     // set entire TCCR1A register to 0
    TCCR0B = 0;     // same for TCCR1B
 
    // set compare match register to desired timer count:
    OCR0A = 256;
    // turn on CTC mode:
    TCCR0B |= (1 << WGM12);
    // Set CS10 and CS12 bits for 1024 prescaler:
    TCCR0B |= (1 << CS10);
    TCCR0B |= (1 << CS12);
    // enable timer compare interrupt:
    TIMSK0 |= (1 << OCIE1A);
    // enable global interrupts:
    sei();
    
    
}
 
ISR(TIMER0_COMPA_vect)
{
        seconds++;
    if (seconds == 500)
    {
        seconds = 0;   
        readMySensor();
        myFile.close();
    }
}


void loop()
{
      
     uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	int i;
        
        digitalWrite(13, true); // Flash a light to show received good message
	// Message with a good checksum received, dump it.
	Serial.print("Got: ");
	
	for (i = 0; i < buflen; i++)
	{
	  verify[1] = buf[i]  ;
         
          Serial.println(verify[1]);
	}
       
       if (verify[1] == 'k')
       {
        vw_rx_stop();
          myFile = SD.open("log.txt");
        if (myFile) 
        {
          // read from the file until there's nothing else in it:
         while (myFile.available()) 
         {
        tempdata1[0] = char(myFile.read());
        tempdata1[1]= 0;


        if (tempdata1[0] != ' ')
        {
          tempstring = tempstring + tempdata1;
        }
        else
        {
           tempstring = tempstring + ' ';
          tempstring.toCharArray(buffer,50);
        vw_send((uint8_t *)buffer, strlen(buffer));
        vw_wait_tx();
          tempstring = "";
        }

  }
       
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
         vw_rx_start();  
       }
	
        digitalWrite(13, false);
    }

void readMySensor()
{  
      RTCTime time;
    RTCDate date;
    rtc.readTime(&time);
    rtc.readDate(&date);
      digitalWrite(13, true);
      int sensorValue = analogRead(A0);
      float voltage = sensorValue * (5.0 / 1023.0);
  myFile = SD.open("log.txt", FILE_WRITE);  
          if (myFile) 
          {
             Serial.print("Writing to log.txt...");
             //myFile.print("sensor:");
             myFile.print( voltage );
             myFile.print(" ");
             myFile.print(date.year);
             myFile.print("/");
             myFile.print(date.month);
             myFile.print("/");
             myFile.print(date.day);
             myFile.print(" ");
             myFile.print(time.hour);
             myFile.print(":");
             myFile.print(time.minute);
             myFile.print(":");
             myFile.println(time.second);
             myFile.close(); 
             Serial.println("done.");
             
          } 
        else 
          {
        Serial.println("error opening log.txt");
          }

}

Is there any ideas on how to avoid the crashes or how to continue the transmission/downloading of the data after ISR? Please correct me if i'm wrong. Thanks in advance help.

} doesn't match in loop() and readMySensor(), I was unable to compile.

Please try this. This is able to be compiled.Thanks.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <Wire.h>
#include <VirtualWire.h>
#include <SoftI2C.h>
#include <DS3232RTC.h>
#include <SD.h>
#include <stdlib.h>

File myFile;
SoftI2C i2c(A4, A5);
DS3232RTC rtc(i2c);


#define LEDPIN 13
  // you must download and install the VirtualWire.h to your hardware/libraries folder
#undef int
#undef abs
#undef double
#undef float
#undef round



int seconds = 0;
int j = 0;
int storage [60];
int tempvoltage;
int tempyea;
int tempmont;
int tempda;
int temphou;
int tempminu;
int tempsecon;
int k = 0; 
char verify[2];
int precision1;
int precision2;
int precision3;
int mult;
float charf1;
char tempdata1[2];
char* temppointer1;
char* tempstore1;
char buffer[50];
String tempstring;
 
void setup()
{
    
     Serial.begin(9600);	  // Debugging only
    vw_set_ptt_inverted(true);    // Required for RX Link Module
    vw_setup(2000);                   // Bits per sec
    vw_set_rx_pin(6);           // We will be receiving on pin 23 (Mega) ie the RX pin from the module connects to this pin. 
    vw_rx_start();                      // Start the receiver 
    vw_set_tx_pin(3);     
  
    Serial.print("Initializing SD card...");

   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  
  
 
    pinMode(LEDPIN, OUTPUT);
 
    // initialize Timer1
    cli();          // disable global interrupts
    TCCR0A = 0;     // set entire TCCR1A register to 0
    TCCR0B = 0;     // same for TCCR1B
 
    // set compare match register to desired timer count:
    OCR0A = 256;
    // turn on CTC mode:
    TCCR0B |= (1 << WGM12);
    // Set CS10 and CS12 bits for 1024 prescaler:
    TCCR0B |= (1 << CS10);
    TCCR0B |= (1 << CS12);
    // enable timer compare interrupt:
    TIMSK0 |= (1 << OCIE1A);
    // enable global interrupts:
    sei();
    
    
}
 
ISR(TIMER0_COMPA_vect)
{
        seconds++;
    if (seconds == 500)
    {
        seconds = 0;   
        readMySensor();
        myFile.close();
    }
}


void loop()
{
      
     uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	int i;
        
        digitalWrite(13, true); // Flash a light to show received good message
	// Message with a good checksum received, dump it.
	Serial.print("Got: ");
	
	for (i = 0; i < buflen; i++)
	{
	  verify[1] = buf[i]  ;
         
          Serial.println(verify[1]);
	}
       
       if (verify[1] == 'k')
       {
        vw_rx_stop();
          myFile = SD.open("log.txt");
        if (myFile) 
        {
          // read from the file until there's nothing else in it:
         while (myFile.available()) 
         {
        tempdata1[0] = char(myFile.read());
        tempdata1[1]= 0;


        if (tempdata1[0] != ' ')
        {
          tempstring = tempstring + tempdata1;
        }
        else
        {
           tempstring = tempstring + ' ';
          tempstring.toCharArray(buffer,50);
        vw_send((uint8_t *)buffer, strlen(buffer));
        vw_wait_tx();
          tempstring = "";
        }

  }
       
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
         vw_rx_start();  
       }
	
        digitalWrite(13, false);
    }

void readMySensor()
{  
      RTCTime time;
    RTCDate date;
    rtc.readTime(&time);
    rtc.readDate(&date);
      digitalWrite(13, true);
      int sensorValue = analogRead(A0);
      float voltage = sensorValue * (5.0 / 1023.0);
  myFile = SD.open("log.txt", FILE_WRITE);  
          if (myFile) 
          {
             Serial.print("Writing to log.txt...");
             //myFile.print("sensor:");
             myFile.print( voltage );
             myFile.print(" ");
             myFile.print(date.year);
             myFile.print("/");
             myFile.print(date.month);
             myFile.print("/");
             myFile.print(date.day);
             myFile.print(" ");
             myFile.print(time.hour);
             myFile.print(":");
             myFile.print(time.minute);
             myFile.print(":");
             myFile.println(time.second);
             myFile.close(); 
             Serial.println("done.");
             
          } 
        else 
          {
        Serial.println("error opening log.txt");
          }

}

My idea is:
ISR-> Read voltage and time, then store into SD card. (Every 5min)

What is going to trigger the interrupt service routine? What is the required tolerance on when the data is read? Is it 5 minutes +/- 0.001 seconds, or is it "5 minutes more or less".

Polling to determine if there is serial data or if it's time to take another reading are not mutually exclusive.

How rapidly does the system need to respond to a download request? With 5 minutes between readings, a delay of a few milliseconds in taking a reading or in responding to a download request does not seem unreasonable.

An ISR is supposed to be fast. Interrupts are disabled while the ISR is running, so millis() doesn't update, Serial I/O doesn't happen, etc. Reading the sensor and getting the time in the ISR may be OK, but writing to the SD card in the ISR definitely is not.

I really see no reason to be using interrupts at all.

What is going to trigger the interrupt service routine?

Thanks for your reply. I am using timer to trigger the interrupt service routine (ISR). I just want to get a set of value (data) every 5min. There is no required tolerance. Let say my circuit ISR start on 12:00am, the following time will be 12:05am,12:10am and so on. However, there is a chance where someone will request download on 12:04:50am and if the transmission cannot finish on 12:05am, ISR will be triggered and then, main loop is stop.

This is the case that i wish to avoid. Currently, i set my timer to around 6 second. The ISR is start every 6 seconds and i faced this problem. Is there any solution to continue the download after ISR? Also, if writing to SD card is not suitable to be run in ISR, where should i put it to?

To echo PaulS, I really don't see why you're using interrupts at all.You can use millis to tell when it's time to take a sensor reading - see blink without delay example. So every iteration of loop, you can check to see whether it's sensor time or whether a request for data transmission has been received.

If you want to avoid delaying the sensor read because you're sending data, you can break that up too so that it only sends a single line from the file on each iteration of loop. Note that you'll need to be careful that you don't try to write the latest reading to the file while you're reading from it. One solution to this might be to open a new log file when you're asked for data - do you really want to send all your log history every time the PC asks or just whatever's new since the last transmission?

Is there any solution to continue the download after ISR?

After the interrupt, the program returns to the place it left off. If you've diddled with a file while the program was interrupted, it is possible that something happened to cause the code to fail after an interrupt happens. If you don't use interrupts, then interrupt conflicts can be avoided.

You could, when asked to download the data, determine how long it is until the next read/store cycle, and if that is going to happen before the downloaded can be completed, you could refuse to download data at this time. I'd do this only if it was critical that there be exactly 5 minutes between reading.

If that is not critical, I'd do the download and then take the next reading as soon as possible after the download completes, and then again on regular 5 minute cycles.