ok
Dependencies: MMA8451Q SLCD mbed
Fork of ACC_LCD_341_all_axies by
Diff: acc_341.cpp
- Revision:
- 3:c5291c70af48
- Parent:
- 2:6003ed409def
- Child:
- 4:1d559bac561a
--- a/acc_341.cpp Thu Sep 25 23:45:35 2014 +0000 +++ b/acc_341.cpp Sat Nov 05 23:46:00 2016 +0000 @@ -1,4 +1,5 @@ #include "mbed.h" +#include <math.h> #include "MMA8451Q.h" #include "SLCD.h" @@ -8,9 +9,9 @@ Works pretty well. Still rough, program wise - sc 140710 */ -#define DATATIME 0.200 +#define DATAINTERVAL 0.200 -#define PROGNAME "ACCLCD341\r/n" +#define PROGNAME "ACCLCD341-541\r\n" #define PRINTDBUG // @@ -30,48 +31,44 @@ MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); Serial pc(USBTX, USBRX); +Timer dataTimer; -float sqrt_newt(float argument) { - return (sqrt(argument)); -} -void LCDMess(char *lMess, float dWait){ +void LCDMess(char *lMess){ slcd.Home(); slcd.clear(); slcd.printf(lMess); - wait(dWait); } - +void initialize_global_vars(){ + pc.printf(PROGNAME); + dataTimer.start(); + dataTimer.reset(); +} int main() { float xAcc; - float yAcc; - float vector; + float yAcc; char lcdData[10]; //buffer needs places dor decimal pt and colon -#ifdef PRINTDBUG - pc.printf(PROGNAME); -#endif + initialize_global_vars(); // main loop forever while(true) { - + if(dataTimer.read() > DATAINTERVAL){ + dataTimer.reset(); //Get accelerometer data - tilt angles minus offset for zero mark. - xAcc = abs(acc.getAccX()); - yAcc = abs(acc.getAccY()); - // Calulate vector sum of x and y reading. - vector = sqrt_newt(pow(xAcc,2) + pow(yAcc,2)); - + xAcc = acc.getAccX(); + yAcc = acc.getAccY(); #ifdef PRINTDBUG pc.printf("xAcc = %f\r\n", xAcc); pc.printf("yAcc = %f\r\n", yAcc); - pc.printf("vector = %f\r\n", vector); #endif - sprintf (lcdData,"%4.3f",vector); - LCDMess(lcdData, DATATIME); + sprintf (lcdData,"%3.2f",xAcc); + if (xAcc < 0) sprintf (lcdData,">%3.2f",fabs(xAcc)); + LCDMess(lcdData); // Wait then do the whole thing again. - wait(DATATIME); + } } } \ No newline at end of file