0

I'm trying to get a sensor to activate if a push button is clicked. And deactivate when a certain condition is met and reactivate when the push button is pressed again.

#include <SPI.h> // Included for SFE_LSM9DS0 library
#include <Wire.h>
#include <SFE_LSM9DS0.h>
#define LSM9DS0_XM 0x1D // Would be 0x1E if SDO_XM is LOW
#define LSM9DS0_G 0x6B // Would be 0x6A if SDO_G is LOW
// Create an instance of the LSM9DS0 library called `dof` the
// parameters for this constructor are:
// [SPI or I2C Mode declaration], [gyro I2C address], [xm I2C address]
LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);
const byte INT1XM = 4; // INT1XM tells us when accel data is ready
//const byte INT2XM = 8; // INT2XM tells us when mag data is ready
//const byte DRDYG = 7; // DRDYG tells us when gyro data is ready
const int buttonPin = 15; // the number of the pushbutton pin
int buttonState = 0;
double X;
double Y;
double Z;
void setup(){
 pinMode(INT1XM, INPUT);
 pinMode(buttonPin, INPUT);
 Serial.begin(9600);
 Serial1.begin(9600); // Start serial at 115200 bps
 // Use the begin() function to initialize the LSM9DS0 library.
 // You can either call it with no parameters (the easy way):
 uint16_t status = dof.begin();
 // Or call it with declarations for sensor scales and data rates: 
 //uint16_t status = dof.begin(dof.G_SCALE_2000DPS, dof.A_SCALE_6G, dof.M_SCALE_2GS);
 // begin() returns a 16-bit value which includes both the gyro and
 // accelerometers WHO_AM_I response. You can check this to make sure
 // communication was successful.
 // Serial.println(status, HEX);
 setODR();
}
void loop(){
 buttonState = digitalRead(buttonPin); //Read push button logic.
 // Serial.println(buttonState);
 if (buttonState == 1) { //if push button is pressed
 printAccel(); //print sensor data
 }
}
void printAccel()
{
 // Only read from the accelerometer if the accel interrupts,
 // which means that new data is ready.
 if (digitalRead(INT1XM))
 {
 // Use the readAccel() function to get new data from the accel.
 // After calling this function, new values will be stored in
 // the ax, ay, and az variables.
 dof.readAccel();
 //Serial.print("A: ");
 // Using the calcAccel helper function, we can get the
 // accelerometer readings in g's.
 X=dof.calcAccel(dof.ax);
 Y=dof.calcAccel(dof.ay);
 Z=dof.calcAccel(dof.az);
 Serial.print(X);
 Serial.print(", ");
 Serial.print(Y);
 Serial.print(", ");
 Serial.println(Z); 
 // Serial1.println(Z); 
 fall(); //jump to check condition
 }
}
void setODR(){
 dof.setAccelODR(dof.A_ODR_25);
 dof.setAccelScale(dof.A_SCALE_16G);
}
void fall(){
 if(-0.08<X && X<0.08){
 if(-0.08<Y && Y<0.08){
 if(-0.08<Z && Z<0.08){
 Serial1.println("N");
 Serial.println("Fallen!");
 loop(); //if condition is met, jump to loop to wait for push button to be pressed again.
 }
 }
 }
 //else{
 printAccel(); //else continue printing sensor data
 //}
}

However I am having some trouble getting it to work, above is what I have worked out thus far.

asked Nov 10, 2014 at 14:19
2
  • What does your DFA look like? Commented Nov 10, 2014 at 14:29
  • @IgnacioVazquez-Abrams I'm a little new to this, what does DFA stands for? Commented Nov 10, 2014 at 14:40

1 Answer 1

3

You'll need to store whether the button has been pressed.

bool buttonActivated = false;
...
void loop(){
 buttonState = digitalRead(buttonPin); //Read push button logic.
 if (buttonState == 1 ) {
 buttonActivated = true;
 }
 if( buttonActivated ) {
 printAccel(); //print sensor data
 }
}

to deactivate, use:

buttonActivated = false;
answered Nov 10, 2014 at 16:54
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.