Motion Detector which can detect the moving direction.
Dependencies: C12832 MMA7660 mbed
Fork of app-board-Bubble-Level by
Revision 2:257b84d739a2, committed 2015-02-19
- Comitter:
- Xinliang_Zhao
- Date:
- Thu Feb 19 10:35:58 2015 +0000
- Parent:
- 1:876f52a697c1
- Commit message:
- Motion Detector which can specify the moving direction.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Thu Feb 19 10:35:58 2015 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/chris/code/C12832/#7de323fa46fe
--- a/C12832_lcd.lib Sun Sep 22 17:44:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/dreschpe/code/C12832_lcd/#c9afe58d786a
--- a/main.cpp Sun Sep 22 17:44:42 2013 +0000 +++ b/main.cpp Thu Feb 19 10:35:58 2015 +0000 @@ -2,26 +2,77 @@ //on the application board LCD display #include "mbed.h" #include "MMA7660.h" -#include "C12832_lcd.h" +#include "C12832.h" + +C12832 lcd(p5, p7, p6, p8, p11); //On board LCD display +MMA7660 MMA(p28, p27); //I2C Accelerometer +//Serial pc(USBTX, USBRX); +//DigitalOut connectionLed(LED1);//Accel OK LED +BusOut myleds(LED1, LED2, LED3, LED4); + +int pow(int x) { + int pow = 1; + if(x == 0) { + return pow; + } + for(int i = 0; i < x; i++) { + pow *= 2; + } + return pow; +} -C12832_LCD lcd; //On board LCD display -MMA7660 MMA(p28, p27); //I2C Accelerometer -DigitalOut connectionLed(LED1);//Accel OK LED - +void motion_detect() { + if(MMA.y() < -0.5) { + lcd.cls(); + lcd.locate(55,12); + lcd.printf("UP"); + for(int i = 0; i < 4; i++) { + myleds = pow(i); + wait(0.05); + } + myleds = 0; + } + + if(MMA.y() > 0.5) { + lcd.cls(); + lcd.locate(55,12); + lcd.printf("DOWN"); + for(int i = 3; i >= 0; i--) { + myleds = pow(i); + wait(0.05); + } + myleds = 0; + } + + if(MMA.x() < -0.5) { + lcd.cls(); + lcd.locate(55,12); + lcd.printf("RIGHT"); + myleds = 6; + wait(0.05); + myleds = 9; + wait(0.05); + myleds = 0; + } + + if(MMA.x() > 0.5) { + lcd.cls(); + lcd.locate(55,12); + lcd.printf("LEFT"); + myleds = 9; + wait(0.05); + myleds = 6; + wait(0.05); + myleds = 0; + } +} + int main() { - int x=0,y=0; lcd.cls(); //clear LCD screen if (MMA.testConnection()) - connectionLed = 1; //Accelerometer init OK + myleds = 15; //Accelerometer init OK while(1) { - //read X,Y +/-Gs and scale for #display pixels - x = (x + MMA.x() * 32.0)/2.0; - y = (y -(MMA.y() * 16.0))/2.0; - lcd.fillcircle(x+63, y+15, 3, 1); //draw bubble - lcd.circle(63, 15, 8, 1); - wait(.1); //time delay - lcd.fillcircle(x+63, y+15, 3, 0); //erase bubble + motion_detect(); } - }