# Arduino Forum

## Forum 2005-2010 (read only) => General => Exhibition => Topic started by: jezuz on Feb 26, 2009, 03:04 am

Title: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Feb 26, 2009, 03:04 am
I made a little project using the ADX330 accelerometer and my arduino... I know its simple and its been done before (which I found out to my dismay AFTER i made this) but I am quite proud cause I am only a few weeks into the arduino and I made this code from scratch - all it took was the consumption of 10 cups of coffee and a few head scratching moments...  ;)

anyway, enjoy:

Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: mem on Feb 26, 2009, 10:13 am
Well done. I hope you enjoyed getting that working. What do you have planned for your next project?
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: foxweb on Feb 26, 2009, 03:55 pm
2 jezuz

I bought the accelerometer (http://www.sparkfun.com/commerce/product_info.php?products_id=8462) and used it in 3D Ruby program for control of 3D-cube (http://foxweb.net.ru/test/arduino/ruby/) :)
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Feb 26, 2009, 09:51 pm
thx guys, il post the code when i get home... i took a look at 3d ruby... and it seems pretty awesome, il play around with that a little, but my next project is a lillypad + accelerometer robotic hand cntrol project... im still working on the code behind it but its actually fairly simple...  il let u knw when im done  ;D
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Feb 27, 2009, 02:12 am
iight guys here is the code...

int xpin = 3;                   // x-axis of the accelerometer
int ypin = 2;                   // y-axis
int zpin = 1;                   // z-axis (only on 3-axis models)
long xy_max = 470;
long xy_min = 550;

int forwardPin = 9;
int backPin = 10;
int leftPin = 11;
int rightPin = 12;
int centerPin = 8;

long x = 0;
long y = 0;

void setup()
{
Serial.begin(9600);

pinMode(forwardPin, OUTPUT);
pinMode(backPin, OUTPUT);
pinMode(leftPin, OUTPUT);
pinMode(rightPin, OUTPUT);

pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}

void loop()
{

digitalWrite(centerPin, LOW);

if (y < xy_max) { //backwards

digitalWrite(backPin, HIGH);

Serial.println("The arduino is moving(tilted) backwards");

} else if (y >= xy_max){

digitalWrite(backPin, LOW);

}

if (y > xy_min) { //forwards

digitalWrite(forwardPin, HIGH);

Serial.println("The arduino is moving(tilted) forwards");

} else if (y <= xy_min){

digitalWrite(forwardPin, LOW);

}

if (x > xy_min) { //left

digitalWrite(leftPin, HIGH);

Serial.println("The arduino is moving(tilted) left");

} else if (x <= xy_min){

digitalWrite(leftPin, LOW);

}

if (x < xy_max) { //right

digitalWrite(rightPin, HIGH);

Serial.println("The arduino is moving(tilted) right");

}  else if (x >= xy_max){

digitalWrite(rightPin, LOW);

}

if (y >= xy_max && y <= xy_min && x <= xy_min && x >= xy_max)     { // nothing

digitalWrite(forwardPin, LOW);
digitalWrite(backPin, LOW);
digitalWrite(centerPin, HIGH);
digitalWrite(leftPin, LOW);
digitalWrite(rightPin, LOW);
Serial.println("The arduino is currently not moving(tilted) in any direction");
}
}
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: mem on Feb 27, 2009, 09:46 am
Thanks for sharing.

You can simplify the position checks, for example you can do the y position check as follows:

[font=Courier New]
if (y < xy_max) { //backwards
digitalWrite(backPin, HIGH);
Serial.println("The arduino is moving(tilted) backwards");
}
else{
digitalWrite(backPin, LOW);
}[/font]

This works because if y is not less than xy_max it must be greater than or equal , so you don't need to do that check

If you just wanted to control the led without the serial message you can simplify further. The following code will set the pin HIGH when x  is less than xy_max and low otherwise

[font=Courier New]digitalWrite(backPin,  (x<xy_max) );[/font]

the expression (x<xy_max)  evaluates to true (the same as HIGH) if x  is less than xy_max and false (the same as LOW) otherwise.

Have fun!
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Feb 27, 2009, 11:09 pm
ure absolutely right, i just did that so it was easier to understand for me (i was doing it late, and when im tired i always try to write out code completely so that i understand it better, and thus dont make any stupid mistakes) but thx:) and il have some pix of the robotic arm up later tonight if ure interested ;)
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: mem on Feb 27, 2009, 11:23 pm
Looking forward to seeing the pictures  :)
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Mar 04, 2009, 04:20 am
the project has been put on hold due to difficulties with the damn robotic arm's motors... and im quite busy with schoolwork at the time... il be sure to get right back ontop of the work as soon as i can...  ;)
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Mar 26, 2009, 01:57 am
ok, so i pcb'd the project..finally... but i was out of the good wires so i ended up just putting some crappy short ones so that atleast i could test if it sorta works, and so it would look valid... here are the picks:

