Hi All.
I am trying to receive Serial input from Bluetooth, read it, print it to the Serial monitor and then write to an SD File.
The hardware is a Due, attached to a BT receiver, and with an Adafruit Datalogger on top.
The Bluetooth input is available - no problem with that. I can read it. But, after engaging the Bluetooth and when the programs attempts to Serial print, the program hangs.
During the compilation and loading the Bluetooth is switched off, so there is no Bluetooth input. The setup runs and prints is log to the monitor. Next, I engage the Bluetooth. That's when the clash begins.
Here is the Serial output. I have added debug comments. The comments show where the serial printing stopped!
Edit. Note: Bluetooth input is to pins 0 and 1.
Sketch: /home/graham/monitoring/ArduinoUno/HRV_shells/HR_sym_receiving_due_skinny_recv/HR_sym_receiving_due_skinny_recv.ino
Uploaded: Aug 29 2018
File: 08292101.csv
Setup() completed.
Debug: Serial.available() = OK
Debu <<<< == this is where it hangs
I believe the Serial.print (output) and BT Serial (input) are clashing. They are perhaps using the same pins for input and output?
What could the problem be?
How can I solve it?
Below is my program.
Look forward to your replies
Thanks
EGB
=======================
(The program has been cut down to remove clutter!)
#include <SPI.h>
#include <SD.h>
// ------------------------------------
// -- Inventory of Pins ---------------
// ------------------------------------
// Pin 10 set OUTPUT/HIGH for initialization - of SDCard?; other pins for control by SPI
const int chipSelectPin = 10; // 10 only for AdaFruit DataLogger
const int W5500_SS = 4; // 4 or 7; ? chip select for AdaFruit DataLogger
const int dataReadyPin = 6;
// ------------------------------------
// SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
// ------------------------------------
// Used in void do_receiving_timestamp():
String timestampString;
String readString;
char c;
// ------------------------------------
// Used in void recv_with_end_marker():
char endMarker = '\n';
// ------------------------------------
// DataLogger SD shield
File targetFile;
String outputFile;
// DateTime now;
char file_name_buffer[8];
String fullString = "Recvr_time,Sendr_time_Ch1,Ch2,Ch3";
// ------------------------------------
// Setting time
String string_rtc;
int hh; int mm; int ss;
int i; int j;
char s_month[5];
int year, month, day;
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
// <-- __DATE__ to YYYY MM DD --------------------
void set_rtc_yyyy_month_day(char const *time) {
sscanf(time, "%s %d %d", s_month, &day, &year);
month = (strstr(month_names, s_month)-month_names)/3;
month = month + 1;
}
// <-- Setting Date and Time --------------------
void set_rtc_hh_mm_ss() {
// Because of bugs in RTC.adjust()
string_rtc = String(__TIME__);
char HH[3]; char MM[3]; char SS[3];
for ( i = 0; i < 2; i++) {
HH[i] = string_rtc.charAt(i);
HH[i + 1] = '\0';
}
hh = atoi(HH);
for ( i = 0; i < 2; i++) {
j = i + 3;
MM[i] = string_rtc.charAt(j);
MM[i + 1] = '\0';
}
mm = atoi(MM);
for ( i = 0; i < 2; i++) {
j = i + 6;
SS[i] = string_rtc.charAt(j);
SS[i + 1] = '\0';
}
ss = atoi(SS);
}
// <-- Creating file name --------------------
void create_file_name() {
set_rtc_hh_mm_ss();
set_rtc_yyyy_month_day(__DATE__);
sprintf(file_name_buffer,"%02d%02d%02d%02d", month, day, hh, mm );
outputFile = String(file_name_buffer);
outputFile += ".csv";
Serial.println("File: " + outputFile);
}
// <-- Marking Time of Receipt --------------------
void do_receiving_timestamp() {
set_rtc_hh_mm_ss();
set_rtc_yyyy_month_day(__DATE__);
sprintf(file_name_buffer,"%02d/%02d/%02d:%02d:%02d:%02d", year, month, day, hh, mm, ss );
//interrupt_seq_number++;
//fullString = String(non_volatile_time_of_interrupt);
fullString += (",interrupt:");
fullString += (file_name_buffer);
fullString += (",");
//fullString += String(interrupt_seq_number);
}
// <-- Receiving BT input --------------------
void recv_with_end_marker() {
do_receiving_timestamp();
readString = Serial.readStringUntil(endMarker);
fullString = timestampString;
fullString += ",";
fullString += readString;
}
// <-- Writing to STD Output --------------------
void do_serial_write() {
j = fullString.length();
for (i = 0; i < j; i++) {
Serial.print(fullString.charAt(i));
}
Serial.write("\n");
}
// <-- Checking SD access Pin --------------------
void Check_SD_access_pin() {
if (SD.begin(chipSelectPin)) {
Serial.println("SDCard init OK.\n");
} else {
Serial.println("FAIL 3: init SDCard");
return;
}
Serial.println("Header to " + outputFile);
targetFile = SD.open(outputFile, FILE_WRITE);
if (targetFile) {
targetFile.println(fullString);
} else {
Serial.println("FAIL 4: opening File");
}
targetFile.close();
}
// <-------------------------------------------------
// <-- Set up process -------------------------------
// <-------------------------------------------------
void setup() {
//noInterrupts(); // Disable all interrupts before initialization
Serial.begin(115200);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
pinMode(dataReadyPin, INPUT);
pinMode(W5500_SS, OUTPUT);
digitalWrite(W5500_SS, HIGH); // davekw7x: If it's low, the Wiznet chip corrupts the SPI bus
delay(100);
SPI.begin();
delay(100);
create_file_name();
// < ---- SDCard present ------------>
Serial.println("\nSetup() completed.\n");
}
// <-------------------------------------------------
// <-- On-going Loop --------------------------------
// <-------------------------------------------------
void loop() {
if (Serial.available() > 0 ) {
Serial.println("Debug: Serial.available() = OK");
recv_with_end_marker();
Serial.println("Debug: recv_with_end_marker() = OK");
do_serial_write();
Serial.println("Debug: do_serial_write() = OK");
}
}