Lab4 code

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
toh2018
Date:
Thu Mar 01 13:35:25 2018 +0000
Parent:
2:840d7ce4075f
Commit message:
add part two code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 840d7ce4075f -r 38630c6ae590 main.cpp
--- a/main.cpp	Fri Feb 16 13:53:36 2018 +0000
+++ b/main.cpp	Thu Mar 01 13:35:25 2018 +0000
@@ -85,3 +85,107 @@
         pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f \n \r", x, y, z);
     }
 }
+
+/*****************************************
+Second part code please uncomment and turn above codes to comment to run 
+#include "mbed.h"
+#include "rtos.h"
+#include "MMA8451Q.h"
+ 
+  PinName const SDA = PTE25;
+  PinName const SCL = PTE24;
+  
+  
+  
+  Timer timer;
+ 
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+ 
+int main(void)
+{
+    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+    PwmOut rled(LED1);
+    PwmOut gled(LED2);
+    PwmOut bled(LED3);
+
+    Serial pc(USBTX, USBRX); // tx, rx
+ 
+    pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
+ 
+    while (true) {
+        float x, y, z;
+         
+        x = acc.getAccX();
+        y = acc.getAccY();
+        z = acc.getAccZ();
+        
+        // set the time
+        
+        timer.start();
+        //timer.reset();
+        
+        
+        
+        //pc.printf("time is: %f \n \r",timer.read());
+        Thread::wait(300);
+        
+       //check the time 
+       
+       
+      
+            
+        // this is flat state
+        if(timer.read() <=10 && z>= 0.9 ){  
+           
+           gled= 0;
+           rled= 1;
+           bled= 1;
+           
+           pc.printf("flat \n \r");
+           
+            pc.printf("time is: %f \n \r",timer.read());
+           
+          
+                     
+        }else if(timer.read()<=6 && y<= -0.90 ){ 
+            
+           gled= 0;
+           rled= 1;
+           bled= 1;
+            
+            pc.printf("Right \n \r");
+            pc.printf("time is: %f \n \r",timer.read());
+          
+            
+            //this is the Up
+        }else if(timer.read()<=6 && x<= -0.90 ){
+            
+            gled= 0;
+           rled= 1;
+           bled= 1;
+          
+            pc.printf("up \n \r");
+            pc.printf("time is: %f \n \r",timer.read());
+            
+           
+        }
+        
+        else{
+            
+            pc.printf("Error message \n \r");
+            rled = 0;
+            timer.reset();
+            gled= 1;
+            rled= 0;
+            bled= 1;
+            
+        }
+            
+       
+        // this is to turn the light off during the turn event   
+    
+}
+}
+
+
+*******************************************/