Accelerometer ko shake karke led ka color change hotai
Fork of LAB22_AccLedShake by
Diff: main.cpp
- Revision:
- 0:652d94a5c5ac
- Child:
- 1:0c71a0a434be
diff -r 000000000000 -r 652d94a5c5ac main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jul 03 16:24:27 2016 +0000 @@ -0,0 +1,45 @@ +#include "mbed.h" //Importing mbed header files +#include "MMA8451Q.h" //Importing libraries to get accelerometer values +#define MMA8451_I2C_ADDRESS (0x1d<<1) //Setting Accelerometer address +MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //Initializing accelerometer pins and address +BusOut led(PTB18,PTB19,PTD1); //BusOut to control ON/OFF state of 3 LEDs +int main() +{ + led=0x7; //Ensure all LEDs are off initially + int i=0; //Counter to toggle light color + int yvali; //Variable to get value of Y axis of accelerometer + while(1) + { + yvali = abs(acc.getAccY()*100); //Getting absolute value of Y axis acceleration + if(yvali>30) //Initialize shake loop, once execution enters this loop, at least one LED change SHOULD BE visible + { + while(yvali>30) //Ensure that light change is not triggered during the duration of shake + { + if(yvali<30) //Checking if shaking has stopped + { + wait(50); //delay to sample after 50ms + if(yvali<30) //Ensuring if shaking has stopped + { + if(i==0) //Turn on only red LED once every three light swithches + { + led=0x6; + } + else if(i==1) //Turn on only green LED once every three light swithches + { + led=0x5; + } + else if(i==2) //Turn on only blue LED once every three light swithches + { + led=0x3; + } + i++; //Increment counter + if(i==3) //Ensure counter always increments between 0 to 2 + { + i=0; + } + } + } //Exit when LED color is changed one time + } //End of light change loop after shaking stops + } + } //Infinite loop to keep measuring Y axis acceleration +}