LSM303D 3D Compass and Accelerometer
The LSM303DLM consists of a digital 3-axis accelerometer and 3-axis magnetometer. It combines the two into a single package that is ideal for making a tilt-compensated compass.
The six independent magnetic and acceleration readings are available through an I2C interface.
Can be bought from: http://www.google.com/url?q=http%3A%2F%2Fwww.pololu.com%2Fproduct%2F1273&sa=D&sntz=1&usg=AFQjCNEaN451q_XPZpHn8AK5-zigc1fkpg
Datasheet can be accessed here: http://www.pololu.com/file/download/LSM303DLM.pdf?file_id=0J514
Connections
A minimum of four connections are necessary: VIN, GND, SCL and SDA
External Libraries
The LSM303D library was used to interface the mbed with the LSM303DLM compass and accelerometer device. Documentation can be found here:
[Repository '/users/DrCoyle/code/LSM303D/' not found]
Code
Import programLSM303D_SPI
5-level Ball Game. Avoid hitting the lines by controlling the ball. See more info at: https://developer.mbed.org/users/shurjo_1234/notebook/lsm303d-3d-compass-and-accelerometer/
Using the LSM303D library
#include "mbed.h" #include "LSM303D.h" SPI spi(p5, p6, p7); // mosi, miso, sclk LSM303D sensor1(spi,p15); LSM303D sensor2(spi,p16); LSM303D sensor3(spi,p17); LSM303D sensor4(spi,p18); #include "uLCD_4DGL.h" uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; int main() { // Chip must be deselected sensor1.initialize(); float vx_old = sensor1.magnitometer(2); float fx=63.0,fy=2.0,vx= 0.0,vy=0.4; int x=63,y=2,radius=2; uLCD.background_color(BLACK); uLCD.cls(); int pts[20]; for (int level=1; level<=5; level++) { if (level == 1) { pts[0] = 30; pts[1] = 63; pts[2] = 96; pts[3] = 63; } else if (level == 2) { pts[0] = 0; pts[1] = 40; pts[2] = 50; pts[3] = 40; pts[4] = 76; pts[5] = 80; pts[6] = 126; pts[7] = 80; } else if (level == 3) { pts[0] = 0; pts[1] = 30; pts[2] = 50; pts[3] = 30; pts[4] = 76; pts[5] = 60; pts[6] = 126; pts[7] = 60; pts[8] = 76; pts[9] = 90; pts[10] = 126; pts[11] = 90; } else if (level == 4) { pts[0] = 0; pts[1] = 28; pts[2] = 50; pts[3] = 28; pts[4] = 76; pts[5] = 56; pts[6] = 126; pts[7] = 56; pts[8] = 76; pts[9] = 84; pts[10] = 126; pts[11] = 84; pts[12] = 76; pts[13] = 112; pts[14] = 126; pts[15] = 112; } else { pts[0] = 0; pts[1] = 20; pts[2] = 50; pts[3] = 20; pts[4] = 76; pts[5] = 40; pts[6] = 126; pts[7] = 40; pts[8] = 76; pts[9] = 60; pts[10] = 126; pts[11] = 60; pts[12] = 76; pts[13] = 80; pts[14] = 126; pts[15] = 80; pts[16] = 76; pts[17] = 100; pts[18] = 126; pts[19] = 100; } uLCD.cls(); for (int i=0; i<level; i++) { uLCD.line(pts[4*i], pts[4*i+1], pts[4*i+2], pts[4*i+3], WHITE); } y = 2; fy = 2; while (y<126) { vx = (sensor1.magnitometer(2)-vx_old)/100; //draw ball uLCD.filled_circle(x, y, radius, RED); //bounce off edge walls and slow down a bit? for (int i=0; i<level; i++) { if (x>pts[4*i] && x<pts[4*i+2] && y>pts[4*i+1]-1 && y<pts[4*i+3]+1) { uLCD.cls(); uLCD.text_width(4); //4X size text uLCD.text_height(4); uLCD.color(RED); uLCD.locate(0,0); uLCD.printf("GAME OVER"); return 0; } } uLCD.filled_circle(x, y, radius, BLACK); //move ball fx=fx+vx; fy=fy+vy; x=(int)fx; y=(int)fy; if (x<0) { x += 126; fx = x; } if (x>126) { x -= 126; fx = x; } } uLCD.cls(); uLCD.text_width(4); //4X size text uLCD.text_height(4); uLCD.color(RED); uLCD.locate(1,1); if (level <= 4) uLCD.printf("%2D",level+1); else { uLCD.locate(1,1); uLCD.printf("YOU WIN"); } wait(1); } }
DEMO
Please log in to post comments.