user27157: Here is my code. It is suppose to read a couple of sensors and print them out in the serial monitor. Any help please? Note that I'm new to object orientated programming.
/* The Blue Box

   Project description:
   Arduino black box flight data recorder
   This projects acts like a little homemade black box. It records data of a flying object.

   Details about the code:
   Altitude is calculated in meters above sea level.
   Records: day of the week, year, month, day, hour, minute, second, time from activation, yaw, pitch, roll, altitude, temperature

   This code is subject to a lot of ameliorations.

   By: xxx
   Date: 2016/08/28

   Extra credits:

   GY-521 library:
   I2Cdev device library code is placed under the MIT license
   Copyright (c) 2012 Jeff Rowberg
   https://github.com/jrowberg/i2cdevlib

   MPL3115A2 library
   Adafruit Industries
   https://github.com/adafruit/Adafruit_MPL3115A2_Library

   SD card library
   Code from example by Tom Igoe
   Included in the Arduino IDE itself

   DS3231 library
   Rinky-Dink Electronics
   http://www.rinkydinkelectronics.com/library.php?id=73
*/

#include <SPI.h>                        //include the SPI library
#include <SD.h>                         //include the SD card library
#include <Wire.h>                       //include I2C library
#include "I2Cdev.h"                     //include another I2C library
#include "Wire.h"                       //include another I2C library (yes, again)
#include "MPU6050_6Axis_MotionApps20.h" //include the MPU6050 library
#include "SparkFunMPL3115A2.h"          //include the MPL3115A2 library
#include <DS3231.h>                     //include the DS3231 library

//==================================================

#define testtest                    //*** COMMENT THIS LINE BEFORE UPLOADING OFFICIAL CODE TO FINAL PROJECT ***

#define chipSelect 10               //pin for the CS/SS
#define led 2                       //led and/or buzzer

volatile bool mpuInterrupt = false; //some random bool

bool ledState = false;              //state of the led and/or buzzer
bool dmpReady = false;              //state of the mpu6050

int yprInt[3];                      //gyro data in a integer because the extra precision of the float is total bullshit
int altitude;                       //altitude above takeoff point in meters
int altitudeCalibration;            //calibration for the altimeter
int temperature;                    //temperature in degrees Celcius

float counter = 0;                  //time counter, a somewhat internal clock that starts when the module starts dataloging

uint8_t mpuIntStatus;               //holds actual interrupt status byte from MPU
uint8_t devStatus;                  //return status after each device operation
uint8_t fifoBuffer[64];             //FIFO storage buffer

uint16_t packetSize;                //expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;                 //count of all bytes currently in FIFO

String day;                         //day of the week
String ymd;                         //date: year,month,day
String hms;                         //time: hour,minute,second

MPU6050 mpu;                        //change mpu's adress
Quaternion q;                       //quaternion container
VectorFloat gravity;                //gravity vector

MPL3115A2 myPressure;               //MPL3115A2 object declaration as myPressure
DS3231  rtc(A0, A1);                //DS3231 object declaration as

void dmpDataReady() {
  mpuInterrupt = true;
}

class MainLoop
{

    unsigned long previousMillis;

  public:
    MainLoop()
    {
      rtc.begin();            //start DS3231

      Wire.begin();
      myPressure.begin();                                         //start MPL3115A2 communication
      myPressure.setModeAltimeter();
      myPressure.setOversampleRate(7);
      myPressure.enableEventFlags();
      altitudeCalibration = myPressure.readAltitude();            //read altitude

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

      TWBR = 24;
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE

      Fastwire::setup(400, true);
#endif

      mpu.initialize();
      devStatus = mpu.dmpInitialize();
      mpu.setXGyroOffset(0);
      mpu.setYGyroOffset(0);
      mpu.setZGyroOffset(0);
      mpu.setZAccelOffset(0);

      if (devStatus == 0)
      {
        mpu.setDMPEnabled(true);
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();
        dmpReady = true;
        packetSize = mpu.dmpGetFIFOPacketSize();
      }

#ifdef testtest

      Serial.begin(115200); //start serial communication
#endif

#ifndef testtest

      if (SD.begin()); ();       //sd card
#endif

    }

