aaron moreno / Mbed 2 deprecated ACCLCD341AM

Dependencies:   MMA8451Q SLCD mbed

Fork of ACC_LCD_341_MID by Stanley Cohen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers acc_341.cpp Source File

acc_341.cpp

00001 #include "mbed.h"
00002 #include "MMA8451Q.h"
00003 #include "SLCD.h"
00004 
00005 /* 
00006 Test of the accelerometer, digital I/O, on-board LCD screen.
00007  Looing at vector product of the x-y components of the accelerometer.
00008  Works pretty well. Still rough, program wise - sc 140710
00009  */
00010 
00011 #define DATATIME 0.200
00012 
00013 #define PROGNAME "ACCLCD341\r/n"
00014 
00015 #define PRINTDBUG
00016 // 
00017 #if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
00018   PinName const SDA = PTE25;  // Data pins for the accelerometer/magnetometer.
00019   PinName const SCL = PTE24;  // DO NOT CHANGE
00020 #elif defined (TARGET_KL05Z)
00021   PinName const SDA = PTB4;
00022   PinName const SCL = PTB3;
00023 #else
00024   #error TARGET NOT DEFINED
00025 #endif
00026 
00027 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00028 
00029 SLCD slcd; //define LCD display
00030 
00031 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
00032 Serial pc(USBTX, USBRX);
00033 
00034 float sqrt_newt(float argument) {
00035     return (sqrt(argument));
00036 }
00037    
00038 
00039 void LCDMess(char *lMess, float dWait){
00040         slcd.Home();
00041         slcd.clear();
00042         slcd.printf(lMess);
00043         wait(dWait);
00044 } 
00045 
00046 
00047 int main() {
00048     float xAcc;
00049     float yAcc;
00050     float vector;  
00051     char lcdData[10]; //buffer needs places dor decimal pt and colon
00052     
00053 #ifdef PRINTDBUG
00054         pc.printf(PROGNAME);
00055 #endif
00056 // main loop forever 
00057     while(true) {
00058 
00059 //Get accelerometer data - tilt angles minus offset for zero mark.
00060         xAcc = abs(acc.getAccX());
00061         yAcc = abs(acc.getAccY());     
00062  // Calulate vector sum of x and y reading.       
00063         vector = sqrt_newt(pow(xAcc,2) + pow(yAcc,2));
00064         
00065  
00066 #ifdef PRINTDBUG
00067         pc.printf("xAcc = %f\r\n", xAcc);
00068         pc.printf("yAcc = %f\r\n", yAcc);
00069         pc.printf("vector = %f\r\n",  vector);
00070 #endif
00071 
00072         sprintf (lcdData,"%4.3f",vector);
00073         LCDMess(lcdData, DATATIME);
00074 // Wait then do the whole thing again.
00075         wait(DATATIME);
00076     }
00077 }