Problem communicating sensors data to PC

holmes4:
I think your problem is in this line

inputString += inChar;

in serialEvent3().

You need to use an array of char and not a String!

String has a bug which makes a mess of the heap and then makes a mess of the ram!

Here is a bit of code which (I think - I can't test it) should fix things. It does build.

volatile char inputString[256]; // is 256 bytes big enough to hold the whole GPS response?

volatile int i = 0;
volatile boolean stringComplete = false;

void serialEvent3() {
  while (Serial3.available()) {
    char inChar = (char)Serial3.read();
    inputString[i] = inChar;
    i++;
    if (inChar == '\n') {
      stringComplete = true;
      inputString[i] ='/0';
    }
  }
}




You need to set i to 0 when you set stingComplete = false in loop().

As far as I could work serialEvent is, if not actually an ISR, it must be called from one. So any thing it changes needs to be volatile.

If this does not work try renaming serialEvent3() and then calling it from loop(). 

Mark

Hi guys,

This solved my problem, i just had to make small changes, now it works as i wanted.
i removed the volatile declaration and, added one code line for emptying the char array, and one for reinitializing the i counter. Thats it!

Thanks for your help!

int raw_values[11];
float ypr[3]; // yaw pitch roll
float ypr2[3]; // yaw pitch roll

char inputString[256]; // is 256 bytes big enough to hold the whole GPS response?
int i = 0;
boolean stringComplete = false;

float acc;
boolean paso;

FreeIMU my3IMU = FreeIMU();
FreeIMU my3IMU2 = FreeIMU();


void setup() { 
  Serial.begin(115200);
  Serial3.begin(57600); //Set Mega hardware port baud to match GPS default baud
  delay(20);
  Serial3.println("$PMTK300,100,0,0,0,0*2C"); //configurar a 10hz
  Wire.begin();
  pinMode(2, INPUT); 
  my3IMU.init(); // the parameter enable or disable fast mode
  my3IMU2.init(0x69, false); 
}

void loop() { 
  my3IMU.getYawPitchRoll(ypr);
  my3IMU2.getYawPitchRoll(ypr2);
  my3IMU.getRawValues(raw_values); // *******************************
  paso=digitalRead(2);
  acc=sqrt((float(raw_values[0])*float(raw_values[0]))+(float(raw_values[1])*float(raw_values[1]))+(float(raw_values[2])*float(raw_values[2])));
  
  Serial.print(ypr[0]);          //Yaw Imu 1
  Serial.print(',');
  Serial.print(ypr[1]);          //Pitch Imu 1
  Serial.print(',');
  Serial.print(paso);            //"Step detection"
  Serial.print(',');
  Serial.print(ypr2[1]);         //Pitch Imu 2
  Serial.print(',');
  Serial.print(acc);             //Aceleracion
  Serial.print(',');
  if (stringComplete) {
    Serial.print(inputString); 
    inputString[0] = '\0';
    i=0;
    stringComplete = false;
  }else {
  Serial.println("");
  }

}

void serialEvent3() {
  while (Serial3.available()) {
    char inChar = (char)Serial3.read(); 
    inputString[i] = inChar;
    i++;
    if (inChar == '\n') {
      stringComplete = true;
      inputString[i] ='\0';
    } 
  }
}