Accelerometer ko shake karke led ka color change hotai

Dependencies:   MMA8451Q mbed

Fork of LAB22_AccLedShake by Akashlal Bathe

Files at this revision

API Documentation at this revision

Comitter:
akashlal
Date:
Thu Jul 07 05:01:45 2016 +0000
Parent:
0:652d94a5c5ac
Commit message:
na

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Jul 03 16:24:27 2016 +0000
+++ b/main.cpp	Thu Jul 07 05:01:45 2016 +0000
@@ -3,43 +3,39 @@
 #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() 
+Serial pc(USBTX,USBRX);
+int i=0; //Counter to toggle light color
+int xvali,yvali,zvali; //Variable to get value of Y axis of accelerometer
+
+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
-                    {
+        xvali = (acc.getAccX()+1)*100; //Getting value of X axis acceleration
+        yvali = (acc.getAccY()+1)*100; //Getting absolute value of Y axis acceleration
+        zvali = (acc.getAccZ()+1)*100; //Getting absolute value of Z axis acceleration
+        pc.printf("$%d %d %d;",xvali,yvali,zvali);
+        if(xvali>250||yvali>250||zvali>250) //Initialize shake loop, once execution enters this loop, at least one LED change SHOULD BE visible
+        {            
                         if(i==0) //Turn on only red LED once every three light swithches
                         {
-                            led=0x6;
+                            led=0x06;
                         }
                         else if(i==1) //Turn on only green LED once every three light swithches
                         {
-                            led=0x5;
+                            led=0x05;
                         }
                         else if(i==2) //Turn on only blue LED once every three light swithches
                         {
-                            led=0x3;
+                            led=0x03;
                         }
                         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
+                        }
+                        wait(1);
         }
     } //Infinite loop to keep measuring Y axis acceleration
 }