ok

Dependencies:   MMA8451Q SLCD mbed

Fork of ACC_LCD_341_all_axies by Stanley Cohen

Revision:
5:9e4173e23e05
Parent:
4:1d559bac561a
Child:
6:88a2bae825ae
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/acc_all_axies.cpp	Mon Nov 28 15:43:04 2016 +0000
@@ -0,0 +1,105 @@
+#include "mbed.h"
+#include <math.h> 
+#include "MMA8451Q.h"
+#include "SLCD.h"
+
+#define NUMAXES 3
+#define XAXIS 0
+#define YAXIS 1
+#define ZAXIS 2
+#define NUMBUTS 2
+#define LBUT PTC12  // port addresses for buttons
+#define RBUT PTC3
+#define BUTTONTIME 0.200
+#define DATAINTERVAL 0.200
+#define LCDWAIT  1.5
+#define LCDDATALEN 10
+
+#define PROGNAME "ACC_LCD_all_axes_v1\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
+#endif
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+SLCD slcd; //define LCD display
+char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon
+int currentAxis = XAXIS; // xaxis
+
+MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+Serial pc(USBTX, USBRX);
+Timer dataTimer;
+Timer ButtonTimer; // for reading button states
+DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; // set up buttons
+
+char axisName[NUMAXES][LCDDATALEN] = {"<X<","<Y<", "<Z<"};   
+
+void LCDMess(char *lMess){
+        slcd.Home();
+        slcd.clear();
+        slcd.printf(lMess);
+} 
+
+void LCDsignedFloat(float theNumber){
+    sprintf (lcdData," %3.2f",theNumber); 
+    // changed SLCD.cpp to interpret < as -
+    if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber));   
+    LCDMess(lcdData); 
+} 
+
+void initialize_global_vars(){
+    pc.printf(PROGNAME);
+    // set up DAQ timers
+    ButtonTimer.start();
+    ButtonTimer.reset();
+    dataTimer.start();
+    dataTimer.reset(); 
+    LCDMess(axisName[currentAxis]);  
+    wait(LCDWAIT);  
+    
+} 
+
+int main() {
+    int i; // loop index
+    float axisValue[NUMAXES]; // set up an array of axid values
+    int buttonSum = 0;
+    
+    initialize_global_vars();
+// main loop forever 
+    while(true) {
+        while (ButtonTimer > BUTTONTIME){ //Use a while instead of an if 
+            buttonSum = 0; // Note
+            buttonSum = !buttons[0] + !buttons[1];
+            if (buttonSum != 0) { // 
+                currentAxis = (currentAxis + 1 ) % NUMAXES; // Cycle through the 
+                                                        // Axis with modulus
+                                                        // to reset back to zero
+                LCDMess(axisName[currentAxis]);  
+                wait(LCDWAIT);           
+            }// for loop to look at buttons
+            ButtonTimer.reset(); // Make sure you reset the timer or..
+                                // Groundhog Day.
+        }
+        while (dataTimer.read() > DATAINTERVAL){
+            dataTimer.reset();             
+            axisValue[XAXIS]= acc.getAccX();
+            axisValue[YAXIS] = acc.getAccY(); 
+            axisValue[ZAXIS] = acc.getAccZ();     
+#ifdef PRINTDBUG
+            for (i=0; i< NUMAXES;i++){
+                pc.printf("Acc %d = %f\r\n",i, axisValue[i]);
+            }
+#endif
+            LCDsignedFloat(axisValue[currentAxis]);              
+       }
+    }
+}
\ No newline at end of file