Accelerometer ko shake karke led ka color change hotai

Dependencies:   MMA8451Q mbed

Fork of LAB22_AccLedShake by Akashlal Bathe

Committer:
akashlal
Date:
Thu Jul 07 05:01:45 2016 +0000
Revision:
1:0c71a0a434be
Parent:
0:652d94a5c5ac
na

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akashlal 0:652d94a5c5ac 1 #include "mbed.h" //Importing mbed header files
akashlal 0:652d94a5c5ac 2 #include "MMA8451Q.h" //Importing libraries to get accelerometer values
akashlal 0:652d94a5c5ac 3 #define MMA8451_I2C_ADDRESS (0x1d<<1) //Setting Accelerometer address
akashlal 0:652d94a5c5ac 4 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //Initializing accelerometer pins and address
akashlal 0:652d94a5c5ac 5 BusOut led(PTB18,PTB19,PTD1); //BusOut to control ON/OFF state of 3 LEDs
akashlal 1:0c71a0a434be 6 Serial pc(USBTX,USBRX);
akashlal 1:0c71a0a434be 7 int i=0; //Counter to toggle light color
akashlal 1:0c71a0a434be 8 int xvali,yvali,zvali; //Variable to get value of Y axis of accelerometer
akashlal 1:0c71a0a434be 9
akashlal 1:0c71a0a434be 10 int main()
akashlal 0:652d94a5c5ac 11 {
akashlal 0:652d94a5c5ac 12 led=0x7; //Ensure all LEDs are off initially
akashlal 0:652d94a5c5ac 13 while(1)
akashlal 0:652d94a5c5ac 14 {
akashlal 1:0c71a0a434be 15 xvali = (acc.getAccX()+1)*100; //Getting value of X axis acceleration
akashlal 1:0c71a0a434be 16 yvali = (acc.getAccY()+1)*100; //Getting absolute value of Y axis acceleration
akashlal 1:0c71a0a434be 17 zvali = (acc.getAccZ()+1)*100; //Getting absolute value of Z axis acceleration
akashlal 1:0c71a0a434be 18 pc.printf("$%d %d %d;",xvali,yvali,zvali);
akashlal 1:0c71a0a434be 19 if(xvali>250||yvali>250||zvali>250) //Initialize shake loop, once execution enters this loop, at least one LED change SHOULD BE visible
akashlal 1:0c71a0a434be 20 {
akashlal 0:652d94a5c5ac 21 if(i==0) //Turn on only red LED once every three light swithches
akashlal 0:652d94a5c5ac 22 {
akashlal 1:0c71a0a434be 23 led=0x06;
akashlal 0:652d94a5c5ac 24 }
akashlal 0:652d94a5c5ac 25 else if(i==1) //Turn on only green LED once every three light swithches
akashlal 0:652d94a5c5ac 26 {
akashlal 1:0c71a0a434be 27 led=0x05;
akashlal 0:652d94a5c5ac 28 }
akashlal 0:652d94a5c5ac 29 else if(i==2) //Turn on only blue LED once every three light swithches
akashlal 0:652d94a5c5ac 30 {
akashlal 1:0c71a0a434be 31 led=0x03;
akashlal 0:652d94a5c5ac 32 }
akashlal 0:652d94a5c5ac 33 i++; //Increment counter
akashlal 0:652d94a5c5ac 34 if(i==3) //Ensure counter always increments between 0 to 2
akashlal 0:652d94a5c5ac 35 {
akashlal 0:652d94a5c5ac 36 i=0;
akashlal 1:0c71a0a434be 37 }
akashlal 1:0c71a0a434be 38 wait(1);
akashlal 0:652d94a5c5ac 39 }
akashlal 0:652d94a5c5ac 40 } //Infinite loop to keep measuring Y axis acceleration
akashlal 0:652d94a5c5ac 41 }