Accelerometer 

Example

: Advanced Example

This example shows how to get the acceleration on the different axis. It gets the configuration registers values and the acceleration registers values too.


File:
"WaspACC_2_advancedExample.pde"


Code



/*
 *  ------Waspmote Accelerator Advanced Example--------
 *
 *  Explanation: This example shows how to get the acceleration on the
 *  different axis. It gets the configuration registers values and the 
 *  acceleration registers values too.
 *
 *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 *
 *  Version:           0.1
 *  Design:            David Gascón
 *  Implementation:    David Cuartielles, Alberto Bielsa
 */
 

void setup()
{
  ACC.ON();
  USB.begin(); // starts using the serial port
}

void loop()
{

  //----------Check Register-----------------------
  // should always answer 0x3A, it is used to check
  // the proper functionality of the accelerometer
  byte check = ACC.check();

  //----------Status Register-----------------------
  // the status will be '1' if the sensor is on sleep
  // mode and '0' if it is ready to send data back
  byte status = ACC.getStatus();

  //----------CRTL REG #1-----------------------
  int ctrl_1 = ACC.getCTRL1();

  //----------CRTL REG #2-----------------------
  int ctrl_2 = ACC.getCTRL2();

  //----------CRTL REG #3-----------------------
  int ctrl_3 = ACC.getCTRL3();

  //----------X Values-----------------------
  int x_valH, x_valL, x_acc;

  x_valH = ACC.readRegister(outXhigh);
  x_valL = ACC.readRegister(outXlow);
  x_acc = ACC.getX();

  //----------Y Values-----------------------
  int y_valH, y_valL, y_acc;

  y_valH = ACC.readRegister(outYhigh);
  y_valL = ACC.readRegister(outYlow);
  y_acc = ACC.getY();

  //----------Z Values-----------------------
  int z_valH, z_valL, z_acc;

  z_valH = ACC.readRegister(outZhigh);
  z_valL = ACC.readRegister(outZlow);
  z_acc = ACC.getZ();

  //-------------------------------

  if (x_valH != -1 && y_valH != -1 && z_valH != -1 && check == 0x3A)
  {
    USB.print("\n------------------------------\nCheck: 0x"); 
    USB.println(check, HEX);
    USB.print("status "); 
    USB.println(status, BIN);
    USB.println("\n \tCTRL1\tCTRL2\tCTRL3"); 
    USB.print("\t0x"); 
    USB.print(ctrl_1, HEX);
    USB.print("\t0x"); 
    USB.print(ctrl_2, HEX);
    USB.print("\t0x"); 
    USB.println(ctrl_3, HEX);
    USB.println("\n \t0X\t0Y\t0Z"); 
    USB.print(" valH\t0x"); 
    USB.print(x_valH, HEX);
    USB.print("\t0x"); 
    USB.print(y_valH, HEX);
    USB.print("\t0x"); 
    USB.println(z_valH, HEX);
    USB.print(" valL\t0x"); 
    USB.print(x_valL, HEX);
    USB.print("\t0x"); 
    USB.print(y_valL, HEX);
    USB.print("\t0x"); 
    USB.println(z_valL, HEX);
    USB.print(" ACC\t"); 
    USB.print(x_acc, DEC);
    USB.print("\t"); 
    USB.print(y_acc, DEC);
    USB.print("\t"); 
    USB.println(z_acc, DEC);
  }
  else
    USB.print("no data");

  delay(500);
}
    

Download code

You can download the code of this example.


© Libelium Comunicaciones Distribuidas S.L.

| Terms of use