Array doesn't show up all the values

Board: ESP32 - Sparkfun Thing

Hello guys,
I have a problem with filling an array with values I get from the accelerometer (via I2C) - yaw, pitch, roll. I want to fill an array and eventually put between them some delimiter so I can split them in android app (AppInventor) afterward. Data is being sent by BLE to android device as I mentioned.

The array now outputs the values but sometimes it shows only one of the coordinates and the other time it shows them all and even with the text which corresponds to the name of BLE device I gave it in the upper part of the code (didn’t mention there.)

I have trimmed the code the area of interest.

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "MPU9250.h"

MPU9250 IMU(Wire,0x68);

BLECharacteristic *pCharacteristic;

double coordArray[3];
double roll, pitch, yaw;

void setup {

 BLEDevice::init("SMART HELMET"); // Give it a name

// Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
  pCharacteristic->addDescriptor(new BLE2902());

// Start the service

  // Start advertising
  Serial.println("Waiting a client connection to notify...");


void loop {

   float accelX = IMU.getAccelX_mss();
   float accelY = IMU.getAccelY_mss();
   float accelZ = IMU.getAccelZ_mss();
   float gyroX = IMU.getGyroX_rads()/57.3;
   float gyroY = IMU.getGyroY_rads()/57.3;
   float gyroZ = IMU.getGyroZ_rads()/57.3;
   float magX = IMU.getMagX_uT();
   float magY = IMU.getMagY_uT();
   float magZ = IMU.getMagZ_uT(); 

   pitch = atan2 (accelY ,( sqrt ((accelX * accelX) + (accelZ * accelZ))));
   roll = atan2(-accelY ,accelZ);

   // yaw from mag
   float Yh = (magY * cos(roll)) - (magZ * sin(roll));
   float Xh = (magX * cos(pitch))+(magY * sin(roll)*sin(pitch)) + (magZ * cos(roll) * sin(pitch));
   yaw =  atan2(Yh, Xh);
   roll = roll*57.3;
   pitch = pitch*57.3;
   yaw = yaw*57.3;     // in degrees /Angle relative to north
   if (yaw < 0.1){
     yaw = yaw + 360;  // in degrees /relative to north

 result(yaw, pitch, roll);

   char txString[32]; 

   for (unsigned int i=0; i<3; i++){
  char tamp[20];
 dtostrf(coordArray[i], 4, 2, tamp); //convert one of your coordinates
  for (unsigned int j=0;j<6;j++) {txString[6*i+j] = tamp[j];} //concatenate it inside txString

 pCharacteristic->setValue(txString);  //Notifying via BLE - this is reason why i concateante the values to String


Serial.println("Yaw: ");
    Serial.println(",  Pitch: ");
    Serial.println(",  Roll: ");


void result (float x, float y, float z)
  coordArray[0] = x;
  coordArray[1] = y;
  coordArray[2] = z;

11:14:41.685 -> Yaw: 
11:14:41.685 -> 317.34
11:14:41.685 -> ,  Pitch: 
11:14:41.685 -> 0.45
11:14:41.685 -> ,  Roll: 
11:14:41.685 -> -179.56
11:14:41.685 -> *****txString****
11:14:41.685 -> 317.340.45
11:14:41.685 -> ************

11:14:40.563 -> Yaw: 
11:14:40.563 -> 287.08
11:14:40.563 -> ,  Pitch: 
11:14:40.563 -> -18.24
11:14:40.563 -> ,  Roll: 
11:14:40.563 -> 160.78
11:14:40.563 -> *****txString****
11:14:40.563 -> 287.08-18.24160.78ART HELMET  //name of a BLE device [i]SMART HELMET[/i]
11:14:40.563 -> ************

I’m sorry for an unprofessional naming of my problem but I’m a beginner in coding, please be patient with me, thank you for understanding and any advice you could provide.

UPDATE: Code updated. Comments added.

Much better to post a complete working program - ideally the minimum needed to demonstrate the problem. In your snippet there is no indication of where the values come from.

Why are you trying to concatenate the values. It would be much simpler to print them like this

Serial.print(", ");
Serial.print(", ");


Hello, I updated the code so it's more understandable.

Regarding the concatenation, I try to notify via BLE that's why I want to concatenate the values.

There is no excuse for posting such piss-poorly indented code. Put EVERY { on a line BY ITSELF. Put EVERY } on a line BY ITSELF. Use Tools + Auto Format to properly indent your code.

Before you use txString, prove that it contains exactly what you think it does, and NOTHING more or less. Show us the proof.

borisak: Regarding the concatenation, I try to notify via BLE that's why I want to concatenate the values.

There is no need to concatenate values before sending. The output of these two pieces of code will appear identical to the receiving device

Serial.println("Hello World");
Serial.print("Hello ");


Um… You are calling

dtostrf(coordArray[i], 4, 2, tamp);

to convert the number to an intermediate tamp string and then you are unconditionally copying the first 6 characters of tamp to the next subsequent portion of txString

for (unsigned int j=0;j<6;j++) {txString[6*i+j] = tamp[j];}

You call it “concatenation”.

But think what will happen if if the first 6 characters of tamp contain a zero-terminator? That zero-terminator will get copied to txString. It will sit in the middle of txString and will act as a terminator for the whole txString.

This is why sometimes you don’t see the rest of txString when you print it - because sometimes you get a shorter tamp and end up with a zero-terminator in the middle of txString.

That is exactly what happened in your example: 0.45 is that shorter value that “smuggled” a zero-terminator into your txString.

Another problem: you failed to zero-terminate your txString after “concatenating” the last bit of information to it. You might get lucky and get a zero-terminator from tamp. Or you might not.

I don’t normally recommend the use of standard strcat - it is a poorly designed function. But in your case it would work fine, if used properly, instead of your “homemade” concatenation technique.

Anyway, if you want to “roll your own” string concatenation, beware of the fact that the input string might terminate sooner that you expect it to.