Hi,
I'm having a strange issue with my Arduino Mega receiving data over Bluetooth. I have an Arduino Mega with a Bluetooth HC-05 module wired to Serial1 and serial monitor running over the default Serial channel. The HC-05 is talking to a Bluetooth enabled ELM327 module. I set up my initial code to send a simple AT command to retrieve the voltage from the ELM327 and display it via the serial monitor.
The initial data is received and displayed without issue, so I started to expand to include additional detail, so the RTC was added and time retrieved and displayed to serial monitor. The output was nice and clean:
Starting Serial 2 - OK
Initialising RTC - Success
12.3V
20181116074526,12.3V
12.3V
20181116074528,12.3V
12.3V
20181116074529,12.3V
I then started to code up the SD interface to record the data at which point, the data displayed both in the serial monitor and what was written to the SD card was mangled slightly. See the attached image as cut/paste seems to clean it up.
I trimmed the code back, and it appears to be related to the line:
pinMode(10,OUTPUT);
Without initialising SD, or even changing it to an unused pin in the 30's range, when the pinMode command is issued I get junk back. I've followed several threads in the forums and tried setting the pinMode for Serial1, I've also tried "pulling the pin up" according to a different thread, I've also moved between different Serial interfaces as well, all to no avail. If I use a simple sketch that allows me to send/receive through the monitor to the bluetooth, if I set pinMode, no issues, so I know I'm missing something, but I'm not sure what.
I apologise if this one has been answered before, but I found nothing in my attempts to nut this out.
Please find the full code excerpt below:
/*********************
* Include libraries *
*********************/
#include <PCD8544.h> // LCD library
#include "SD.h" // SD Card - ADA Fruit library
#include <SPI.h> // SD Card
#include <RTClib.h> // Real Time Clock - ADA Fruit library
#include <Wire.h> // Needed by RTC
/***************************
* Define global variables *
***************************/
#define SerialBaud 9600 // Baud rate of Standard Serial interface
#define Serial1Baud 38400 // Baud rate of secondary serial interface - assumes Mega
String _voltagedata;
RTC_DS1307 rtc; // Real Time Clock
/****************
* Declare Pins *
****************/
/*********************
* Declare variables *
*********************/
void setup() {
//pinMode(10,OUTPUT);
// Set up standard serial
Serial.begin(SerialBaud);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
Serial.println(" ");
// Initialise Bluetooth
Serial.print("Starting Serial 2 - ");
Serial2.begin(Serial1Baud);
Serial2.flush();
Serial.println("OK");
// initialise RTC
Serial.print("Initialising RTC");
if (! rtc.begin())
{
Serial.println("Couldnt find RTC");
while (1);
}
else
{
Serial.println(" - Success");
}
}
void loop() {
_voltagedata = GetVoltage();
String _time = GetTime();
Serial.print(_time);
Serial.print(",");
Serial.println(_voltagedata);
delay(1000);
}
/*****************
* Fetch Voltage *
*****************/
String GetVoltage()
{
char _receivedchar = ' ';
char _receiveddata[50];
int _counter;
// Send command to bluetooth
String _command = "atrv\r";
Serial2.print(_command);
Serial2.flush();
// wait for initial data feed
while (!Serial2.available());
_counter=0;
bool _alldatareceived = false;
bool _toolong = false;
unsigned long _time = millis();
while ((!_alldatareceived) && (!_toolong))
{
while (Serial2.available())
{
_receivedchar = Serial2.read();
if ((_counter < 20))
{
_receiveddata[_counter] = _receivedchar;
}
_counter=_counter+1;
if (_receivedchar == 62)
{
_alldatareceived = true;
}
}
// Check if waiting for data taking too long
if ((millis() - _time) >= 2000)
{
_toolong = true;
}
}
// check returned data integrity
bool _dataok = true;
for (int i=0;i<_command.length();i++)
{
if (_command[i] != _receiveddata[i])
{
_dataok = false;
}
}
// Get data
char _voltage[5];
if ((_dataok) && (!_toolong))
{
_voltage[0] = _receiveddata[5];
_voltage[1] = _receiveddata[6];
_voltage[2] = _receiveddata[7];
_voltage[3] = _receiveddata[8];
_voltage[4] = _receiveddata[9];
}
else
{
_voltage[0] = 70;
_voltage[1] = 65;
_voltage[2] = 73;
_voltage[3] = 76;
_voltage[4] = 33;
}
// Return data
Serial.println(_voltage);
return _voltage;
}
/************
* Get Time *
************/
String GetTime()
{
// Get Time
DateTime now = rtc.now();
String currenttime;
currenttime = String(now.year());
if (now.month() < 10)
{
currenttime = currenttime + "0" + String(now.month());
}
else
{
currenttime = currenttime + String(now.month());
}
if (now.day() < 10)
{
currenttime = currenttime + "0" + String(now.day());
}
else
{
currenttime = currenttime + String(now.day());
}
if (now.hour() < 10)
{
currenttime = currenttime + "0" + String(now.hour());
}
else
{
currenttime = currenttime + String(now.hour());
}
if (now.minute() < 10)
{
currenttime = currenttime + "0" + String(now.minute());
}
else
{
currenttime = currenttime + String(now.minute());
}
if (now.second() < 10)
{
currenttime = currenttime + "0" + String(now.second());
}
else
{
currenttime = currenttime + String(now.second());
}
return currenttime;
}
Thanks heaps for your help.