    void go(  )
    {
      unsigned long currentMillis = millis();

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

      TWBR = 24;

#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE

      Fastwire::setup(400, true);
#endif

      mpu.initialize();
      devStatus = mpu.dmpInitialize();
      mpu.setXGyroOffset(0);
      mpu.setYGyroOffset(0);
      mpu.setZGyroOffset(0);
      mpu.setZAccelOffset(0);

      if (devStatus == 0)
      {
        mpu.setDMPEnabled(true);
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();
        dmpReady = true;
        packetSize = mpu.dmpGetFIFOPacketSize();
      }

      else
      {
        return;
      }

      altitude = myPressure.readAltitude() - altitudeCalibration; //read altitude
      temperature = myPressure.readTemp();                        //read temperature


      day = rtc.getDOWStr();  //get day of the week
      ymd = rtc.getDateStr(); //get day, month, year
      hms = rtc.getTimeStr(); //get hour, minutes, seconds

#ifdef testtest

      Serial.print(day);
      Serial.print(" ");
      Serial.print(ymd);
      Serial.print(" -- ");
      Serial.print(hms);
      Serial.print(", ");
      Serial.print(counter);
      Serial.print(", ");
      Serial.print(yprInt[0]);
      Serial.print(", ");
      Serial.print(yprInt[1]);
      Serial.print(", ");
      Serial.print(yprInt[2]);
      Serial.print(", ");
      Serial.print(altitude);
      Serial.print(" ,");
      Serial.print(temperature);
      Serial.println();
#endif

#ifndef testtest

      if (altitude > 1) //if the device is higher then 1m, then we sstart recording everything
      {
        File dataFile = SD.open("datalog.txt", FILE_WRITE);

        if (dataFile)
        {
          dataFile.print(day);
          dataFile.print(" ");
          dataFile.print(ymd);
          dataFile.print(" -- ");
          dataFile.print(hms);
          dataFile.print(", ");
          dataFile.print(counter);
          dataFile.print(", ");
          dataFile.print(yprInt[0]);
          dataFile.print(", ");
          dataFile.print(yprInt[1]);
          dataFile.print(", ");
          dataFile.print(yprInt[2]);
          dataFile.print(", ");
          dataFile.print(altitude);
          dataFile.print(" ,");
          dataFile.print(temperature);
          dataFile.println();
          dataFile.close();
        }
      }
#endif

      counter = counter + 0.5;

    }
};

MainLoop main;

void setup() {
}

void loop()  {
  main.go();
}


user10794: 
mind giving a small example. I'm really not familiar with class and constructors

Here is an example of trying to do things in the constructor:
class foo 
{
  public:
  foo () // constructor
    {
    Serial.begin (115200);
    Serial.println ("Hello, world.");
    }
}; // end of class foo

foo bar;  // make an instance of foo

void setup() { }
void loop() { }

On my setup that prints "Hel" and nothing else.

Now if we change things around it works:
class foo 
{
  public:
  void begin ()
    {
    Serial.begin (115200);
    Serial.println ("Hello, world.");
    }
}; // end of class foo

foo bar;  // make an instance of foo

void setup() 
  {
  bar.begin ();
  }

void loop() { }

Now we are doing things in the right order. "Under the hood" the main function provided by the IDE initializes the hardware (by calling init) and then calls setup. Now we can call the begin function in our class.

It would have worked if we created the class instance inside setup because that is after init is called, like this:
class foo 
{
  public:
  foo () // constructor
    {
    Serial.begin (115200);
    Serial.println ("Hello, world.");
    }
}; // end of class foo


void setup() 
  { 
  foo bar;  // make an instance of foo
  }

void loop() { }

However now bar is not a global variable.

What is sometimes done is to make a function to return an instance of the class, which creates it the first time it is called, like this:
class foo 
{
  public:
  foo () // constructor
    {
    Serial.println (F("Hello, world."));
    }
  void doSomething ()
    {
    Serial.println (F("Doing something"));
    }
}; // end of class foo

foo & getFoo ()
  {
  static foo myBar;  // make an instance of foo
  return myBar;
  }  // end of getFoo

void setup() 
  { 
  Serial.begin (115200);
  Serial.println (F("Program starting."));

  foo & bar = getFoo ();
  bar.doSomething ();
  }  // end of setup

void loop() 
  { 
  foo & bar = getFoo ();
  bar.doSomething ();
  delay (500);
  }  // end of loop

Now the function getFoo returns a reference to the instance of foo created as a static variable inside it. Thus, the first time it is called, the instance is created. Subsequent times just return the same instance.

user4143: Move all that code out of the constructor and put it in a .begin() method to call from setup(). You are calling functions before the chip has been initialized and things are being undone.
It is only safe to use a constructor to initialize a few variables and the like. Simply because the call sequence is:

Constructors
System Init
Setup
Repeat forever:
-> Loop

System Init configures the timers, peripherals, IO pins, etc. Things which need to be done before the code in your constructor.

27157: <p>Here is my code. It is suppose to read a couple of sensors and print them out in the serial monitor. Any help please? Note that I'm new to object orientated programming.</p>

