How do I fix the spacing in each tab column?

Hey everyone,

I'm trying to print orientation angles from my BNO055 in a list so I can see all x,y,z values in a single column. However, when the number of digits change, the length of that row changes. How can I fix the spacing and also have the decimal point be in the same place ?

Thank you.

  Serial.print(pascals/3377); //Inches(Hg)
  Serial.print("\t\t");
  Serial.print(altm); //meters
  Serial.print("\t\t");
  Serial.print(tempC); //°Celcius
  Serial.print("\t|\t");
  Serial.print(event.orientation.x,4);
  Serial.print("\t\t");
  Serial.print(event.orientation.y,4);
  Serial.print("\t\t");
  Serial.print(event.orientation.z,4);
  Serial.println(); 

Try reading the documentation

That's a problem with how your terminal interprets a tab character.
Use sprintf() to format the string.

Since you didn't post the whole code, I have no clue what types your variables are. But assuming the first three are integers and the last three are doubles (float), here is a start:

char buffer [60];   //Probably much larger than you actually need.
sprintf (buffer, "%8d:%8d:%8d,%8f4:%8f4:%8f4", pascals/3377, altm, tempC, event.orientation.x, event.orientation.y, event.orientation.z);
Serial.println(buffer);
1 Like

Hey Steve,

I tried using that code. It's properly formatted now thanks. Unfortunately, the values aren't showing up. All of the variables are floats. Also, what is the purpose of the '4' after %8f ?

image


#include <Wire.h>
#include <SD.h> //Load SD library
#include <Adafruit_MPL3115A2.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>

// Our weather page presents pressure in Inches (Hg)
// Use http://www.onlineconversion.com/pressure.htm for other units
  
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
Adafruit_BNO055 bno = Adafruit_BNO055(55);
int chipSelect = 4; //chip select pin for the MicroSD Card Adapter
File file; // file object that is used to read and write data

char buffer[60]; //buffer to temporarily store data (for serial.print columns)

void setup() {
  //------------------------------------------------------------------------------------------------- put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Adafruit_MPL3115A2 & BNO055 test!");

  pinMode(chipSelect,OUTPUT); //chip select pin must be set to OUTPUT mode

  //------------------------------------------------------------------------------------------------- Initialise the sensors and components
  if(!bno.begin()) //!bno.begin() == 1, means its not working
  {
    Serial.println("BNO055    NOT DETECTED !!! Check wiring or I2C ADDR!"); //ADD A LINE OF CODE IN HERE FOR BUZZER NOTIFICATION
    while(1); //don't do anything else if its still not working
    
  } else if (!baro.begin() == 1) {
    Serial.println("MPL3115A2 NOT DETECTED !!! Check wiring or I2C ADDR!");
    while(1);
    
  } else if (!SD.begin(chipSelect)){
    Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
    
  } else {
    Serial.println("Flight Data Recorder is GO for launch");
  }

  //------------------------------------------------------------------------------------------------- File management in MicroSD card
  if (SD.exists("file.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    
    if (SD.remove("file.txt") == true) {
      Serial.println("Successfully removed file.");
      file = SD.open("file.txt", FILE_WRITE); // open "file.txt" to write data
      
    } else {
      Serial.println("Could not removed file.");
    }
  }

  

  
  delay(1000);
  //bno.setExtCrystalUse(true);
  
}

void loop() {
  // put your main code here, to run repeatedly:

  //------------------------------------------------------------------------------------------------- GET DATA FROM MPL3115A2
  float pascals = baro.getPressure();
  float altm = baro.getAltitude();
  float tempC = baro.getTemperature();

  //------------------------------------------------------------------------------------------------- GET DATA FROM BNO055
  sensors_event_t event; 
  bno.getEvent(&event);

  //------------------------------------------------------------------------------------------------- PRINT DATA TO SERIAL MONITOR
  
  sprintf (buffer, "%8f:%8f:%8f,%8f4:%8f4:%8f4", pascals/3377, altm, tempC, event.orientation.x, event.orientation.y, event.orientation.z);
  Serial.println(buffer);

  Serial.println(); 
}

The IDE does not support the float data type in sprintf(). There is a way to enable floats in sprintf(), but I don't know where to find it. Maybe a search of this forum will turn it up.

The alternative is to use the dtostrf() function to convert the floats to strings and use the %s placeholder in sprintf to print them.

What Arduino board are you using?

Read the docs.

The 4 means four decimal places.

But, since I haven't used floats (in Arduino a float is a double) in an sprintf() call, I forget that %8f4 won't work.

So use the tip provided by GroundFungus to make them strings then print the strings using "%8s" in the sprintf() function.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.