http://img410.imageshack.us/gal.php?g=dscn0994.jpg
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: aashka on Jul 05, 2010, 09:11 am
hey I am using Triple Axis Accelerometer Breakout - MMA7260Q. I dont understand the behavior of accelerometer. From where can I know xymin and xymax ? I tried to figure it out by myself. COuldnt do it.. please help if someone knows..
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: JanGoXX on Jul 27, 2010, 05:29 pm
Nice :-)
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: duemilanove on Jul 30, 2010, 10:38 pm
Nice project!  :) I'm going to start working with accelerometers too with the wii nunchuck. I have the connector from SparkFun (http://www.sparkfun.com/commerce/product_info.php?products_id=9281)
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Jul 31, 2010, 07:19 pm
Thanks guys. Blast from the past, though (this thing must have been on page 30 by now).
Quote

I'm going to start working with accelerometers too with the wii nunchuck. I have the connector from SparkFun

I tried my luck with the wii nunchuck and it failed me miserably (I didn't use the premade connector tho, so perhaps I mucked up when resoldering the breakout wires  :'(). If you get yours working, I'd enjoy seeing it in action.  :)
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: reha on Aug 23, 2010, 12:14 am
hey I am using Triple Axis Accelerometer Breakout - MMA7260Q. I dont understand the behavior of accelerometer. From where can I know xymin and xymax ? I tried to figure it out by myself. COuldnt do it.. please help if someone knows..

Hello,
freescale published a nice application notes ( http://www.freescale.com/files/sensors/doc/app_note/AN3107.pdf  ) I guess it can help you. regards, reha
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: evantheking on Oct 17, 2010, 09:03 pm
what are these values :

long xy_max = 470;
long xy_min = 550;

Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Oct 17, 2010, 10:21 pm
Quote
what are these values :

long xy_max = 470;
long xy_min = 550;

These are the limits for the values to use in the "if" statements.

They are backwards, as you can tell (max < min), but that's really just semantics.

What happens is a set of if statements check to see if the accelerometer x and y outputs are greater than (in this case) the min, or less than the x, and light up the led's accordingly.
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: gabertzz on Jan 19, 2011, 04:33 pm
Sir where we can buy that kind of Accelerometer? Thanks for reply
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: hypermiler on Jan 20, 2011, 03:34 am
Great post, thanks for sharing. I'm looking forward to trying this with an MMA3671L, which is 3-axis. I'm sort of unclear how you derived your constants (xy_max and xy_min). Were these part of the specification for your chip, or did you derive these experimentally by just tilting the board around and looking at outputs on serial? Thanks!
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: hypermiler on Jan 20, 2011, 07:33 am
So, I did this using just the X and Y accel pins from a 5dof board (http://www.sparkfun.com/products/9268), and had to recalibrate the xy_min and xy_max to:

long xy_max = 300;
long xy_min = 350;

...but got it working!

I did the recalibration by just examining the serial output for each pin as I moved the board in space and noting the values as I moved it:

Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: gabertzz on Jan 23, 2011, 01:49 pm
Good day! Sir can you post the schematic of your project. We will make a project like this. Thanks
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: jezuz on Jan 23, 2011, 08:35 pm
@Hypermiler

Cool, nice to see my project is still being reused.

@Gabertzz
Sorry I didn't get back to you sooner, I don't monitor this thread.

Here's the link to the accelerometer I used:

http://www.sparkfun.com/products/9269

The code is self explanatory, I think, but if anyone needs a schematic PM/email me and I'll draw one up.
Title: Re: Arduino + Accelerometer: Tilt Detection
Post by: vudangkhoa on Jan 24, 2011, 12:09 am
This project is great.