Some logging strategies use a specific heartbeat message that can be used to correlate the timing of other log entries.hi,
When data logging you must consider how you are going use and analyse the raw data.
For example,
tagging every Depth data message with the Date and Time is not necessary.
To know the position where the depth data was recorded is important.
You are using a GPS device which sends out a $GPS position message every precise 1 second.
So you have a very accurate timing signal for the position, the $GPS string is recorded to the SD card.
The 10 or so Depth messages that are recorded to SD must have been logged during that 1 second GPS period. It does not make any sense to tag them with a Date.Time to mSeconds.
Also if you record the GPS position message and Depths to the same datafile, it makes sense when you start to plot and analyse the logged data.
E
hi,Can I ask you some personal question?
/*
** MEGA MINI **
** MOSI - pin 51
** MISO - pin 50
** CLK - pin 52
** CS - pin 53 (for MKRZero SD: SDCARD_SS_PIN)
** SDA - pin 20
** SCL - pin 21
*/
#include <Wire.h>
#include <EEPROM.h>
#include "math.h"
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
#define DS1307_CTRL_ID 0x68
RTC_DS1307 rtc;
bool flag = false;
String GPSmessage = "";
const int chipSelect = 53;
String dataString = "";
String dataString1 = "";
String dataString2 = "";
String dataString3 = "";
String strID = "";
String strID1 = "";
String strID2 = "";
String strID3 = "";
bool stringComplete = false;
bool stringComplete1 = false;
bool stringComplete2 = false;
bool stringComplete3 = false;
unsigned long msec;
const int ledPin = LED_BUILTIN;
int ledState = LOW;
const long interval = 500;
unsigned long CurrentBaudRate = 19200;
unsigned long x = 0;
void setup() {
//============= SD Card ===============//
Serial.println("Initializing SD card...");
// see if the card is not present:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("SD card Ready");
//============= RTC ===============//
if (!rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// attachInterrupt (0, handleInt, RISING);
// setSQW(0x10);
// //delay (2000);
//============= flush serial input buffer ===============//
while (Serial.available() > 0) {
char t = Serial.read();
}
while (Serial1.available() > 0) {
char t = Serial1.read();
}
// flush serial2 inp buffer
while (Serial2.available() > 0) {
char t = Serial2.read();
}
// flush serial3 inp buffer
while (Serial3.available() > 0) {
char t = Serial3.read();
}
while (!Serial) { }// wait for serial port to connect
Serial.begin(CurrentBaudRate);
Serial1.begin(CurrentBaudRate);
Serial2.begin(CurrentBaudRate);
Serial3.begin(CurrentBaudRate);
Serial.println(CurrentBaudRate);
}
byte i, second, minute, hour;
void loop() {
// ===== Reading Date and Time from RTC ============//
DateTime now = rtc.now();
char dt[16];
char tm[16];
uint8_t thisSec, thisMin, thisHour;
thisSec = now.second();
thisMin = now.minute();
thisHour = now.hour();
sprintf(dt, "%02d-%02d-%02d", now.year(), now.month(), now.day());
sprintf(tm, "%02d%02d%02d", now.hour(), now.minute(), now.second());
//=============== Serial0======================//
if (stringComplete) {
strID = (dataString.substring(0, 2));
if (strID == "$G") {
Serial.print(dataString);
File dataFile = SD.open("s0gpslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.print(",");
dataFile.print(dataString);
dataFile.close();
}
}
else {
Serial.print(dataString);
File dataFile = SD.open("s0eslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.print(",");
dataFile.print(dataString);
dataFile.close();
}
}
dataString = ""; // clear the string:
stringComplete = false;
strID = "";
}
//================Serial 1=======================//
if (stringComplete1) {
strID1 = (dataString1.substring(0, 2));
if (strID1 == "$G")
{
Serial.print(dataString1);
File dataFile = SD.open("s1gpslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.print(",");
dataFile.print(dataString1);
dataFile.close();
}
else {
File dataFile = SD.open("s1eslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.print(",");
dataFile.print(dataString1);
dataFile.close();
}
}
dataString1 = ""; // clear the string:
stringComplete1 = false;
strID1 = "";
}
//==============Serial 2======================//
if (stringComplete2) {
strID2 = (dataString2.substring(0, 2));
if (strID2 == "$G") {
Serial.print(dataString2);
File dataFile = SD.open("s2gpslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.print(",");
dataFile.println(dataString2);
dataFile.close();
}
}
else {
File dataFile = SD.open("s2eslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.print(",");
dataFile.print(dataString2);
dataFile.close();
}
}
dataString2 = ""; // clear the string:
stringComplete2 = false;
strID2 = "";
}
//===================Serial 3========================//
if (stringComplete3) {
strID3 = (dataString3.substring(0, 2));
if (strID3 == "$G") {
Serial.print(dataString3);
File dataFile = SD.open("s3gpslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.print(",");
dataFile.print(dataString3);
dataFile.close();
}
}
else {
Serial.print(dataString3);
File dataFile = SD.open("s3eslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.print(",");
dataFile.print(dataString3);
dataFile.close();
}
}
dataString3 = ""; // clear the string:
stringComplete3 = false;
strID3 = "";
}
}
}
void serialEvent() {
// if (flag == false) {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the dataString:
dataString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
//}
void serialEvent1() {
while (Serial1.available()) {
// get the new byte:
char inChar = (char)Serial1.read();
// add it to the dataString:
dataString1 += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n' || '\r') {
stringComplete1 = true;
}
}
}
void serialEvent2() {
// if (flag == false) {
while (Serial2.available()) {
// get the new byte:
char inChar = (char)Serial2.read();
// add it to the dataString:
dataString2 += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete2 = true;
// Serial.print(dataString2);
}
}
}
//}
void serialEvent3() {
while (Serial3.available()) {
// get the new byte:
char inChar = (char)Serial3.read();
// add it to the dataString:
dataString3 += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete3 = true;
}
}
}
Absolutely fine here too Sir.All ok here, pleased to hear all is ok with you.
/*
** MEGA MINI **
** MOSI - pin 51
** MISO - pin 50
** CLK - pin 52
** CS - pin 53 (for MKRZero SD: SDCARD_SS_PIN)
** SDA - pin 20
** SCL - pin 21
*/
#include <Wire.h>
#include <EEPROM.h>
#include "math.h"
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
#define DS1307_CTRL_ID 0x68
RTC_DS1307 rtc;
unsigned long msec;
#include <TimerOne.h>
#define RxTimeOut 5
String String1;
String inputString1 = ""; // a String to hold incoming data
boolean NewDataFlag1=0;
unsigned int Serial1TimeOut=0;
bool string1Complete = false;
String String2;
String inputString2 = ""; // a String to hold incoming data
boolean NewDataFlag2=0;
unsigned int Serial2TimeOut=0;
const int chipSelect = 53;
String strID = "";
void timerIsr()
{
serial1Event();
if(Serial1TimeOut>0)Serial1TimeOut--;
if(Serial1TimeOut==0 && NewDataFlag1==1)
{
String1=inputString1;
inputString1="";
NewDataFlag1=0;
}
if(Serial2TimeOut>0)Serial2TimeOut--;
if(Serial2TimeOut==0 && NewDataFlag2==1)
{
String2=inputString2;
inputString2="";
NewDataFlag2=0;
}
}
void setup() {
Serial.begin(115200);
Serial1.begin(19200);
Serial2.begin(19200);
inputString1.reserve(200);
inputString2.reserve(200);
Timer1.initialize(10000); // Timer event time
Timer1.attachInterrupt( timerIsr );
Serial.println("READY");
//========================================= SD Card =======================================================//
// Serial.println("Initializing SD card...");
// see if the card is not present:
if (!SD.begin(chipSelect)) {
// Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
// Serial.println("SD card Ready");
//====================================================== RTC =============================================//
if (!rtc.begin())
{
// Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!rtc.isrunning()) {
// Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
String String1A,String1B,String1C,String1D,String1E;
int CR1A, CR1B, CR1C, CR1D;
int CR2;
int CR3;
int CR4;
int CR5;
//---------------------------------------------
void loop() {
DateTime now = rtc.now();
char dt[16];
char tm[16];
char buffer [16];
uint8_t thisSec, thisMin, thisHour;
thisSec = now.second();
thisMin = now.minute();
thisHour = now.hour();
sprintf(dt, "%02d-%02d-%02d", now.year(), now.month(), now.day());
sprintf(tm, "%02d%02d%02d", now.hour(), now.minute(), now.second());
sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec);
msec = millis() % 1000; //TO USE LAST 3 DIGITS OF MILLIS OF ARDUINO
//SERIAL_PORT_1
if (string1Complete == true)
{
CR1A = String1.indexOf(13); //finds location of first
String1A = String1.substring(0, CR1A); //captures first data String
CR1B = String1.indexOf(13, CR1A + 2 ); //finds location of second ,
String1B = String1.substring(CR1A + 2, CR1B); //captures second data String
CR1C = String1.indexOf(13, CR1B + 2 );
String1C = String1.substring(CR1B + 2, CR1C);
CR1D = String1.indexOf(13, CR1C + 2 );
String1D = String1.substring(CR1C + 2, CR1D);
// CR1E = String1.indexOf(13, CR1D + 2 );
// String1E = String1.substring(CR1D + 2, CR1E);
// CR1F = String1.indexOf(13, CR1E + 2 );
// String1F = String1.substring(CR1E + 2, CR1F);
// CR1G = String1.indexOf(13, CR1F + 2 );
// String1G = String1.substring(CR1F + 2, CR1G);
// Serial.println(" ----------- GROUP 1--------------");
Serial.println(String1A);
Serial.println(String1B);
Serial.println(String1C);
Serial.println(String1D);
// Serial.println(String1E);
// Serial.println(String1F);
// Serial.println(String1G);
strID = (String1.substring(0, 2));
if (strID == "$G") {
File dataFile = SD.open("SERIAL_1.txt", FILE_WRITE);
// if the file is available, write to it:
dataFile.print(dt);
dataFile.print(" ");
dataFile.print(tm);
dataFile.println(msec);
dataFile.println(String1A);
dataFile.println(String1B);
// if (strID == "$G") {
dataFile.println(String1C);
dataFile.close();
}
string1Complete = false;
}
}
//-----------------------------------------------------------------------------------------------------------------
void serial1Event() {
while (Serial1.available()) {
char inChar1 = (char)Serial1.read();
inputString1 += inChar1;
Serial1TimeOut = RxTimeOut;
NewDataFlag1 = 1;
if (NewDataFlag1 == 1) {
string1Complete = true;
}
}
}
It means I am getting 6 messages at a frequency of 5Hz and 1 message at a 2Hz frequency. So the total will be 32Hz. Please keep in my that I am only using 1 port (Rx1 of atmega2560) and getting messages from one GPS.What does this mean: it logs data perfectly even up to 32Hz.
by Jake Hertz
by Don Wilcher
by Duane Benson