Sometimes the Data Logger stops logging data after working for a little while (i.e. the red light stops flashing when the RFID detects a bird and is no longer writing anything to the SD card). If I turn the power off and on it starts working normally. The design works great on the nests, but the birds do not swarm the nests like they do our foraging arrays. I am thinking it might be caused by too many birds present on the arrays but I have not had much success replicating the issue myself so I am not convinced.
I am hoping someone might have an idea of what is causing the issue. Below is an example of the code I use to program the Mega's to track the nests, and below that is the code I use to set up the real-time clock (although I am not sure that is the issue since it's almost a carbon copy of the Adafruit code).
Let me know if you have any ideas or have any questions.
RFID CODE
Code: Select all
#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[11];
int bytesread = 0;
File myFile;
RTC_DS1307 RTC;
SoftwareSerial XBee(2, 3);
// VALUES TO CHANGE //////////////////////////////////////////
//////////////////////////////////////////////////////////////
//New Block Marker// - DO NOT TOUCH
char NewD[]="NewDataBlock";
//Experiment// CHANGE FOR EXPERIMENT
char Avy[]="NESTS"; //ex. NESTS or STINGRAY
//Empty Data Field//
char BkN[]="DARW"; //ex. ="N/A";
//Location IDs// UNIQUE TO ARDUINO
char P0_L[]="Loca_01"; //Location 0 Name
char P1_L[]="Loca_02"; //Location 1 Name (ex. Nest_1)
char P2_L[]="Loca_03"; //Location 2 Name (ex. Nest_2)
char P3_L[]="Loca_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: "
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
//////////////////////////////////////////////////////////////
// Code for intializing the unit & SD card ///////////////////
//////////////////////////////////////////////////////////////
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(",");
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(",");
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(",");
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(",");
timeToSd();
}
}
//////////////////////////////////////////////////////////////
//Logging the RFID tags///////////////////////////////////////
//////////////////////////////////////////////////////////////
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(Avy);
Serial.print("-");
Serial.print(code);
Serial.print(",");
Serial.print(BkN);
Serial.print(",");
Serial.print(P0_L);
Serial.print(",");
Serial.print(P0_T);
Serial.print(",");
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(",");
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(Avy);
Serial.print("-");
Serial.print(code);
Serial.print(",");
Serial.print(BkN);
Serial.print(",");
Serial.print(P1_L);
Serial.print(",");
Serial.print(P1_T);
Serial.print(",");
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(",");
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(Avy);
Serial.print("-");
Serial.print(code);
Serial.print(",");
Serial.print(BkN);
Serial.print(",");
Serial.print(P2_L);
Serial.print(",");
Serial.print(P2_T);
Serial.print(",");
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(",");
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(Avy);
Serial.print("-");
Serial.print(code);
Serial.print(",");
Serial.print(BkN);
Serial.print(",");
Serial.print(P3_L);
Serial.print(",");
Serial.print(P3_T);
Serial.print(",");
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(",");
timeToSd();
dataFile.flush();
digitalWrite(13, LOW); // glow off LED
}
bytesread = 0;
}
}
}
Code: Select all
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// uncomment it & upload to set the time, date and start run the RTC!
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since 1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}