a simple code with a not-so-simple mbed freeze

Dependencies:   MPU6050 mbed-rtos mbed

Revision:
2:141571165c57
Parent:
1:d2011078309d
diff -r d2011078309d -r 141571165c57 main.cpp
--- a/main.cpp	Mon Feb 18 11:02:59 2013 +0000
+++ b/main.cpp	Mon Feb 18 13:32:59 2013 +0000
@@ -5,87 +5,51 @@
 DigitalOut myled(LED1);
 Serial pc1(USBTX, USBRX);
 MPU6050 mpu(0x69);
-
-int moyZ=0;
-int16_t moy[64];
+int16_t ax, ay, az;
+int16_t gx, gy, gz;
+int moyZ=0;                 //global Z value (mean)
+int16_t moy[64];            //array of different measurements
 
 
-void moyennage_Z()
+void moyennage_Z()              //calculates the mean value by going through the whole array, sum and divide by the sample size (64)
 {
     for (int n=0; n<64; n++) {
         moyZ=moyZ+moy[n];
-
-
     }
     moyZ=(int)moyZ/64;
 }
 
-
-void mon_thr(void const *args)
+void mon_thr(void const *args)      //blinking thread to test mbed's state
 {
     while (true) {
         myled=!myled;
+        wait(0.5);
     }
 }
 
-
 int main()
 
 {
-
-    Thread thread(mon_thr); 
-    pc1.printf("MPU6050 test\n\n\r");
-    pc1.printf("MPU6050 initialize \n\r");
-
+    Thread thread(mon_thr);
+    pc1.printf("MPU6050 test\n\n\r");               //procedure to test the connection to the mpu6050, if valid
+    pc1.printf("MPU6050 initialize \n\r");          //the code to execute is embedded in a if{}
     mpu.initialize();
     pc1.printf("MPU6050 testConnection \n\r");
-
-    int16_t ax, ay, az;
-    int16_t gx, gy, gz;
-    
-
-int comptFin=0;
-int comptDeb=0;
-
-
     bool mpu6050TestResult = mpu.testConnection();
     if(mpu6050TestResult) {
         pc1.printf("MPU6050 test passed \n\r");
 
-        while(comptFin<64) {
-            mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
-            moy[comptFin]=az;
-            comptFin++;
-
-        }
-        moyennage_Z();
-        while(1) {
-            while(comptFin<64) {
+        while(1) {                                             
+            for (int n=0; n<64; n++) {                          //refreshing the 64-int16 array
                 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
-                moy[comptFin]=az;
-                comptFin++;
+                moy[n]=az;
                 wait(0.01);
             }
-
-
-            comptFin%=64;
-            moyZ-=(int)moy[comptDeb]/64;
-            comptDeb++;
-            comptDeb%=64;
-            moyZ+=(int)moy[comptFin]/64;
-            //moyennage_Z();
+            moyennage_Z();                                      //calculating the mean value by a simple sum and divide
             printf("%i\n\r",moyZ+17000);
-            wait(0.01);
-
-
-
-
+            wait(0.1);
         }
-
-
     } else {
         pc1.printf("MPU6050 test failed \n\r");
     }
-
-
 }