Accelerometer 

Example

: Direction Change Interruption

This example shows how to manage the direction change interruption.


File:
"WaspACC_4_DirectionChange.pde"


Code



/*
 *  ------Waspmote Accelerator Direction Change Example--------
 *
 *  Explanation: This example shows how to manage the direction change 
 *  interruption.
 *
 *  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 <http://www.gnu.org/licenses/>.
 *
 *  Version:           0.1
 *  Design:            David Gascón
 *  Implementation:    David Cuartielles, Alberto Bielsa
 */

long previous = 0;

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


void loop()
{
  // check the interruptions for 2 seconds
  previous = millis();
  while(millis() - previous <= 2000)
  {
    if( intFlag & ACC_INT ) finishDDtest();
  }
  printAcc();
}

//// Functions ///////////////////////////////////////////////////////////

/*
 * printAcc(void) - dump to the USB accX, accY, accZ if they are good data
 */
void printAcc(void)
{
  //----------Check Register-----------------------
  // should always answer 0x3A, it is used to check
  // the proper functionality of the accelerometer
  byte check = ACC.check();

  //----------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();

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

  // print out data only if the sensor reports its identity properly
  if (check == 0x3A && x_valH != -1 && y_valH != -1 && z_valH != -1)
  {
    USB.println("------------------------------------------"); 
    USB.println(" \t0X\t0Y\t0Z"); 
    
    // show the thresholds (for orientation)
    USB.print("ThsI:\t"); 
    USB.print(DD_THSI_H_val<<4 | DD_THSI_L_val>>4, DEC); USB.print("\t");
    USB.print(DD_THSI_H_val<<4 | DD_THSI_L_val>>4, DEC); USB.print("\t");
    USB.println(DD_THSI_H_val<<4 | DD_THSI_L_val>>4, DEC);
    
    // show the acceleration
    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);
    
    // show the thresholds (for orientation)
    USB.print("ThsE:\t"); 
    USB.print(DD_THSE_H_val<<4 | DD_THSE_L_val>>4, DEC); USB.print("\t");
    USB.print(DD_THSE_H_val<<4 | DD_THSE_L_val>>4, DEC); USB.print("\t");
    USB.println(DD_THSE_H_val<<4 | DD_THSE_L_val>>4, DEC);
  }
  else
    USB.print("no data");

  delay(500);
}


/*
 * finishFFinterrupt(void) - detach the free fall interrupt
 */
void finishDDtest () {
  // clear the accelerometer interrupt flag on the general interrupt vector
  intFlag &= ~(ACC_INT);

  // read the acceleration source register
  int auxReg = ACC.readRegister(DD_SRC);

  // print the results if the interrupt bit is
  // active. It could happen that there was an
  // error in the communication, IA is the Active
  // Interrupt pin inside the DD_SRC register
  if (auxReg & IA && auxReg & ~(IA))
  {
    USB.println("------------------------------------------"); 
    USB.print("DD_SRC: "); 

    USB.println(auxReg, BIN);

    USB.print("\nAcceleration detected on the axis: ");
    if (auxReg & XHIE) USB.print("OXH ");
    if (auxReg & YHIE) USB.print("OYH ");
    if (auxReg & ZHIE) USB.print("OZH ");
    if (auxReg & XLIE) USB.print("OXL ");
    if (auxReg & YLIE) USB.print("OYL ");
    if (auxReg & ZLIE) USB.print("OZL ");
    USB.println();
    
    // make the LEDs blink
    Utils.blinkLEDs(1000);
  }

  // unset the Free Fall interrupt
  ACC.setDD();  
}
    

Download code

You can download the code of this example.


© 2009 Libelium Comunicaciones Distribuidas S.L.

| Terms of use