I started noticing some of my Dataloggers (https://www.adafruit.com/products/1141) stop working after a while. Some days they run for a few days and others they don't even run for the entire day. However, as soon as I power off my Mega2560 and turn it back on, it appears to be working again. My understanding from reading around is there is some sort of limitation on how much I can actually write to an SD card but I may be misinformed. Is there way to work around this?
Hardware: Mega2560 with an Adafruit Datalogger, Seeedstudio 125kHz RFID modules connected to all four serial ports.
Here is the part of the code for the Datalogger:
Code: Select all
//////////Time to SD Code//////////
void timeToSd() {
if (dataFile) {
//Serial.begin(57600);
DateTime now = RTC.now();
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.print(" | ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.println(now.second(), DEC);
Serial.println("");
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print('/');
dataFile.print(now.year(), DEC);
dataFile.print(',');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.println(","); //"println" should be at the end, not at the "Time: "
XBee.print(now.month(), DEC);
XBee.print('/');
XBee.print(now.day(), DEC);
XBee.print('/');
XBee.print(now.year(), DEC);
XBee.print(',');
XBee.print(now.hour(), DEC);
XBee.print(':');
XBee.print(now.minute(), DEC);
XBee.print(':');
XBee.print(now.second(), DEC);
XBee.println(",");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
Code: Select all
// RFID reader for Arduino
// Wiring version by BANNED <http://people.interaction-ivrea.it/h.BANNED>
// Modified for Arudino by djmatic
// Modified for Electronic Brick 125Khz by Ujwal2
#include <EEPROM.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <SoftwareSerial.h>
File dataFile;
int val = 0;
char code[10];
int bytesread = 0;
File myFile;
RTC_DS1307 RTC;
SoftwareSerial XBee(2, 3);
//New Block Marker// - DO NOT TOUCH
char NewD[]="NewDataBlock";
//Aviary// CHANGE FOR AVIARY
char Avy[]="COOP"; //ex. COOP for Barn Coop
//Empty Data Field//
char BkN[]="N/A"; //ex. ="N/A";
//Location IDs// UNIQUE TO ARDUINO
char P0_L[]="Bowl_01"; //Location 0 Name
char P1_L[]="Bowl_02"; //Location 1 Name (ex. Nest_1)
char P2_L[]="Bowl_03"; //Location 2 Name (ex. Nest_2)
char P3_L[]="Bowl_04"; //Location 3 Name (ex. Nest_3)
//Location Type// UNIQUE TO ARDUINO
char P0_T[]="N/A"; //Location 0 Type
char P1_T[]="N/A"; //Location 1 Name
char P2_T[]="N/A"; //Location 2 Name
char P3_T[]="N/A"; //Location 3 Name
//////////Time to SD Code//////////
void timeToSd() {
if (dataFile) {
//Serial.begin(57600);
DateTime now = RTC.now();
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.print(" | ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.println(now.second(), DEC);
Serial.println("");
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print('/');
dataFile.print(now.year(), DEC);
dataFile.print(',');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.println(","); //"println" should be at the end, not at the "Time: "
XBee.print(now.month(), DEC);
XBee.print('/');
XBee.print(now.day(), DEC);
XBee.print('/');
XBee.print(now.year(), DEC);
XBee.print(',');
XBee.print(now.hour(), DEC);
XBee.print(':');
XBee.print(now.minute(), DEC);
XBee.print(':');
XBee.print(now.second(), DEC);
XBee.println(",");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
XBee.begin(9600);
Serial.begin(9600); // RFID reader works at 9600 BAUD rate
Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600);
Wire.begin();
RTC.begin();
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(SS, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(10,11,12,13)) { // this will need to be changed to chipselect for non-mega boards
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("card initialized.");
Serial.println("");
// Open up the file we're going to log to!
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (! dataFile) {
Serial.println("error opening datalog.txt");
// Wait forever since we cant write data
while (1) ;
}else {
DateTime now = RTC.now();
//New Data Block 0 Pin//
Serial.print("Starting New Data Block: ");
Serial.print(Avy);
Serial.print(" , ");
Serial.print(BkN);
Serial.print(" , ");
Serial.print(P0_L);
Serial.print(" , ");
Serial.print(P0_T);
Serial.print(" , ");
dataFile.print(Avy);
dataFile.print("-");
dataFile.print(NewD);
dataFile.print(",");
dataFile.print(BkN);
dataFile.print(",");
dataFile.print(P0_L);
dataFile.print(",");
dataFile.print(P0_T);
dataFile.print(",");
XBee.print(Avy);
XBee.print("-");
XBee.print(NewD);
XBee.print(",");
XBee.print(BkN);
XBee.print(",");
XBee.print(P0_L);
XBee.print(",");
XBee.print(P0_T);
XBee.print(",");
timeToSd();
//New Data Block 1st Pin//
Serial.print("Starting New Data Block: ");
Serial.print(Avy);
Serial.print(" , ");
Serial.print(BkN);
Serial.print(" , ");
Serial.print(P1_L);
Serial.print(" , ");
Serial.print(P1_T);
Serial.print(" , ");
dataFile.print(Avy);
dataFile.print("-");
dataFile.print(NewD);
dataFile.print(",");
dataFile.print(BkN);
dataFile.print(",");
dataFile.print(P1_L);
dataFile.print(",");
dataFile.print(P1_T);
dataFile.print(",");
XBee.print(Avy);
XBee.print("-");
XBee.print(NewD);
XBee.print(",");
XBee.print(BkN);
XBee.print(",");
XBee.print(P1_L);
XBee.print(",");
XBee.print(P1_T);
XBee.print(",");
timeToSd();
//New Data Block 2nd Pin//
Serial.print("Starting New Data Block: ");
Serial.print(Avy);
Serial.print(" , ");
Serial.print(BkN);
Serial.print(" , ");
Serial.print(P2_L);
Serial.print(" , ");
Serial.print(P2_T);
Serial.print(" , ");
dataFile.print(Avy);
dataFile.print("-");
dataFile.print(NewD);
dataFile.print(",");
dataFile.print(BkN);
dataFile.print(",");
dataFile.print(P2_L);
dataFile.print(",");
dataFile.print(P2_T);
dataFile.print(",");
XBee.print(Avy);
XBee.print("-");
XBee.print(NewD);
XBee.print(",");
XBee.print(BkN);
XBee.print(",");
XBee.print(P2_L);
XBee.print(",");
XBee.print(P2_T);
XBee.print(",");
timeToSd();
//New Data Block 3rd Pin//
Serial.print("Starting New Data Block: ");
Serial.print(Avy);
Serial.print(" , ");
Serial.print(BkN);
Serial.print(" , ");
Serial.print(P3_L);
Serial.print(" , ");
Serial.print(P3_T);
Serial.print(" , ");
dataFile.print(Avy);
dataFile.print("-");
dataFile.print(NewD);
dataFile.print(",");
dataFile.print(BkN);
dataFile.print(",");
dataFile.print(P3_L);
dataFile.print(",");
dataFile.print(P3_T);
dataFile.print(",");
XBee.print(Avy);
XBee.print("-");
XBee.print(NewD);
XBee.print(",");
XBee.print(BkN);
XBee.print(",");
XBee.print(P3_L);
XBee.print(",");
XBee.print(P3_T);
XBee.print(",");
timeToSd();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
readRfidPort1();
readRfidPort4();
readRfidPort3();
readRfidPort2();
}
//////////////////////////////////////////////////////////////////////////////////////////////
void readRfidPort1() { //1st Pin (RX = pin 0 & TX = pin 1)
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 2) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 2)||(val == 3)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
//codeToSd();
//timeToSd();
Serial.print("RFID Aviary-Tag: ");
Serial.print(Avy);
Serial.print("-");
Serial.println(code);
Serial.print("No Data: ");
Serial.println(BkN);
Serial.print("Location & Location Type: ");
Serial.print(P0_L);
Serial.print(" | ");
Serial.println(P0_T);
Serial.print("Writing Time to File: ");
digitalWrite(13, HIGH); //indicate it by glowing LED at pin 13
delay(1500); // delay 1.5 seconds
dataFile.print(Avy);
dataFile.print("-");
dataFile.print(code);
dataFile.print(",");
dataFile.print(BkN);
dataFile.print(",");
dataFile.print(P0_L);
dataFile.print(",");
dataFile.print(P0_T);
dataFile.print(",");
XBee.print(Avy);
XBee.print("-");
XBee.print(code);
XBee.print(",");
XBee.print(BkN);
XBee.print(",");
XBee.print(P0_L);
XBee.print(",");
XBee.print(P0_T);
XBee.print(",");
timeToSd();
dataFile.flush();
digitalWrite(13, LOW); // glow off LED
}
bytesread = 0;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
void readRfidPort4 () { // 2nd Pin (RX = pin 15 & TX = pin 14)
if(Serial3.available() > 0) { // if data available from reader
if((val = Serial3.read()) == 2) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial3.available() > 0) {
val = Serial3.read();
if((val == 2)||(val == 3)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
//codeToSd();
//timeToSd();
Serial.print("RFID Aviary-Tag: ");
Serial.print(Avy);
Serial.print("-");
Serial.println(code);
Serial.print("No Data: ");
Serial.println(BkN);
Serial.print("Location & Location Type: ");
Serial.print(P1_L);
Serial.print(" | ");
Serial.println(P1_T);
Serial.print("Writing Time to File: ");
digitalWrite(13, HIGH); //indicate it by glowing LED at pin 13
delay(1500); // delay 1.5 seconds
dataFile.print(Avy);
dataFile.print("-");
dataFile.print(code);
dataFile.print(",");
dataFile.print(BkN);
dataFile.print(",");
dataFile.print(P1_L);
dataFile.print(",");
dataFile.print(P1_T);
dataFile.print(",");
XBee.print(Avy);
XBee.print("-");
XBee.print(code);
XBee.print(",");
XBee.print(BkN);
XBee.print(",");
XBee.print(P1_L);
XBee.print(",");
XBee.print(P1_T);
XBee.print(",");
timeToSd();
dataFile.flush();
digitalWrite(13, LOW); // glow off LED
}
bytesread = 0;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
void readRfidPort3 () { // 3rd Pin (RX = pin 17 & TX = pin 16)
if(Serial2.available() > 0) { // if data available from reader
if((val = Serial2.read()) == 2) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial2.available() > 0) {
val = Serial2.read();
if((val == 2)||(val == 3)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
//codeToSd();
//timeToSd();
Serial.print("RFID Aviary-Tag: ");
Serial.print(Avy);
Serial.print("-");
Serial.println(code);
Serial.print("No Data: ");
Serial.println(BkN);
Serial.print("Location & Location Type: ");
Serial.print(P2_L);
Serial.print(" | ");
Serial.println(P2_T);
Serial.print("Writing Time to File: ");
digitalWrite(13, HIGH); //indicate it by glowing LED at pin 13
delay(1500); // delay 1.5 seconds
dataFile.print(Avy);
dataFile.print("-");
dataFile.print(code);
dataFile.print(",");
dataFile.print(BkN);
dataFile.print(",");
dataFile.print(P2_L);
dataFile.print(",");
dataFile.print(P2_T);
dataFile.print(",");
XBee.print(Avy);
XBee.print("-");
XBee.print(code);
XBee.print(",");
XBee.print(BkN);
XBee.print(",");
XBee.print(P2_L);
XBee.print(",");
XBee.print(P2_T);
XBee.print(",");
timeToSd();
dataFile.flush();
digitalWrite(13, LOW); // glow off LED
}
bytesread = 0;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
void readRfidPort2() { // 4th Pin (RX = pin 19 & TX = pin 18)
if(Serial1.available() > 0) { // if data available from reader
if((val = Serial1.read()) == 2) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial1.available() > 0) {
val = Serial1.read();
if((val == 2)||(val == 3)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
//codeToSd();
//timeToSd();
Serial.print("RFID Aviary-Tag: ");
Serial.print(Avy);
Serial.print("-");
Serial.println(code);
Serial.print("No Data: ");
Serial.println(BkN);
Serial.print("Location & Location Type: ");
Serial.print(P3_L);
Serial.print(" | ");
Serial.println(P3_T);
Serial.print("Writing Time to File: ");
digitalWrite(13, HIGH); //indicate it by glowing LED at pin 13
delay(1500); // delay 1.5 seconds
dataFile.print(Avy);
dataFile.print("-");
dataFile.print(code);
dataFile.print(",");
dataFile.print(BkN);
dataFile.print(",");
dataFile.print(P3_L);
dataFile.print(",");
dataFile.print(P3_T);
dataFile.print(",");
XBee.print(Avy);
XBee.print("-");
XBee.print(code);
XBee.print(",");
XBee.print(BkN);
XBee.print(",");
XBee.print(P3_L);
XBee.print(",");
XBee.print(P3_T);
XBee.print(",");
timeToSd();
dataFile.flush();
digitalWrite(13, LOW); // glow off LED
}
bytesread = 0;
}
}
}