3 axis accel
Dependencies: MMA8451Q SLCD_minus_mod mbed
Fork of ACC_LCD_341_Basic by
Revision 6:0f05e120b749, committed 2016-11-21
- Comitter:
- eseifert
- Date:
- Mon Nov 21 04:43:21 2016 +0000
- Parent:
- 5:29c6ba524263
- Commit message:
- ssd541-hw14.1
Changed in this revision
acc_341.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/acc_341.cpp Mon Nov 14 15:50:33 2016 +0000 +++ b/acc_341.cpp Mon Nov 21 04:43:21 2016 +0000 @@ -1,9 +1,9 @@ #include "mbed.h" -#include <math.h> +#include <math.h> #include "MMA8451Q.h" #include "SLCD.h" -/* +/* Test of the accelerometer, digital I/O, on-board LCD screen. Looing at vector product of the x-y components of the accelerometer. Works pretty well. Still rough, program wise - sc 140710 @@ -11,73 +11,100 @@ #define DATAINTERVAL 0.200 #define LCDDATALEN 10 - +// buttons +#define NUMBUTS 1 +#define LBUT PTC12 // port addresses for buttons +//#define RBUT PTC3 +#define BUTTONTIME 0.2 +// lcd title +#define LCDTITLE "ACCL" +#define TITLEWAIT .8 +// serial title #define PROGNAME "ACCLCD341-541\r\n" #define PRINTDBUG -// -#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) - PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. - PinName const SCL = PTE24; // DO NOT CHANGE -#elif defined (TARGET_KL05Z) - PinName const SDA = PTB4; - PinName const SCL = PTB3; -#else - #error TARGET NOT DEFINED +#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) +PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. +PinName const SCL = PTE24; // DO NOT CHANGE #endif - #define MMA8451_I2C_ADDRESS (0x1d<<1) - SLCD slcd; //define LCD display char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); Serial pc(USBTX, USBRX); -Timer dataTimer; +Timer DataTimer; - +Timer ButtonTimer; // for reading button states +DigitalIn buttons[NUMBUTS] = {LBUT}; -void LCDMess(char *lMess){ - slcd.Home(); - slcd.clear(); - slcd.printf(lMess); -} +void LCDMess(char *lMess) +{ + slcd.Home(); + slcd.clear(); + slcd.printf(lMess); +} -void LCDsignedFloat(float theNumber){ - sprintf (lcdData," %3.2f",theNumber); - if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber)); - LCDMess(lcdData); -} +void initialize_global_vars() +{ + // print lcd title + pc.printf(PROGNAME); + LCDMess(LCDTITLE); + wait(TITLEWAIT); + // set up DAQ timers + DataTimer.start(); + DataTimer.reset(); + // set up button timers + ButtonTimer.start(); + ButtonTimer.reset(); +} -void initialize_global_vars(){ - pc.printf(PROGNAME); - dataTimer.start(); - dataTimer.reset(); -} +void LCDsignedFloat(float theNumber) +{ + sprintf (lcdData," %3.2f",theNumber); + if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber)); + LCDMess(lcdData); +} -int main() { - float xAcc; - float yAcc; - float zAcc; - +int main() +{ + int displayState = 0; + char axis = ' '; + float accl[3] = {0,0,0}; + 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 = acc.getAccX(); - yAcc = acc.getAccY(); - zAcc = acc.getAccZ(); - -#ifdef PRINTDBUG - pc.printf("xAcc = %f\r\n", xAcc); - pc.printf("yAcc = %f\r\n", yAcc); - pc.printf("zAcc = %f\r\n", zAcc); -#endif - - LCDsignedFloat(xAcc); -// Wait then do the whole thing again. - } + if(DataTimer.read() > DATAINTERVAL) { + DataTimer.reset(); + accl[0] = acc.getAccX(); + accl[1] = acc.getAccY(); + accl[2] = acc.getAccZ(); + LCDsignedFloat(accl[displayState]); + } + if (ButtonTimer > BUTTONTIME) { + if(!buttons[0]) { + // a button is pressed + switch(displayState) { + case 0: + axis = 'x'; + LCDMess(&axis); + wait(.5); + break; + case 1: + axis = 'y'; + LCDMess(&axis); + wait(.5); + break; + case 2: + axis = 'z'; + LCDMess(&axis); + wait(.5); + break; + } + displayState++; + displayState = displayState%3; + ButtonTimer.reset(); + } + } } } \ No newline at end of file