<pre><code>/* The Blue Box

   Project description:
   Arduino black box flight data recorder
   This projects acts like a little homemade black box. It records data of a flying object.

   Details about the code:
   Altitude is calculated in meters above sea level.
   Records: day of the week, year, month, day, hour, minute, second, time from activation, yaw, pitch, roll, altitude, temperature

   This code is subject to a lot of ameliorations.

   By: xxx
   Date: 2016/08/28

   Extra credits:

   GY-521 library:
   I2Cdev device library code is placed under the MIT license
   Copyright (c) 2012 Jeff Rowberg
   https://github.com/jrowberg/i2cdevlib

   MPL3115A2 library
   Adafruit Industries
   https://github.com/adafruit/Adafruit_MPL3115A2_Library

   SD card library
   Code from example by Tom Igoe
   Included in the Arduino IDE itself

   DS3231 library
   Rinky-Dink Electronics
   http://www.rinkydinkelectronics.com/library.php?id=73
*/

#include &lt;SPI.h&gt;                        //include the SPI library
#include &lt;SD.h&gt;                         //include the SD card library
#include &lt;Wire.h&gt;                       //include I2C library
#include "I2Cdev.h"                     //include another I2C library
#include "Wire.h"                       //include another I2C library (yes, again)
#include "MPU6050_6Axis_MotionApps20.h" //include the MPU6050 library
#include "SparkFunMPL3115A2.h"          //include the MPL3115A2 library
#include &lt;DS3231.h&gt;                     //include the DS3231 library

//==================================================

#define testtest                    //*** COMMENT THIS LINE BEFORE UPLOADING OFFICIAL CODE TO FINAL PROJECT ***

#define chipSelect 10               //pin for the CS/SS
#define led 2                       //led and/or buzzer

volatile bool mpuInterrupt = false; //some random bool

bool ledState = false;              //state of the led and/or buzzer
bool dmpReady = false;              //state of the mpu6050

int yprInt[3];                      //gyro data in a integer because the extra precision of the float is total bullshit
int altitude;                       //altitude above takeoff point in meters
int altitudeCalibration;            //calibration for the altimeter
int temperature;                    //temperature in degrees Celcius

float counter = 0;                  //time counter, a somewhat internal clock that starts when the module starts dataloging

uint8_t mpuIntStatus;               //holds actual interrupt status byte from MPU
uint8_t devStatus;                  //return status after each device operation
uint8_t fifoBuffer[64];             //FIFO storage buffer

uint16_t packetSize;                //expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;                 //count of all bytes currently in FIFO

String day;                         //day of the week
String ymd;                         //date: year,month,day
String hms;                         //time: hour,minute,second

MPU6050 mpu;                        //change mpu's adress
Quaternion q;                       //quaternion container
VectorFloat gravity;                //gravity vector

MPL3115A2 myPressure;               //MPL3115A2 object declaration as myPressure
DS3231  rtc(A0, A1);                //DS3231 object declaration as

void dmpDataReady() {
  mpuInterrupt = true;
}

class MainLoop
{

    unsigned long previousMillis;

  public:
    MainLoop()
    {
      rtc.begin();            //start DS3231

      Wire.begin();
      myPressure.begin();                                         //start MPL3115A2 communication
      myPressure.setModeAltimeter();
      myPressure.setOversampleRate(7);
      myPressure.enableEventFlags();
      altitudeCalibration = myPressure.readAltitude();            //read altitude

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

      TWBR = 24;
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE

      Fastwire::setup(400, true);
#endif

      mpu.initialize();
      devStatus = mpu.dmpInitialize();
      mpu.setXGyroOffset(0);
      mpu.setYGyroOffset(0);
      mpu.setZGyroOffset(0);
      mpu.setZAccelOffset(0);

      if (devStatus == 0)
      {
        mpu.setDMPEnabled(true);
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();
        dmpReady = true;
        packetSize = mpu.dmpGetFIFOPacketSize();
      }

#ifdef testtest

      Serial.begin(115200); //start serial communication
#endif

#ifndef testtest

      if (SD.begin()); ();       //sd card
#endif

    }

