I am having two Teensy 4.1 (lets call them T1 and T2) setup in such a way that T1 reads data from the sensors and sends them at 100 Hz to T2 via Serial 5. Because I have control over the string sent by T1, I also send out a '\r' in the serial to let T2 know that a full string has been received. This is working fine as I want it to.
The problem comes when I am trying to store the data received by T2 into the SD card. I am also naming the file based on the date provided by my RTC module and also index the file based on the number of lines I have stored in the file. What happens is that now the serial data I am receiving gets garbled and T2 keeps restarting. In addition, the file names in the SD card are garbled as well.
Here is a screen shot of the serial monitor
Here is a screen shot of the file name in the SD card
I have even tried storing the serial data with a constant file name instead, and even that isn't changing anything. If I comment out the parts involving SD card, the received serial data in T2 is exactly as sent by T1.
Would anyone know what is causing my problems or if there is a better way to the SD write? Below is the code that i am using for T2:
The problem comes when I am trying to store the data received by T2 into the SD card. I am also naming the file based on the date provided by my RTC module and also index the file based on the number of lines I have stored in the file. What happens is that now the serial data I am receiving gets garbled and T2 keeps restarting. In addition, the file names in the SD card are garbled as well.
Here is a screen shot of the serial monitor
Here is a screen shot of the file name in the SD card
I have even tried storing the serial data with a constant file name instead, and even that isn't changing anything. If I comment out the parts involving SD card, the received serial data in T2 is exactly as sent by T1.
Would anyone know what is causing my problems or if there is a better way to the SD write? Below is the code that i am using for T2:
Code:
#include <SD.h>
#include <RTClibw1.h>
#include <SPI.h>
#include <Wire.h>
//SD CARD
const int chipSelect = BUILTIN_SDCARD;
File dataFile ;
boolean file_w = false;
uint8_t file_idx=0;
static boolean write_flag=false;
char file_name[13];
//Serial read
char rx_arr[90];
static uint8_t ndex=0;
static uint16_t line_count = 0;
//Time for SD File
RTC_DS3231 rtc;
static uint8_t rtcYear, rtcMonth, rtcDay;
#define chip1 Serial5
void setup()
{
Serial.begin(115200);
chip1.begin(115200);
String sktch = __FILE__;
sktch = (sktch.substring((sktch.indexOf("\r")), (sktch.lastIndexOf("\\")) + 1));
Serial.print("Sketch Name: ");
Serial.println(sktch);
Serial.print("Flashed On : ");
Serial.println(F(__DATE__ " " __TIME__));
delay(500);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
while (1) ;
}
Serial.println("SD Card Found.");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(__DATE__, __TIME__));
DateTime now = rtc.now();
rtcDay = now.day();
rtcMonth = now.month();
rtcYear = now.year()%100;
delay(3000);
}
void loop()
{
receive_data();
if (write_flag == true)
record_data();
}
void receive_data()
{
while(chip1.available())
{
char datachar = chip1.read();
rx_arr[ndex] = datachar;
if(datachar=='\r')
{
rx_arr[ndex]='\0';
++line_count;
Serial.println(rx_arr);
write_flag = true;
ndex=0;
if (line_count == 0)
{
file_idx = file_idx+1;
}
break;
}
else
write_flag = false;
ndex = ndex + 1;
}
}
void record_data()
{
DateTime now = rtc.now();
// change file name & index if on a different date
if((rtcDay != now.day()))
{
rtcYear = now.year()%100;
rtcMonth = now.month();
rtcDay = now.day();
file_idx = 0;
}
sprintf(file_name, "%02u%02u%02u_F%03u.txt", rtcYear, rtcMonth, rtcDay, file_idx);
dataFile = SD.open(file_name, FILE_WRITE);
dataFile.println(rx_arr);
dataFile.close();
write_flag=false;
}