I'm exchanging data between an Arduino Uno and a Mega via serial, which has worked well, being able to send int and float variables seamlessly. The protocol I use starts and ends a stream with "<" and ">" respectively, separating values with a ",". I'm using a library to separate the values.
Now I connected a MPU6050 accelerometer to the Arduino Uno and need to send the data to the Mega, nonetheless, the ang_x and ang_y variables (containing the tilt angles) always arrive as "nan".
When reading the Serial output of the Uno from the PC, the stream is correct:
<-9.39,6.82,9.99>
But when the stream is sent over to the Mega it is finally printed as:
X: nan Y: nan Float test: 9.99
I checked reading the incoming stream directly and added a float variables with known value to debug the problem. But no luck.
This is the "sending" code on the Arduino Uno:
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
MPU6050 sensor;
int ax, ay, az;
int gx, gy, gz;
long tiempo_prev;
float dt;
float ang_x, ang_y;
float ang_x_prev, ang_y_prev;
float float_test = 9.99;
int num;
void setup() {
Serial.begin(38400);
Wire.begin();
sensor.initialize();
}
void loop() {
//**** Calculates tilt of the accelerometer
sensor.getAcceleration(&ax, &ay, &az);
sensor.getRotation(&gx, &gy, &gz);
float accel_ang_x=atan(ay/sqrt(pow(ax,2) + pow(az,2)))*(180.0/3.14);
float accel_ang_y=atan(-ax/sqrt(pow(ay,2) + pow(az,2)))*(180.0/3.14);
ang_x = 0.98*(ang_x_prev+(gx/131)*dt) + 0.02*accel_ang_x;
ang_y = 0.98*(ang_y_prev+(gy/131)*dt) + 0.02*accel_ang_y;
ang_x_prev=ang_x;
ang_y_prev=ang_y;
//**** Sends result via serial, including start/end markers
Serial.write("<");
Serial.print(ang_x);
Serial.write(",");
Serial.print(ang_y);
Serial.write(",");
Serial.print(float_test);
Serial.write(">");
delay(10);
}
And this is the "receiving code on the Mega:
#include <Separador.h>
Separador s;
/*
String str;
byte receivedByte;
*/
const byte numChars = 32;
char receivedChars[numChars];
String str;
String str_x;
String str_y;
String test;
boolean newData = false;
char rc;
void setup() {
Serial.begin(38400);
Serial1.begin(38400);
}
void loop() {
/*
if (Serial1.available() > 0) {
receivedByte = Serial1.read();
Serial.println(receivedByte);
}
*/
recvBTData();
processRcvdBTData();
}
// **** Searches for start/end markers in the serial stream and stores characters
void recvBTData() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//**** Takes characters identified by recvBTData() and reconstructs original data into Strings
void processRcvdBTData() {
if (newData == true) {
String str(receivedChars);
String str_x = s.separa(str, ',', 0);
String str_y = s.separa(str, ',', 1);
String test = s.separa(str, ',', 2);
//**** Prints out original variables (but can be replaced with whatever operation is needed)
newData = false;
Serial.write("X: ");
Serial.print(str_x);
Serial.write(" Y: ");
Serial.print(str_y);
Serial.write(" Float test: ");
Serial.println(test);
}
}
Thanks for your help.
Best,
Marcel