I want to use 3-axis accelerometer, to tell me when something is moving or sitting still.
The accelerometer code to the arduino works great.
What I need help on is with the if else code. Is there some way I can capture the (x) int and if it stays the
same # for ever then the object is not moving. (Not moving object)
If the (x) capture is moving then the code would say moving. (Moving)
#include <Wire.h>
#include <ADXL345.h>
ADXL345 adxl;
void setup(){
Serial.begin(9600);
adxl.powerOn();
adxl.setActivityThreshold(75); //62.5mg per increment
adxl.setInactivityThreshold(75); //62.5mg per increment
adxl.setTimeInactivity(10); // how many seconds of no activity is inactive?
//look of activity movement on this axes - 1 == on; 0 == off
adxl.setActivityX(1);
adxl.setActivityY(1);
adxl.setActivityZ(1);
//look of inactivity movement on this axes - 1 == on; 0 == off
adxl.setInactivityX(1);
adxl.setInactivityY(1);
adxl.setInactivityZ(1);
//look of tap movement on this axes - 1 == on; 0 == off
adxl.setTapDetectionOnX(0);
adxl.setTapDetectionOnY(0);
adxl.setTapDetectionOnZ(1);
//set values for what is a tap, and what is a double tap (0-255)
adxl.setTapThreshold(50); //62.5mg per increment
adxl.setTapDuration(15); //625us per increment
adxl.setDoubleTapLatency(80); //1.25ms per increment
adxl.setDoubleTapWindow(200); //1.25ms per increment
//set values for what is considered freefall (0-255)
adxl.setFreeFallThreshold(7); //(5 - 9) recommended - 62.5mg per increment
adxl.setFreeFallDuration(45); //(20 - 70) recommended - 5ms per increment
//setting all interrupts to take place on int pin 1
//I had issues with int pin 2, was unable to reset it
adxl.setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_FREE_FALL_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT, ADXL345_INT1_PIN );
//register interrupt actions - 1 == on; 0 == off
adxl.setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
adxl.setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
adxl.setInterrupt( ADXL345_INT_FREE_FALL_BIT, 1);
adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT, 1);
adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
}
void loop(){
int one;
int two;
int x,y,z;
adxl.readXYZ(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z
Serial.println(x);
one = x + 1;
two = one - 1;
if (one==two)
{
if (x==x)
{
if (x==x)
{
if (x==x)
{
Serial.print("off");
}
}
}
}
else
{
Serial.print("on=");
delay(500);
}
}
I want to use 3-axis accelerometer, to tell me when something is moving or sitting still.
Well you are going to be out of luck there, because an accelerometer is only going to tell you if something is accelerating, or not accelerating.
An accelerometer attached to an object which is moving with a constant velocity, is going to give you exactly the same answer if the object is sitting still.
So what would be the best sensor for something moving or not moving, for the Arduino.
What I want to know with the 3-axis accelerometer, is I want to put in inside my dryer and tell when it is moving or off,
with wireless.
BenBenBen:
So what would be the best sensor for something moving or not moving, for the Arduino.
What I want to know with the 3-axis accelerometer, is I want to put in inside my dryer and tell when it is moving or off,
with wireless.
If it is a cloths drier, put an AC relay across the drum motor leads and then monitor the relay contact status.
With that being said, How can I use the if else to capture the (x) value?
Is there some way to save the (x) value and then read it. If it is = to the saved value then dryer is off.
It is not hard to tell if the accelerometer is being spun around in your dryer. Please know that you need to understand the following concepts: vector, running average and standard deviation.
In case you know them, skip past this paragraph. A vector is a quantity with 3 components, such as an arrow pointing from origin to a location. Your 3-axis accelerometer gives you 3 components of the vector force felt by the accelerometer. You need to reconstruct this vector's magnitude (length of the arrow) from the three components the accelerometer gives you. A running average takes the average of the most recent x number of samples and average them, so if more samples are taken, oldest samples are removed so the running average will follow the trend of change but not jump up and down as much as each sample, due to various reasons. A standard deviation is telling you how spread out your samples are in a set (say 10 samples). The more spread out your samples are, the larger your standard deviation.
OK, once you know these concepts, construct the vector magnitude from the 3 components you measure. Then start doing running average on this magnitude. So every time you read a new set of 3 components, add a new magnitude to the the running average. Then calculate standard deviation. If you sense your magnitude's running average to be around 10, say between 9 and 11, and your standard deviation is less than say 0.5, you are not moving. If your standard deviation is greater than say 1 or magnitude's running average is say "below 8 or above 12", then you are "moving". If your values fall in the gaps I didn't specify, stick with the previous state.
I have a running average class you can use. I believe Rob on the forum has one too.
Is there some way to save the (x) value and then read it. If it is = to the saved value then dryer is off.
Of course, and that's exactly how you'd do it. What part of that are you having problems with?
Of course, one value that differs is not a sure sign that the dryer is moving, so don't send a message immediately.
As others have pointed out, though, there are far easier ways to determine if the dryer is in use.
Putting the accelerometer in the damp, hot dryer is not going to make it last long. Certainly putting the radio in the dryer is a really bad idea. The radio won't be able to get a signal out of the metal drum in the metal box.
How to connect to a device inside the drier drum would be a most interesting exercise. Maybe put a brick inside the drier along with the cloths and then monitor the banging and clanging with some type of acoustic monitor..
There are several different methods that don't include abuse to small "Delicate" pieces of electronics equipment...
Arranged in order of danger/complexity...
Use a microphone to sense/report the motor running.- -> Simple safe and easy although a delay of 3 to 5 minutes might be required to insure that the machine cycle is ended rather than between cycles..
Use a hall effect current sensor to indicate that the device is drawing current above a threshold or limit. - -> requires 110/220VAC connections and might be an issue for the Equipment Warranty but reasonably safe as most if not all Hall Effect devices inherently are isolated from the load power. The advantage here is that the Washer/dryer needn't be opened as the sensor can be connected externally mounted in a box along with the I/O connector(s).
Use a relay wired across the drum motor connections to sense when the motor is powered up... Not the best of the three... At All but very do-able.,
The addition of complex reporting methods IE radio can be left to reporting the monitored event... Where IMO they belong.
The hardest lesson that an engineer must learn to be good... is the K.I.S.S. method of engineering.
The fewer components in any design generally mean the less components to break or wear out/use up.