Simple program to send ADXL345 accelerometer readings to a PC/Laptop over serial port. Also wrote an ADXL345 library
main.cpp@0:48c465d803f7, 2012-05-12 (annotated)
- Committer:
- simonbarker
- Date:
- Sat May 12 16:16:37 2012 +0000
- Revision:
- 0:48c465d803f7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simonbarker | 0:48c465d803f7 | 1 | #include "mbed.h" |
simonbarker | 0:48c465d803f7 | 2 | #include "ADXL345sb.h" |
simonbarker | 0:48c465d803f7 | 3 | |
simonbarker | 0:48c465d803f7 | 4 | DigitalOut myled(LED1); |
simonbarker | 0:48c465d803f7 | 5 | ADXL345sb accel(p5, p6, p7, p8); // mosi, miso, sclk, ncs |
simonbarker | 0:48c465d803f7 | 6 | |
simonbarker | 0:48c465d803f7 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
simonbarker | 0:48c465d803f7 | 8 | |
simonbarker | 0:48c465d803f7 | 9 | int main() { |
simonbarker | 0:48c465d803f7 | 10 | |
simonbarker | 0:48c465d803f7 | 11 | float readings[3]; |
simonbarker | 0:48c465d803f7 | 12 | |
simonbarker | 0:48c465d803f7 | 13 | while(1) { |
simonbarker | 0:48c465d803f7 | 14 | wait(0.1); |
simonbarker | 0:48c465d803f7 | 15 | |
simonbarker | 0:48c465d803f7 | 16 | accel.adxlwrite(DATA_FORMAT,0x01); |
simonbarker | 0:48c465d803f7 | 17 | accel.adxlwrite(POWER_CTL ,0x08); |
simonbarker | 0:48c465d803f7 | 18 | |
simonbarker | 0:48c465d803f7 | 19 | accel.adxlreadXYZ(readings); |
simonbarker | 0:48c465d803f7 | 20 | |
simonbarker | 0:48c465d803f7 | 21 | float angleX = acos(readings[0]); |
simonbarker | 0:48c465d803f7 | 22 | float angleY = acos(readings[1]); |
simonbarker | 0:48c465d803f7 | 23 | float angleZ = acos(readings[2]); |
simonbarker | 0:48c465d803f7 | 24 | |
simonbarker | 0:48c465d803f7 | 25 | if(isnan(angleX)) |
simonbarker | 0:48c465d803f7 | 26 | angleX = 0; |
simonbarker | 0:48c465d803f7 | 27 | if(isnan(angleY)) |
simonbarker | 0:48c465d803f7 | 28 | angleY = 0; |
simonbarker | 0:48c465d803f7 | 29 | if(isnan(angleZ)) |
simonbarker | 0:48c465d803f7 | 30 | angleZ = 0; |
simonbarker | 0:48c465d803f7 | 31 | |
simonbarker | 0:48c465d803f7 | 32 | printf("%f,%f,%f,\r\n",angleX,angleY,angleZ); |
simonbarker | 0:48c465d803f7 | 33 | } |
simonbarker | 0:48c465d803f7 | 34 | } |