mbed_adxl335_oled_NXP824

Dependencies:   Adafruit_GFX mbed

Revision:
1:427a48756218
Parent:
0:cf82471d4484
Child:
2:2c53f4db30f9
--- a/main.cpp	Sun Nov 06 06:50:33 2016 +0000
+++ b/main.cpp	Fri Nov 25 05:50:32 2016 +0000
@@ -19,6 +19,7 @@
     };
 };
 
+
 SPIPreInit mySpi(DI,NC,DO);
 Adafruit_SSD1306_Spi oled(mySpi,DC,RST,CS,64,128);
 
@@ -30,20 +31,41 @@
 DigitalIn myinput2(P0_14);
 DigitalIn myinput3(P0_22);
 
+InterruptIn my_button(P0_1);
+DigitalIn my_buttoninput(P0_1);
+
 Serial uart(P0_4, P0_0);
 
 AnalogIn myadc1(A0);
 AnalogIn myadc2(A1);
 AnalogIn myadc3(A3);
 
+int mode=0;
+
+void pressed()
+{
+    wait_ms(20);
+    if(my_buttoninput==0) {
+        if(mode==0)
+            mode=1;
+        else mode =0;
+    }
+
+
+}
+
 int main()
 {
 
     double x, y, z, result;
+
+
     myinput1.mode(PullNone);
     myinput2.mode(PullNone);
     myinput3.mode(PullNone);
 
+    my_button.fall(&pressed);
+
     oled.clearDisplay();
     oled.setTextSize(2);
     oled.setTextCursor(0,0);
@@ -53,23 +75,39 @@
         uart.printf("X: %2.3f V\r\n" ,myadc1.read()*3.3);
         uart.printf("Y: %2.3f V\r\n" ,myadc2.read()*3.3);
         uart.printf("Z: %2.3f V\r\n" ,myadc3.read()*3.3);
-
-        z= (myadc2.read()*3.3-1.625)*3025.72;
         x = (myadc1.read()*3.3-1.646)*3048.78;
-        y = (myadc3.read()*3.3-1.672)*3067.48;
+        y = (myadc2.read()*3.3-1.642)*3025.72;
+        z = (myadc3.read()*3.3-1.672)*3067.48;
+        if(mode==0) {
+            result = atan2(x,sqrt(z*z+y*y))*180/PI;
+            uart.printf("Alpha: %2.3f degrees\r\n" ,result);
+            oled.printf ("x %.1f d\r\n",result);
+            result = atan2(y,sqrt(z*z+x*x))*180/PI;
+            uart.printf("Beta: %2.3f degrees\r\n" ,result);
+            oled.printf ("y %.1f d\r\n",result);
+            result = atan2(z,sqrt(x*x+y*y))*180/PI;
+            uart.printf("Gamma: %2.3f degrees\r\n" ,result);
+            oled.printf ("z %.1f d\r\n",result);
+            oled.printf ("  Mode A\r\n");
 
-        result = atan2 (x,y) * 180 / PI;
-        uart.printf ("The arc tangent for (x=%f, y=%f) is %.4f degrees\r\n", x, y, result );
-        oled.printf (" %.2f D\r\n",result);
-        oled.printf ("Y: %.0f mg\r\n",z);
-
+        } else {
+            result = atan2 (x,z) * 180 / PI;
+            uart.printf ("The arc tangent for (x=%f, z=%f) is %.4f degrees \r\n", x, z, result );
+            oled.printf ("X %.1f D\r\n",result);
+            oled.printf ("Y: %.0f mg\r\n",y);
+//            result = atan2 (y,z) * 180 / PI;
+//            uart.printf ("The arc tangent for (y=%f, z=%f) is %.4f degrees\r\n", y, z, result );
+//            oled.printf ("Y %.1f D\r\n",result);
+//            oled.printf ("Y: %.0f mg\r\n",y);
+            oled.printf ("  Mode B\r\n");
+        }
         myled1 = 1;
         myled2 = 0;
         myled3 = 1;
 
         oled.display();
 
-        wait(1);
+        wait(1.5);
         oled.setTextCursor(0,0);
         oled.clearDisplay();
     }