This example shows how to manage the free fall interruption.
File:
"WaspACC_3_FreeFall.pde"
/*
* ------Waspmote Accelerator Free Fall Example--------
*
* Explanation: This example shows how to manage the free fall
* 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.setFF();
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 ) finishFFtest();
}
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 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("Ths:\t");
USB.print(FF_WU_THS_H_val<<4 | FF_WU_THS_L_val>>4, DEC); USB.print("\t");
USB.print(FF_WU_THS_H_val<<4 | FF_WU_THS_L_val>>4, DEC); USB.print("\t");
USB.println(FF_WU_THS_H_val<<4 | FF_WU_THS_L_val>>4, DEC);
}
else
USB.print("no data");
delay(500);
}
/*
* finishFFinterrupt(void) - detach the free fall interrupt
*/
void finishFFtest () {
// clear the accelerometer interrupt flag on the general interrupt vector
intFlag &= ~(ACC_INT);
// read the acceleration source register
int auxReg = ACC.readRegister(FF_WU_SRC);
USB.println(auxReg,DEC);
// 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 FF_WU_SRC register
if (auxReg & IA)
{
USB.println("------------------------------------------");
USB.print("FF_WU_SRC: ");
USB.println(auxReg, BIN);
USB.print("\nAcceleration detected on the axis: ");
if (auxReg & XHIE) USB.print("OX ");
if (auxReg & YHIE) USB.print("OY ");
if (auxReg & ZHIE) USB.print("OZ");
USB.println();
// make the LEDs blink
Utils.blinkLEDs(1000);
}
ACC.setFF();
}
You can download the code of this example.