Pages: [1] 2   Go Down
Author Topic: Arduino + Accelerometer: Tilt Detection  (Read 19701 times)
0 Members and 1 Guest are viewing this topic.
New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

anyway, enjoy:

Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Russia, Moscow
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Russian Arduino Community
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

2 jezuz

Please, post your source code.

I bought the accelerometer and used it in 3D Ruby program for control of 3D-cube smiley
Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: February 26, 2009, 03:51:49 pm by jezuz » Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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");
}
}
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:


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


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

    digitalWrite(backPin,  (x<xy_max) );

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!
« Last Edit: February 27, 2009, 03:46:39 am by mem » Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looking forward to seeing the pictures  smiley
Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...  smiley-wink
Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice :-)
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1] 2   Go Up
Jump to: