Go Down

Topic: SD card logs wrong Temperature (Read 295 times) previous topic - next topic

cathytsir

Hello! I am using on Arduino UNO the DHT11 sensor hooked up on a breadboard ,at the same time with other sensors. I also use a breakout board for an SD card micro and although through the serial port the Temperature values are correct, the SD card logs wrong temperature values. The values from the other sensors are correct though..

The code is this
Code: [Select]

/*
  SD card datalogger

  This example shows how to log data from three analog sensors
  to an SD card using the SD library.

  The circuit:
   analog sensors on analog ins 0, 1, and 2
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

  This example code is in the public domain.

*/
//SD Card libraries
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;

//DHT11 Sensor libraries
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN            2         // Pin which is connected to the DHT sensor.
#define DHTTYPE           DHT11     // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
//initializes/defines the output pin of the LM335 temperature sensor
int outputPin = 0;
//this sets the ground pin to LOW and the input voltage pin to high


//Piezo Sensor Pin
const int PIEZO_PIN = A1; // Piezo output

////Accelerometer MMA8452Q//////
/*
  Hardware hookup:
  Arduino --------------- MMA8452Q Breakout
    3.3V  ---------------     3.3V
    GND   ---------------     GND
  SDA (A4) --\/330 Ohm\/--    SDA
  SCL (A5) --\/330 Ohm\/--    SCL


  // */
#include <Wire.h> // Must include Wire library for I2C
#include <SparkFun_MMA8452Q.h> // Includes the SFE_MMA8452Q library

// Begin using the library by creating an instance of the MMA8452Q
//  class. We'll call it "accel". That's what we'll reference from
//  here on out.

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
 

  ////////////////////////////////////////////////////
  //dht sensor
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  delayMS = sensor.min_delay / 1000; // Set delay between sensor readings based on sensor details.

  accel.init();
}

void loop()
{
 
  // Delay between measurements.
  delay(delayMS);
  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");
  }
  else {
    Serial.print("Temperature: ");
    Serial.print(event.temperature);
    Serial.println(" *C");
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println("Error reading humidity!");
  }
  else {
    Serial.print("Humidity: ");
    Serial.print(event.relative_humidity);
    Serial.println("%");
  }

  // read the input of Myoware muscle sensor on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println("Muscle Sensor: ");
  Serial.println(voltage);
  delay(500);


  // Read Piezo ADC value in, and convert it to a voltage
  int piezoADC = analogRead(PIEZO_PIN);
  float piezoV = piezoADC / 1023.0 * 5.0;
  Serial.println("piezoVibration= ");
  Serial.println(piezoV); // Print the voltage.
  delay(500);

  //////Accelerometer/////
  // Use the accel.available() function to wait for new data
  //  from the accelerometer.
  if (accel.available())
  {
    // First, use accel.read() to read the new variables:
    accel.read();

    // accel.read() will update two sets of variables.
    // * int's x, y, and z will store the signed 12-bit values
    //   read out of the accelerometer.
    // * floats cx, cy, and cz will store the calculated
    //   acceleration from those 12-bit values. These variables
    //   are in units of g's.
    // Check the two function declarations below for an example
    // of how to use these variables.
   

    printCalculatedAccels();
    //printAccels(); // Uncomment to print digital readings

    // The library also supports the portrait/landscape detection
    //  of the MMA8452Q. Check out this function declaration for
    //  an example of how to use that.
    printOrientation();

    Serial.println(); // Print new line every time.
    delay(1000);
 

  ////////////////////////////////////////////////////////
  File dataFile = SD.open("datalog.txt", FILE_WRITE);


  if (dataFile)
  {
    //dataFile.println(buf);
    dataFile.print("Temperature: ");
    dataFile.print(event.temperature);
    dataFile.println(" *C");

    dataFile.print("Humidity: ");
    dataFile.print(event.relative_humidity);
    dataFile.println("%");

    dataFile.println("Myoware");
    dataFile.print(voltage);
    // dataFile.print(" V"); //Move to next column using a ","



    dataFile.println("piezoVibration: ");
    dataFile.println(piezoV);
    //dataFile.print(" V"); //Move to next column using a ", "


    ///*
    dataFile.print(accel.cx, 3);
    dataFile.print("\t");
    dataFile.print(accel.cy, 3);
    dataFile.print("\t");
    dataFile.print(accel.cz, 3);
    dataFile.print("\t");

   
    dataFile.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
 }

}