    void go(  )
    {
      unsigned long currentMillis = millis();

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

      TWBR = 24;

#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE

      Fastwire::setup(400, true);
#endif

      mpu.initialize();
      devStatus = mpu.dmpInitialize();
      mpu.setXGyroOffset(0);
      mpu.setYGyroOffset(0);
      mpu.setZGyroOffset(0);
      mpu.setZAccelOffset(0);

      if (devStatus == 0)
      {
        mpu.setDMPEnabled(true);
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();
        dmpReady = true;
        packetSize = mpu.dmpGetFIFOPacketSize();
      }

      else
      {
        return;
      }

      altitude = myPressure.readAltitude() - altitudeCalibration; //read altitude
      temperature = myPressure.readTemp();                        //read temperature


      day = rtc.getDOWStr();  //get day of the week
      ymd = rtc.getDateStr(); //get day, month, year
      hms = rtc.getTimeStr(); //get hour, minutes, seconds

#ifdef testtest

      Serial.print(day);
      Serial.print(" ");
      Serial.print(ymd);
      Serial.print(" -- ");
      Serial.print(hms);
      Serial.print(", ");
      Serial.print(counter);
      Serial.print(", ");
      Serial.print(yprInt[0]);
      Serial.print(", ");
      Serial.print(yprInt[1]);
      Serial.print(", ");
      Serial.print(yprInt[2]);
      Serial.print(", ");
      Serial.print(altitude);
      Serial.print(" ,");
      Serial.print(temperature);
      Serial.println();
#endif

#ifndef testtest

      if (altitude &gt; 1) //if the device is higher then 1m, then we sstart recording everything
      {
        File dataFile = SD.open("datalog.txt", FILE_WRITE);

        if (dataFile)
        {
          dataFile.print(day);
          dataFile.print(" ");
          dataFile.print(ymd);
          dataFile.print(" -- ");
          dataFile.print(hms);
          dataFile.print(", ");
          dataFile.print(counter);
          dataFile.print(", ");
          dataFile.print(yprInt[0]);
          dataFile.print(", ");
          dataFile.print(yprInt[1]);
          dataFile.print(", ");
          dataFile.print(yprInt[2]);
          dataFile.print(", ");
          dataFile.print(altitude);
          dataFile.print(" ,");
          dataFile.print(temperature);
          dataFile.println();
          dataFile.close();
        }
      }
#endif

      counter = counter + 0.5;

    }
};

MainLoop main;

void setup() {
}

void loop()  {
  main.go();
}
</code></pre>
 10794: <blockquote>
  <p>mind giving a small example. I'm really not familiar with class and constructors</p>
</blockquote>

<p>Here is an example of trying to do things in the constructor:</p>

<pre><code>class foo 
{
  public:
  foo () // constructor
    {
    Serial.begin (115200);
    Serial.println ("Hello, world.");
    }
}; // end of class foo

foo bar;  // make an instance of foo

void setup() { }
void loop() { }
</code></pre>

<p>On my setup that prints "Hel" and nothing else.</p>

<hr>

<p>Now if we change things around it works:</p>

<pre><code>class foo 
{
  public:
  void begin ()
    {
    Serial.begin (115200);
    Serial.println ("Hello, world.");
    }
}; // end of class foo

foo bar;  // make an instance of foo

void setup() 
  {
  bar.begin ();
  }

void loop() { }
</code></pre>

<p>Now we are doing things in the right order. "Under the hood" the <code>main</code> function provided by the IDE initializes the hardware (by calling <code>init</code>) and then calls <code>setup</code>. Now we can call the <code>begin</code> function in our class.</p>

<hr>

<p>It would have worked if we created the class instance inside <code>setup</code> because that is after <code>init</code> is called, like this:</p>

<pre><code>class foo 
{
  public:
  foo () // constructor
    {
    Serial.begin (115200);
    Serial.println ("Hello, world.");
    }
}; // end of class foo


void setup() 
  { 
  foo bar;  // make an instance of foo
  }

void loop() { }
</code></pre>

<p>However now <code>bar</code> is not a global variable.</p>

<hr>

<p>What is sometimes done is to make a function to return an instance of the class, which creates it the first time it is called, like this:</p>

<pre><code>class foo 
{
  public:
  foo () // constructor
    {
    Serial.println (F("Hello, world."));
    }
  void doSomething ()
    {
    Serial.println (F("Doing something"));
    }
}; // end of class foo

foo &amp; getFoo ()
  {
  static foo myBar;  // make an instance of foo
  return myBar;
  }  // end of getFoo

void setup() 
  { 
  Serial.begin (115200);
  Serial.println (F("Program starting."));

  foo &amp; bar = getFoo ();
  bar.doSomething ();
  }  // end of setup

void loop() 
  { 
  foo &amp; bar = getFoo ();
  bar.doSomething ();
  delay (500);
  }  // end of loop
</code></pre>

<p>Now the function <code>getFoo</code> returns a <em>reference</em> to the instance of foo created as a static variable inside it. Thus, <em>the first time it is called</em>, the instance is created. Subsequent times just return the same instance.</p>
 4143: <p>Move all that code out of the constructor and put it in a <code>.begin()</code> method to call from <code>setup()</code>. You are calling functions before the chip has been initialized and things are being undone.</p>

<p>It is only safe to use a constructor to initialize a few variables and the like. Simply because the call sequence is:</p>

<ol>
<li>Constructors</li>
<li>System Init</li>
<li>Setup</li>
<li>Repeat forever:</li>
<li>-> Loop</li>
</ol>

<p>System Init configures the timers, peripherals, IO pins, etc. Things which need to be done <em>before</em> the code in your constructor.</p>
