I'm working on a project in which I am trying to log data values that have been obtained from sensors into an SD card.
I am using an SD card shield v4.0 by Seeed studio connected to an Arduino Mega. Currently, I'm using the EM-406A gps module for the project and I have been initially writing the data values obtained from the gps simply by opening the file within the loop and logging the data as it goes.
However, I have read that logging data in this manner would operate would slow (and it definitely did). So I read that logging "blocks" of data can improve the speed, but I'm not really sure how to do that. As of now, I'm storing the gps values (latitude, longitude, speed, altitude) as arrays and as the array reaches a certain size, I open the file within the SD card and write the values in using a for loop. I set the array size limit as a number as 128 and once the array reaches 128 values, it writes each data into the SD card file. But for some reason, this only happens once. It would reach 128 values and write it into the SD card, but the process seems to stop after that time.
int i = 0;
float lat, lon;
unsigned long times;
// This is the main loop of the code. All it does is check for data on
// the RX pin of the ardiuno, makes sure the data is valid NMEA sentences,
// then jumps to the getgps() function.
time = millis();
while(uart_gps.available()) // While there is data on the RX pin...
int c = uart_gps.read(); // load the data into a variable...
if(gps.encode(c)) // if there is a new valid sentence...
getgps(gps); // then grab the data.
// The getgps function will get and print the values we want.
void getgps(TinyGPS &gps)
// To get all of the data into varialbes that you can use in your code,
// all you need to do is define variables and query the object for the
// data. To see the complete list of functions see keywords.txt file in
// the TinyGPS and NewSoftSerial libs.
// Define the variables that will be used
float latitude, longitude;
// Then call this function
// You can now print variables latitude and longitude
// Same goes for date and time
byte month, day, hour, minute, second, hundredths;
// Print data and time
Serial.print("Date: "); Serial.print(month, DEC); Serial.print("/");
Serial.print(day, DEC); Serial.print("/"); Serial.print(year);
Serial.print(" Time: "); Serial.print(hour, DEC); Serial.print(":");
Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC);
Serial.print("."); Serial.println(hundredths, DEC);
//Since month, day, hour, minute, second, and hundr
// Here you can print the altitude and course values directly since
// there is only one value for the function
Serial.print("Altitude (meters): "); Serial.println(gps.f_altitude());
// Same goes for course
Serial.print("Course (degrees): "); Serial.println(gps.f_course());
// And same goes for speed
Serial.print("Speed(kmph): "); Serial.println(gps.f_speed_kmph());
lat[i] = latitude,5;
lon[i] = longitude,5;
alt[i] = gps.f_altitude();
cours[i] = gps.f_course();
vel[i] = gps.f_speed_kmph();
times[i] = time;
if (i == 128)
for (i = 0; i < 128; i++)
File dataFile = SD.open("gps.csv", FILE_WRITE);
Serial.println("Couldn't log GPS data");
i = 0;
// Here you can print statistics on the sentences.
unsigned long chars;
unsigned short sentences, failed_checksum;
gps.stats(&chars, &sentences, &failed_checksum);
//Serial.print("Failed Checksums: ");Serial.print(failed_checksum);
I don't know too much about Arduino since I just started using it about a month or two ago. But from reading through the forums it seems that writing data as "blocks" into the SD card is the fastest way. Could anyone explain more into how to write the data as blocks? Is that not just making it an array and then logging the array into the SD card?
Any help is appreciated!