// This function demonstrates how to use the accel.cx, accel.cy,
//  and accel.cz variables.
// Before using these variables you must call the accel.read()
//  function!
void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.print("\t");
}

// This function demonstrates how to use the accel.readPL()
// function, which reads the portrait/landscape status of the
// sensor.
void printOrientation()
{
  // accel.readPL() will return a byte containing information
  // about the orientation of the sensor. It will be either
  // PORTRAIT_U, PORTRAIT_D, LANDSCAPE_R, LANDSCAPE_L, or
  // LOCKOUT.
  byte pl = accel.readPL();
  switch (pl)
  {
    case PORTRAIT_U:
      Serial.print("Portrait Up");
      break;
    case PORTRAIT_D:
      Serial.print("Portrait Down");
      break;
    case LANDSCAPE_R:
      Serial.print("Landscape Right");
      break;
    case LANDSCAPE_L:
      Serial.print("Landscape Left");
      break;
    case LOCKOUT:
      Serial.print("Flat");
      break;
  }
}

6v6gt

If the humidity is correctly written to the SD card, then I guess that the attempt to get the humidity has overwritten the value of temperature somehow. You appear to be using the same event object (sensors_event_t  event ) for both sensor readings. Try to avoid this by defining a second object.

Another thing you can try is to capture the value of event.temperature into a temporary variable at the time you do this:

Code: [Select]

Serial.print(event.temperature);


and use that temporary variable when you write to the SD card.

I guess there is a much more elegant way of doing it, but that Adafruit sensor architecture appears to be an extremely complex way of doing something simple and I 'm not prepared to study it in any detail. I couldn't even easily find out what datatype event.temperature is, but I guess it is a float.

cathytsir

Thank you for your answer! I think you are right its writing the value of the Humidity on the Temperature..what you said about this temporary variable I didn't quite understand..
but anyway now I have another more serious problem I guess because I can't upload anything on the Arduino board i keep getting this message(!!!):


avrdude: verification error, first mismatch at byte 0x0042
         0x65 != 0x8d
avrdude: verification error; content mismatch
avrdude: verification error; content mismatch

Coding Badly

but anyway now I have another more serious problem I guess because I can't upload anything on the Arduino board
Please do not cross-post.  Thread trimmed.


cathytsir


cathytsir

I hooked up everything in my other Arduino and deleted everything in the code that was related to humidity.. So now the Temperature values are written correctly in the SD card..
But maybe the right thing would be to try using two different variables for temperature and humidity as you suggested..

6v6gt

I must admit that I don't know the Adafruit sensor library architecture, however, I believe that you should use:

Code: [Select]
sensors_event_t event;  // temperature
sensors_event_t event_hum ; // humidity


Previously, you used event for both.


and where you see "event" in a "humidity" part of the code, change it to "event_hum"  E.g. :

Code: [Select]
dht.humidity().getEvent(&event_hum);

cathytsir

aha ok I'll try this thanks a lot!

cathytsir

I must admit that I don't know the Adafruit sensor library architecture, however, I believe that you should use:

Code: [Select]
sensors_event_t event;  // temperature
sensors_event_t event_hum ; // humidity


Previously, you used event for both.


and where you see "event" in a "humidity" part of the code, change it to "event_hum"  E.g. :

Code: [Select]
dht.humidity().getEvent(&event_hum);
I tried it but unfortunately it didn't work..it only printed once the Temperature in the serial port and then nothing...the only solution is to completely erase humidity..It's so weird because in the beginning it used to work fine and write the correct values on the SD card..I don't understand why this problem occured...

6v6gt

You did realise that I had given only one example of a line to change? Here are all that I can see :

Code: [Select]

sensors_event_t event;  // temperature
sensors_event_t event_hum ; // humidity   //*
. . .
. . .

 // Get humidity event and print its value.
 dht.humidity().getEvent(&event_hum);      //*
 if (isnan(event_hum.relative_humidity)) {   //*
   Serial.println("Error reading humidity!");
 }
 else {
   Serial.print("Humidity: ");
   Serial.print(event_hum.relative_humidity);  //*
   Serial.println("%");
 }

. . .
. . .

     dataFile.print("Humidity: ");
     dataFile.print(event_hum.relative_humidity);  //*
     dataFile.println("%");


