Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MPU6050 mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MPU6050.h" 00003 #include "rtos.h" 00004 00005 DigitalOut myled(LED1); 00006 Serial pc1(USBTX, USBRX); 00007 MPU6050 mpu(0x69); 00008 int16_t ax, ay, az; 00009 int16_t gx, gy, gz; 00010 int moyZ=0; //global Z value (mean) 00011 int16_t moy[64]; //array of different measurements 00012 00013 00014 void moyennage_Z() //calculates the mean value by going through the whole array, sum and divide by the sample size (64) 00015 { 00016 for (int n=0; n<64; n++) { 00017 moyZ=moyZ+moy[n]; 00018 } 00019 moyZ=(int)moyZ/64; 00020 } 00021 00022 void mon_thr(void const *args) //blinking thread to test mbed's state 00023 { 00024 while (true) { 00025 myled=!myled; 00026 wait(0.5); 00027 } 00028 } 00029 00030 int main() 00031 00032 { 00033 Thread thread(mon_thr); 00034 pc1.printf("MPU6050 test\n\n\r"); //procedure to test the connection to the mpu6050, if valid 00035 pc1.printf("MPU6050 initialize \n\r"); //the code to execute is embedded in a if{} 00036 mpu.initialize(); 00037 pc1.printf("MPU6050 testConnection \n\r"); 00038 bool mpu6050TestResult = mpu.testConnection(); 00039 if(mpu6050TestResult) { 00040 pc1.printf("MPU6050 test passed \n\r"); 00041 00042 while(1) { 00043 for (int n=0; n<64; n++) { //refreshing the 64-int16 array 00044 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 00045 moy[n]=az; 00046 wait(0.01); 00047 } 00048 moyennage_Z(); //calculating the mean value by a simple sum and divide 00049 printf("%i\n\r",moyZ+17000); 00050 wait(0.1); 00051 } 00052 } else { 00053 pc1.printf("MPU6050 test failed \n\r"); 00054 } 00055 }
Generated on Fri Jul 15 2022 23:39:25 by
1.7.2