# How to detect change itself, rather than change resulting in a specific number

Thread Starter

#### hbasile91

Joined Dec 6, 2020
125
Hi

I am working on a visual accelerometer, 3 axis, adxl 335, pins on the device= Vin, 3vo, GND, Z out, Y out, X out, and test.
I am running this program in arduino. Its working - when there's motion in y, a green LED goes on, then back to off state when back in its original position. Same for Z axis, but with a blue LED. BUT - - not x...

I am asking for advice on how to program a digitalwrite to LED when there's CHANGE in X axis, rather than when X integer = or != a specific value.

im doing this on one axis, the x, and not all axis

the reason im trying to do this is that my accelerometer seems to give back slowly increasing values even when the device isn't experiencing motion of any kind, but ONLY on the X axis. So, the its impossible to program the LED if the value never returns to its original number. It just keeps going up and up forever.

The other two axis, y and z, are working perfectly, and I've been able to program their respective, designated LEDs when I move the device in those directions. Maybe the accelerometer is just broken! I don't know.

Ive seen a few examples for this type of thing online, but I need to know the ACTUAL terms in the programming language that will help me accomplish this. I am a beginner.

one approach is to subtract trend = new value - old value, and if trend >< 1, etc ..then digitalwrite to LED
BUT - - what are the actual terms I would use in the sketch?!

THANKS FOR YOUR HELP
-a beginner arduino fan

BTW, this is my code if you want to see it:

C:
const int xpin = A0;
const int ypin = A1;
const int zpin = A2;

const int apin = 6;
const int bpin = 5;
const int cpin = 3;

void setup()
{
pinMode (xpin, INPUT);
pinMode (ypin, INPUT);
pinMode (zpin, INPUT);

pinMode (apin, OUTPUT);
pinMode (bpin, OUTPUT);
pinMode (cpin, OUTPUT);

Serial.begin (9600);
}

void loop()

{
float x = analogRead(xpin);
delay (500);
float y = analogRead(ypin);
delay (500);
float z = analogRead(zpin);

Serial.print (x);   //find a way to change this output to LEDs
Serial.print ("\t");

Serial.print (y);   //find a way to change this output to LEDs
Serial.print ("\t");

Serial.print (z);   //find a way to change this output to LEDs
Serial.print ("\n");

if (x < 678)
{
digitalWrite (apin, HIGH);
}
if (y <= 464)
{
digitalWrite (bpin, HIGH);
}
if (z <= 4 )
{
digitalWrite (cpin, HIGH);
}
if (x >= 680)
{
digitalWrite (apin, LOW);
}
if (y >= 674)
{
digitalWrite (bpin, LOW);
}
if (z >= 6)
{
digitalWrite (cpin, LOW);
}
}
Moderators note : please use code tags for pieces of code

Last edited by a moderator:
Thread Starter

#### hbasile91

Joined Dec 6, 2020
125
also if anyone has the name of a good book or site to learn this stuff. I read a beginners book, but am looking for more

#### BobTPH

Joined Jun 5, 2013
3,334
Rather than working around it, why not fond it why the x axis is giving incorrect results?

If you swap the connections for x and y does the problem move to the y led? If so, the sensor is the problem, if it remains on the x led then the problem is in reading and interpreting the inputs.

Bob

• atferrari and hbasile91
Thread Starter

#### hbasile91

Joined Dec 6, 2020
125
thank you so much! youre right, the problem is now the Y axis

Thread Starter

#### hbasile91

Joined Dec 6, 2020
125
Rather than working around it, why not fond it why the x axis is giving incorrect results?

If you swap the connections for x and y does the problem move to the y led? If so, the sensor is the problem, if it remains on the x led then the problem is in reading and interpreting the inputs.

Bob
thank you so much! you're right the problem is now the Y axis

#### MrSalts

Joined Apr 2, 2020
286
Every accelerometer suffers from drift and angular random walk and bias instability. Also, if you have a simple three axis accelerometer, not much you can do except software subtraction of a constant rate of change and hope you can update the rate of change occasionally but you'll have to know when the device is not moving to update the drift.
Otherwise, a six axis or 9-axis accelerometer with gyroscopes and temperature compensation and...

in arduino, you can use the millis() or micros() command to get current time since the program was started or reset (or the timer rolled over as a long integer. Capture the current millis() to a variable and an "initial" x-axis value, then some time later capture millis() and "final" x-axis value. Then find a rate of change (x-initial - x-final)/(elapsed time) = rate of change.
You can apply this rate to the drifting access each time you take a measurement. Let's say it's 0.3 degrees per second. That is,
X, Y, Z at previously corrected measurement values (3 seconds ago)
X, Y, Z now with correction factor to X = X - (0.3°/second * 3 seconds) = X-0.9°

#### joeyd999

Joined Jun 6, 2011
4,477
Every accelerometer suffers from drift and angular random walk and bias instability. Also, if you have a simple three axis accelerometer, not much you can do except software subtraction of a constant rate of change and hope you can update the rate of change occasionally but you'll have to know when the device is not moving to update the drift.
Otherwise, a six axis or 9-axis accelerometer with gyroscopes and temperature compensation and...

in arduino, you can use the millis() or micros() command to get current time since the program was started or reset (or the timer rolled over as a long integer. Capture the current millis() to a variable and an "initial" x-axis value, then some time later capture millis() and "final" x-axis value. Then find a rate of change (x-initial - x-final)/(elapsed time) = rate of change.
You can apply this rate to the drifting access each time you take a measurement. Let's say it's 0.3 degrees per second. That is,
X, Y, Z at previously corrected measurement values (3 seconds ago)
X, Y, Z now with correction factor to X = X - (0.3°/second * 3 seconds) = X-0.9°
Even better, I like to push time variant data into a polynomial regression algorithm. In this case, drift and constant acceleration are reflected in first order terms. The 2nd (and higher) order terms are where the interesting stuff happens!

• MrSalts

#### MrSalts

Joined Apr 2, 2020
286
@joeyd999
Let him crawl before he starts walking.

#### djsfantasi

Joined Apr 11, 2010
7,700
@joeyd999
Let him crawl before he starts walking.
He doesn’t even know how to save the previous value nor test it!

@hbasile91 , this is (almost) the Arduino code you need. I don’t know how to get a new value (I could but didn’t read through all the code) or which pin you want to write to.
Code:
// set maxChange to some
// value that indicates a large
// change in X
maxChange = ?;
.
.
.
lastX = currentX;
currentX = getvalue(); // made up function
if (abs(currentX-lastX) > maxChange)
{
digitalWrite(yourPin);
// or anything you want
// when X changes a lot
};

Last edited:

#### MrSalts

Joined Apr 2, 2020
286
He doesn’t even know how to save the previous value nor test it!
That's why I said "learn to crawl before walk" instead of "learn to walk before run". @joeyd999 idea was way beyond the current skill set.

#### joeyd999

Joined Jun 6, 2011
4,477
@joeyd999 idea was way beyond the current skill set.
The story of my life.

#### MrSalts

Joined Apr 2, 2020
286
The story of my life.
The key to management is reading the skill set of the people you're dealing with. A safe bet is zero.

#### ericgibbs

Joined Jan 29, 2010
12,976
Moderation.
Please stay focussed on the TS's request and drop the bickering over other members posts.
If you have a problem with someone else's replies, use the Report feature.

E

Last edited:
Similar threads