If it still does not work, post the entire sketch.

cathytsir

You did realise that I had given only one example of a line to change? Here are all that I can see :

Code: [Select]

sensors_event_t event;  // temperature
sensors_event_t event_hum ; // humidity   //*
. . .
. . .

 // Get humidity event and print its value.
 dht.humidity().getEvent(&event_hum);      //*
 if (isnan(event_hum.relative_humidity)) {   //*
   Serial.println("Error reading humidity!");
 }
 else {
   Serial.print("Humidity: ");
   Serial.print(event_hum.relative_humidity);  //*
   Serial.println("%");
 }

. . .
. . .

     dataFile.print("Humidity: ");
     dataFile.print(event_hum.relative_humidity);  //*
     dataFile.println("%");


If it still does not work, post the entire sketch.
yes i understood that i did exactly what you posted but it doesn't work..

cathytsir

I am also posting the whole code with the modification you suggested..you can check if I missed something. It only prints the Temperature once and then stops..

Code: [Select]
/*
  SD card datalogger

  This example shows how to log data from three analog sensors
  to an SD card using the SD library.

  The circuit:
   analog sensors on analog ins 0, 1, and 2
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

  created  24 Nov 2010
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/
//SD Card libraries
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;

//DHT11 Sensor libraries
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN            2         // Pin which is connected to the DHT sensor.
#define DHTTYPE           DHT11     // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
//initializes/defines the output pin of the LM335 temperature sensor
int outputPin = 0;
//this sets the ground pin to LOW and the input voltage pin to high


//Piezo Sensor Pin
const int PIEZO_PIN = A1; // Piezo output

////Accelerometer MMA8452Q//////
/*
  Hardware hookup:
  Arduino --------------- MMA8452Q Breakout
    3.3V  ---------------     3.3V
    GND   ---------------     GND
  SDA (A4) --\/330 Ohm\/--    SDA
  SCL (A5) --\/330 Ohm\/--    SCL


  // */
#include <Wire.h> // Must include Wire library for I2C
#include <SparkFun_MMA8452Q.h> // Includes the SFE_MMA8452Q library

// Begin using the library by creating an instance of the MMA8452Q
//  class. We'll call it "accel". That's what we'll reference from
//  here on out.

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");


  ////////////////////////////////////////////////////

  //dht sensor///////
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  delayMS = sensor.min_delay / 1000; // Set delay between sensor readings based on sensor details.
  ////////////////////////////////////////////////////////////
  accel.init();
}

void loop()
{

  // Delay between measurements.
  delay(delayMS);
  // Get temperature event and print its value.
  sensors_event_t event;
  sensors_event_t event_hum;//humidity object
 
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");
  }
  else {
    Serial.print("Temperature: ");
    Serial.print(event.temperature);
    Serial.println(" *C");
  }
  // /* // Get humidity event and print its value.
  dht.humidity().getEvent(&event_hum);
  if (isnan(event_hum.relative_humidity)) {
    Serial.println("Error reading humidity!");
  }
  else {
    Serial.print("Humidity: ");
    Serial.print(event_hum.relative_humidity);
    Serial.println("%");
  }
  //*/

  // read the input of Myoware muscle sensor on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println("Muscle Sensor: ");
  Serial.println(voltage);
  delay(500);


  // Read Piezo ADC value in, and convert it to a voltage
  int piezoADC = analogRead(PIEZO_PIN);
  float piezoV = piezoADC / 1023.0 * 5.0;
  Serial.println("piezoVibration= ");
  Serial.println(piezoV); // Print the voltage.
  delay(500);

  //////Accelerometer/////
  // Use the accel.available() function to wait for new data
  //  from the accelerometer.
  if (accel.available())
  {
    // First, use accel.read() to read the new variables:
    accel.read();

    // accel.read() will update two sets of variables.
    // * int's x, y, and z will store the signed 12-bit values
    //   read out of the accelerometer.
    // * floats cx, cy, and cz will store the calculated
    //   acceleration from those 12-bit values. These variables
    //   are in units of g's.
    // Check the two function declarations below for an example
    // of how to use these variables.


    printCalculatedAccels();
    //printAccels(); // Uncomment to print digital readings

    // The library also supports the portrait/landscape detection
    //  of the MMA8452Q. Check out this function declaration for
    //  an example of how to use that.
    printOrientation();

    Serial.println(); // Print new line every time.
    delay(1000);


    ////////////////////////////////////////////////////////
    File dataFile = SD.open("datalog.txt", FILE_WRITE);


    if (dataFile)
    {
      //dataFile.println(buf);
      dataFile.print("Temperature: ");
      dataFile.print(event.temperature);
      dataFile.println(" *C");

      dataFile.print("Humidity: ");
      dataFile.print(event_hum.relative_humidity);
      dataFile.println("%");

      dataFile.println("Myoware");
      dataFile.print(voltage);
      // dataFile.print(" V"); //Move to next column using a ","



      dataFile.println("piezoVibration: ");
      dataFile.println(piezoV);
      //dataFile.print(" V"); //Move to next column using a ", "


      ///*
      dataFile.print(accel.cx, 3);
      dataFile.print("\t");
      dataFile.print(accel.cy, 3);
      dataFile.print("\t");
      dataFile.print(accel.cz, 3);
      dataFile.print("\t");


      dataFile.close();
    }
    // if the file isn't open, pop up an error:
    else {
      Serial.println("error opening datalog.txt");
    }
  }

}

