Go Down

Topic: Arduino + Accelerometer: Tilt Detection (Read 21343 times) previous topic - next topic

jezuz

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:

http://www.youtube.com/watch?v=bk__bhQotFY

mem

Well done. I hope you enjoyed getting that working. What do you have planned for your next project?

foxweb


jezuz

#3
Feb 26, 2009, 09:51 pm Last Edit: Feb 26, 2009, 09:51 pm by jezuz Reason: 1
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

jezuz

iight guys here is the code...

int groundpin = 18;             // analog input pin 4
int powerpin = 19;              // analog input pin 5
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()
{

   
 x = analogRead(xpin);
 y = analogRead(ypin);
 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");
}
}

mem

#5
Feb 27, 2009, 09:46 am Last Edit: Feb 27, 2009, 09:46 am by mem Reason: 1
Thanks for sharing.

Here are some tips that may help you if you add to this sketch

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!

jezuz

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 ;)

mem

Looking forward to seeing the pictures  :)

jezuz

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...  ;)

jezuz

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

aashka

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..


duemilanove

Nice project!  :) I'm going to start working with accelerometers too with the wii nunchuck. I have the connector from SparkFun

jezuz

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.  :)

reha

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

Go Up