3-Axis Digital Accelerometer

Dependencies:   MMA7660FC WebSocketClient WiflyInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WiflyInterface.h"
00003 #include "Websocket.h"
00004 #include "MMA7660FC.h"
00005  
00006 
00007 /* wifly interface:
00008 *     - p9 and p10 are for the serial communication
00009 *     - p19 is for the reset pin
00010 *     - p26 is for the connection status
00011 *     - "mbed" is the ssid of the network
00012 *     - "password" is the password
00013 *     - WPA is the security
00014 */
00015 WiflyInterface wifly(p13, p14, p19, p26, "Nexus 5", "1234qwer", WPA);
00016 DigitalOut myled1(LED1);
00017 DigitalOut myled2(LED2);
00018 DigitalOut myled3(LED3);
00019 DigitalOut myled4(LED4);
00020 
00021 #define ADDR_MMA7660 0x98                   // I2C SLAVE ADDR MMA7660FC
00022 
00023 MMA7660FC Acc(p28, p27, ADDR_MMA7660);      //sda, scl, Addr
00024 Serial pc(USBTX, USBRX);
00025 
00026 
00027 float G_VALUE[64] = {0, 0.047, 0.094, 0.141, 0.188, 0.234, 0.281, 0.328, 0.375, 0.422, 0.469, 0.516, 0.563, 0.609, 0.656, 0.703, 0.750, 0.797, 0.844, 0.891, 0.938, 0.984, 1.031, 1.078, 1.125, 1.172, 1.219, 1.266, 1.313, 1.359, 1.406, 1.453, -1.500, -1.453, -1.406, -1.359, -1.313, -1.266, -1.219, -1.172, -1.125, -1.078, -1.031, -0.984, -0.938, -0.891, -0.844, -0.797, -0.750, -0.703, -0.656, -0.609, -0.563, -0.516, -0.469, -0.422, -0.375, -0.328, -0.281, -0.234, -0.188, -0.141, -0.094, -0.047};
00028 
00029 int main() {
00030     wifly.init(); //Use DHCP
00031     //wifly.init("192.168.21.33","255.255.255.0","192.168.21.2");
00032     while (!wifly.connect());
00033     myled4=1;
00034     printf("IP Address is %s\n\r", wifly.getIPAddress());
00035 
00036     Websocket ws("ws://wot.city/object/accelerometer/send");
00037     while (!ws.connect());
00038     myled2=1;
00039  
00040    
00041     Acc.init();                                                     // Initialization
00042     pc.printf("Value reg 0x06: %#x\n", Acc.read_reg(0x06));         // Test the correct value of the register 0x06
00043     pc.printf("Value reg 0x08: %#x\n", Acc.read_reg(0x08));         // Test the correct value of the register 0x08
00044     pc.printf("Value reg 0x07: %#x\n\r", Acc.read_reg(0x07));       // Test the correct value of the register 0x07
00045            
00046     
00047  
00048     while (1) {
00049         char data[256];
00050        
00051         myled4=1;
00052         float x=0, y=0, z=0;
00053         float ax=0, ay=0, az=0;
00054         
00055         Acc.read_Tilt(&x, &y, &z);                                  // Read the acceleration                    
00056         pc.printf("Tilt x: %2.2f degree \n", x);                    // Print the tilt orientation of the X axis
00057         pc.printf("Tilt y: %2.2f degree \n", y);                    // Print the tilt orientation of the Y axis
00058         pc.printf("Tilt z: %2.2f degree \n", z);                    // Print the tilt orientation of the Z axis
00059 
00060         wait_ms(100);
00061 
00062         
00063         
00064         ax = G_VALUE[Acc.read_x()];
00065         ay = G_VALUE[Acc.read_y()];
00066         az = G_VALUE[Acc.read_z()];
00067         
00068         if(ax<0){
00069             myled1=1;
00070         }else{
00071             myled1=0; 
00072         }
00073         if(ay<0){
00074             myled2=1;
00075         }else{
00076             myled2=0; 
00077         }
00078         if(az<0){
00079             myled3=1;
00080         }else{
00081             myled3=0; 
00082         }
00083         
00084         wait(1);
00085         sprintf( data , "{ \"ax\": %f, \"ay\": %f, \"az\": %f\n}",ax,ay,az);
00086         ws.send(data);
00087         
00088         //wait(1.0);
00089     }
00090 }