// This function demonstrates how to use the accel.cx, accel.cy,
//  and accel.cz variables.
// Before using these variables you must call the accel.read()
//  function!
void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.print("\t");
}

// This function demonstrates how to use the accel.readPL()
// function, which reads the portrait/landscape status of the
// sensor.
void printOrientation()
{
  // accel.readPL() will return a byte containing information
  // about the orientation of the sensor. It will be either
  // PORTRAIT_U, PORTRAIT_D, LANDSCAPE_R, LANDSCAPE_L, or
  // LOCKOUT.
  byte pl = accel.readPL();
  switch (pl)
  {
    case PORTRAIT_U:
      Serial.print("Portrait Up");
      break;
    case PORTRAIT_D:
      Serial.print("Portrait Down");
      break;
    case LANDSCAPE_R:
      Serial.print("Landscape Right");
      break;
    case LANDSCAPE_L:
      Serial.print("Landscape Left");
      break;
    case LOCKOUT:
      Serial.print("Flat");
      break;
  }
}

6v6gt

OK. It does look like you edited the file correctly so there must be something else.

Do the Serial.print() statements to the console give the correct values for temperature and humidity ? If not, can you see if say humidity is appearing where temperature should or what do you see ?

What do you see on the SD card ?  The correct temperature value ? the correct humidity value ? or is humidity appearing where temperature should or what ?

cathytsir

It doesn't print anything in the serial port but the first Temperature value..then nothing..and it doesn't write anything in the SD card with this code..

6v6gt

OK. It is clear that was the wrong approach. I've now changed the code to capture the results of temperature and humidity in temporary variables because it is clear that the attempt to get the value of humidity interferes with the previous value of temperature.

There are, incidentally, much simpler ways of reading a DHT sensor than using the Adafruit "Unified Sensor" approach, but since you have got so far with it, you may as well carry on.

Here it is based on your original posted code, with changes marked. If it doesn't work, I can try something else.


Code: [Select]



// based on your original code - first post. Changes marked with // ***



/*
  SD card datalogger

  This example shows how to log data from three analog sensors
  to an SD card using the SD library.

  The circuit:
   analog sensors on analog ins 0, 1, and 2
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

  This example code is in the public domain.

*/
//SD Card libraries
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;

//DHT11 Sensor libraries
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN            2         // Pin which is connected to the DHT sensor.
#define DHTTYPE           DHT11     // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
//initializes/defines the output pin of the LM335 temperature sensor
int outputPin = 0;
//this sets the ground pin to LOW and the input voltage pin to high


//Piezo Sensor Pin
const int PIEZO_PIN = A1; // Piezo output

////Accelerometer MMA8452Q//////
/*
  Hardware hookup:
  Arduino --------------- MMA8452Q Breakout
    3.3V  ---------------     3.3V
    GND   ---------------     GND
  SDA (A4) --\/330 Ohm\/--    SDA
  SCL (A5) --\/330 Ohm\/--    SCL


  // */
#include <Wire.h> // Must include Wire library for I2C
#include <SparkFun_MMA8452Q.h> // Includes the SFE_MMA8452Q library

