Show angle of orientation modify SLCD for showing degrees

Dependencies:   MMA8451Q SLCD_degrees mbed

Fork of ACC_LCD_341_Basic by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
scohennm
Date:
Tue Dec 13 02:24:37 2016 +0000
Parent:
4:1d559bac561a
Commit message:
Show angle of orientation modify SLCD for showing degrees

Changed in this revision

SLCD.lib Show annotated file Show diff for this revision Revisions of this file
acc_341.cpp Show diff for this revision Revisions of this file
acc_angles.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SLCD.lib	Sun Nov 06 23:56:18 2016 +0000
+++ b/SLCD.lib	Tue Dec 13 02:24:37 2016 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/scohennm/code/SLCD_minus_mod/#ee727b8319f4
+https://developer.mbed.org/users/scohennm/code/SLCD_degrees/#dc3e421470ba
--- a/acc_341.cpp	Sun Nov 06 23:56:18 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,83 +0,0 @@
-#include "mbed.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
- */
-
-#define DATAINTERVAL 0.200
-#define LCDDATALEN 10
-
-#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
-#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;
-
-   
-
-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(){
-    pc.printf(PROGNAME);
-    dataTimer.start();
-    dataTimer.reset(); 
-} 
-
-int main() {
-    float xAcc;
-    float yAcc; 
-    float zAcc; 
-    
-    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(zAcc);
-// Wait then do the whole thing again.
-       }
-    }
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/acc_angles.cpp	Tue Dec 13 02:24:37 2016 +0000
@@ -0,0 +1,114 @@
+#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 PI 3.14159265
+#define PITODEG 180.0
+
+#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 LCDsignedAngle(float theAngle){
+    sprintf (lcdData," %2.0f@",theAngle); 
+    // changed SLCD.cpp to interpret < as -
+    if (theAngle < 0.0) sprintf (lcdData,"<%2.0f@",fabs(theAngle));   
+    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];
+    float angleValue[NUMAXES];
+    int buttonSum = 0;
+    
+    initialize_global_vars();
+// main loop forever 
+    while(true) {
+        while (ButtonTimer > BUTTONTIME){
+            buttonSum = 0;
+            buttonSum = !buttons[0] + !buttons[1];
+            if (buttonSum != 0) {
+                currentAxis = (currentAxis + 1 ) % NUMAXES;
+                LCDMess(axisName[currentAxis]);  
+                wait(LCDWAIT);           
+            }// for loop to look at buttons
+            ButtonTimer.reset();
+        }
+        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
+            if(fabs(axisValue[currentAxis]) > 1.0) axisValue[currentAxis] = 1.0; 
+            angleValue[currentAxis] = asin(axisValue[currentAxis]) * PITODEG / PI;
+            LCDsignedAngle(angleValue[currentAxis]);
+            //LCDsignedFloat(axisValue[currentAxis]);              
+       }
+    }
+}
\ No newline at end of file