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.
main.cpp
00001 #include "mbed.h" 00002 #include "MMA8451Q.h" 00003 00004 PinName const SDA = PTE25; 00005 PinName const SCL = PTE24; 00006 00007 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00008 enum state { intermediate, flat, over, down, up, left, right}; 00009 int main(void) 00010 { 00011 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 00012 DigitalOut rled(LED1,1); 00013 DigitalOut gled(LED2,1); 00014 DigitalOut bled(LED3,1); 00015 Serial pc(USBTX, USBRX); // tx, rx 00016 int state; 00017 00018 pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); 00019 state=intermediate; 00020 while (true) { 00021 float x, y, z; 00022 00023 x = acc.getAccX(); 00024 y = acc.getAccY(); 00025 z = acc.getAccZ(); 00026 switch (state){ 00027 case intermediate: 00028 rled=1; 00029 gled=1; 00030 bled=1; 00031 if (z>0.9) {state=flat; pc.printf("Flat");}//Check position and change state 00032 if (z<-0.9) {state=over; pc.printf("Over");} 00033 if (x>0.9) {state=down; pc.printf("Down");} 00034 if (x<-0.9) {state=up; pc.printf("Up");} 00035 if (y>0.9) {state=left; pc.printf("Left");} 00036 if (y<-0.9) {state=right; pc.printf("Right");} 00037 break; 00038 00039 case flat: 00040 rled=0; 00041 if (z<0.8) state=intermediate; 00042 break; 00043 00044 case over: 00045 gled=0; 00046 if (z>-0.8) state=intermediate; 00047 break; 00048 00049 case down: 00050 bled=0; 00051 if (x<0.8) state=intermediate; 00052 break; 00053 00054 case up: 00055 rled=0; 00056 gled=0; 00057 if (x>-0.8) state=intermediate; 00058 break; 00059 00060 case left: 00061 gled=0; 00062 bled=0; 00063 if (y<0.8) state=intermediate; 00064 break; 00065 00066 case right: 00067 rled=0; 00068 bled=0; 00069 if (y>-0.8) state=intermediate; 00070 break; 00071 } 00072 ThisThread::sleep_for(300); // wait(0.3); 00073 pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); 00074 00075 } 00076 }
Generated on Wed Jul 13 2022 08:38:03 by
 1.7.2
 1.7.2