// Begin using the library by creating an instance of the MMA8452Q
//  class. We'll call it "accel". That's what we'll reference from
//  here on out.

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");


  ////////////////////////////////////////////////////
  //dht sensor
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  delayMS = sensor.min_delay / 1000; // Set delay between sensor readings based on sensor details.

  accel.init();
}

void loop()
{

  float dhtTemp ;  // ***
  float dhtHum ;   // ***

  // Delay between measurements.
  delay(delayMS);
  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");
    dhtTemp = 0 ;  // ***
  }
  else {
    Serial.print("Temperature: ");
    dhtTemp = event.temperature ;  // ***
    Serial.print(dhtTemp);  // ***
    Serial.println(" *C");
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println("Error reading humidity!");
    dhtHum = 0 ; // ***
  }
  else {
    Serial.print("Humidity: ");
    dhtHum = event.relative_humidity ;
    Serial.print(dhtHum);  // ***
    Serial.println("%");
  }

  // read the input of Myoware muscle sensor on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println("Muscle Sensor: ");
  Serial.println(voltage);
  delay(500);


  // Read Piezo ADC value in, and convert it to a voltage
  int piezoADC = analogRead(PIEZO_PIN);
  float piezoV = piezoADC / 1023.0 * 5.0;
  Serial.println("piezoVibration= ");
  Serial.println(piezoV); // Print the voltage.
  delay(500);

  //////Accelerometer/////
  // Use the accel.available() function to wait for new data
  //  from the accelerometer.
  if (accel.available())
  {
    // First, use accel.read() to read the new variables:
    accel.read();

    // accel.read() will update two sets of variables.
    // * int's x, y, and z will store the signed 12-bit values
    //   read out of the accelerometer.
    // * floats cx, cy, and cz will store the calculated
    //   acceleration from those 12-bit values. These variables
    //   are in units of g's.
    // Check the two function declarations below for an example
    // of how to use these variables.


    printCalculatedAccels();
    //printAccels(); // Uncomment to print digital readings

    // The library also supports the portrait/landscape detection
    //  of the MMA8452Q. Check out this function declaration for
    //  an example of how to use that.
    printOrientation();

    Serial.println(); // Print new line every time.
    delay(1000);


    ////////////////////////////////////////////////////////
    File dataFile = SD.open("datalog.txt", FILE_WRITE);


    if (dataFile)
    {
      //dataFile.println(buf);
      dataFile.print("Temperature: ");
      dataFile.print(dhtTemp);   // ***
      dataFile.println(" *C");

      dataFile.print("Humidity: ");
      dataFile.print(dhtHum);  // ***
      dataFile.println("%");

      dataFile.println("Myoware");
      dataFile.print(voltage);
      // dataFile.print(" V"); //Move to next column using a ","



      dataFile.println("piezoVibration: ");
      dataFile.println(piezoV);
      //dataFile.print(" V"); //Move to next column using a ", "


      ///*
      dataFile.print(accel.cx, 3);
      dataFile.print("\t");
      dataFile.print(accel.cy, 3);
      dataFile.print("\t");
      dataFile.print(accel.cz, 3);
      dataFile.print("\t");


      dataFile.close();
    }
    // if the file isn't open, pop up an error:
    else {
      Serial.println("error opening datalog.txt");
    }
  }

}

// This function demonstrates how to use the accel.cx, accel.cy,
//  and accel.cz variables.
// Before using these variables you must call the accel.read()
//  function!
void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.print("\t");
}

// This function demonstrates how to use the accel.readPL()
// function, which reads the portrait/landscape status of the
// sensor.
void printOrientation()
{
  // accel.readPL() will return a byte containing information
  // about the orientation of the sensor. It will be either
  // PORTRAIT_U, PORTRAIT_D, LANDSCAPE_R, LANDSCAPE_L, or
  // LOCKOUT.
  byte pl = accel.readPL();
  switch (pl)
  {
    case PORTRAIT_U:
      Serial.print("Portrait Up");
      break;
    case PORTRAIT_D:
      Serial.print("Portrait Down");
      break;
    case LANDSCAPE_R:
      Serial.print("Landscape Right");
      break;
    case LANDSCAPE_L:
      Serial.print("Landscape Left");
      break;
    case LOCKOUT:
      Serial.print("Flat");
      break;
  }
}


Go Up