/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter

« back to all changes in this revision

Viewing changes to test/accelerometer/main.ino

  • Committer: Tim Marston
  • Date: 2013-01-16 21:42:06 UTC
  • Revision ID: tim@ed.am-20130116214206-5mw4cgsya1h0yiyq
updated arduino.mk

Show diffs side-by-side

added added

removed removed

1
 
/************************************************************************
2
 
* This program is free software; you can redistribute it and/or modify *
3
 
* it under the terms of the GNU License V2.                            *
4
 
* This program is distributed in the hope that it will be useful,      *
5
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of       *
6
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
7
 
* GNU General Public License, version 2 for more details               *
8
 
*                                                                      *
9
 
* Bare bones ADXL345 i2c example for Arduino 1.0                       *
10
 
* by Jens C Brynildsen <http://www.flashgamer.com>                     *
11
 
* This version is not reliant of any external lib                      *
12
 
* (Adapted for Arduino 1.0 from http://code.google.com/p/adxl345driver)*
13
 
*                                                                      *
14
 
* Demonstrates use of ADXL345 (using the Sparkfun ADXL345 breakout)    *
15
 
* with i2c communication. Datasheet:                                   *
16
 
* http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf *
17
 
* If you need more advanced features such as freefall and tap          *
18
 
* detection, check out:                                                *
19
 
* https://github.com/jenschr/Arduino-libraries                         *
20
 
***********************************************************************/
21
 
 
22
 
// Cabling for i2c using Sparkfun breakout with an Arduino Uno / Duemilanove:
23
 
// Arduino <-> Breakout board
24
 
// Gnd      -  GND
25
 
// 3.3v     -  VCC
26
 
// 3.3v     -  CS
27
 
// Analog 4 -  SDA
28
 
// Analog 5 -  SLC
29
 
 
30
 
// Cabling for i2c using Sparkfun breakout with an Arduino Mega / Mega ADK:
31
 
// Arduino <-> Breakout board
32
 
// Gnd      -  GND
33
 
// 3.3v     -  VCC
34
 
// 3.3v     -  CS
35
 
// 20       -  SDA
36
 
// 21       -  SLC
37
 
 
38
 
#include <Wire.h>
39
 
 
40
 
#define DEVICE (0x53) // Device address as specified in data sheet 
41
 
 
42
 
byte _buff[6];
43
 
 
44
 
char POWER_CTL = 0x2D;  //Power Control Register
45
 
char DATA_FORMAT = 0x31;
46
 
char DATAX0 = 0x32;     //X-Axis Data 0
47
 
char DATAX1 = 0x33;     //X-Axis Data 1
48
 
char DATAY0 = 0x34;     //Y-Axis Data 0
49
 
char DATAY1 = 0x35;     //Y-Axis Data 1
50
 
char DATAZ0 = 0x36;     //Z-Axis Data 0
51
 
char DATAZ1 = 0x37;     //Z-Axis Data 1
52
 
 
53
 
 
54
 
void writeTo(byte address, byte val) {
55
 
  Wire.beginTransmission(DEVICE); // start transmission to device 
56
 
  Wire.write(address);             // send register address
57
 
  Wire.write(val);                 // send value to write
58
 
  Wire.endTransmission();         // end transmission
59
 
}
60
 
 
61
 
// Reads num bytes starting from address register on device in to _buff array
62
 
void readFrom(byte address, int num, byte _buff[])
63
 
{
64
 
  Wire.beginTransmission(DEVICE); // start transmission to device 
65
 
  Wire.write(address);             // sends address to read from
66
 
  Wire.endTransmission();         // end transmission
67
 
 
68
 
  Wire.beginTransmission(DEVICE); // start transmission to device
69
 
  Wire.requestFrom(DEVICE, num);    // request 6 bytes from device
70
 
 
71
 
  // device may send less than requested (abnormal)
72
 
  int i = 0;
73
 
  while(Wire.available())
74
 
  { 
75
 
    _buff[i] = Wire.read();    // receive a byte
76
 
    i++;
77
 
  }
78
 
  Wire.endTransmission();         // end transmission
79
 
}
80
 
 
81
 
 
82
 
void setup()
83
 
{
84
 
  // join i2c bus (address optional for master)
85
 
  Wire.begin();
86
 
  // start serial for output. Make sure you set your Serial Monitor to the
87
 
  // same!
88
 
  Serial.begin(9600);
89
 
  Serial.print("init\n");
90
 
  
91
 
  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the
92
 
  //DATA_FORMAT register.
93
 
  writeTo(DATA_FORMAT, 0x01);
94
 
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL
95
 
  //register.
96
 
  writeTo(POWER_CTL, 0x08);
97
 
}
98
 
 
99
 
void readAccel() {
100
 
  //read the acceleration data from the ADXL345
101
 
  uint8_t howManyBytesToRead = 6;
102
 
  readFrom( DATAX0, howManyBytesToRead, _buff); 
103
 
 
104
 
  // each axis reading comes in 10 bit resolution, ie 2 bytes.  Least
105
 
  // Significat Byte first!!  thus we are converting both bytes in to one int
106
 
  int x = (((int)_buff[1]) << 8) | _buff[0];   
107
 
  int y = (((int)_buff[3]) << 8) | _buff[2];
108
 
  int z = (((int)_buff[5]) << 8) | _buff[4];
109
 
  Serial.print("x: ");
110
 
  Serial.print( x );
111
 
  Serial.print(" y: ");
112
 
  Serial.print( y );
113
 
  Serial.print(" z: ");
114
 
  Serial.println( z );
115
 
}
116
 
 
117
 
 
118
 
 
119
 
void loop()
120
 
{
121
 
  readAccel(); // read the x/y/z tilt
122
 
  delay(500); // only read every 0,5 seconds
123
 
}