Snake Game using accelerometer with many fun features.

Dependencies:   C12832 MMA7660 mbed

Fork of app-board-Bubble-Level by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motion_detect.cpp Source File

motion_detect.cpp

00001 //Uses x & y acceleration to simulate a bubble level
00002 //on the application board LCD display
00003 #include "mbed.h"
00004 #include "MMA7660.h"
00005 //#include "C12832_lcd.h"
00006 #include "C12832.h"
00007 #include "motion_detect.h"
00008 
00009 #define LEFT 0
00010 #define RIGHT 1
00011 #define UP 2
00012 #define DOWN 3
00013 
00014 //static C12832_LCD lcd; //On board LCD display
00015 static C12832 lcd(p5, p7, p6, p8, p11);
00016 MMA7660 MMA(p28, p27); //I2C Accelerometer
00017 //Serial pc(USBTX, USBRX);
00018 //DigitalOut connectionLed(LED1);//Accel OK LED
00019 BusOut myleds(LED1, LED2, LED3, LED4);
00020 
00021 int pow(int x) {
00022     int pow = 1;
00023     if(x == 0) {
00024         return pow;
00025     }
00026     for(int i = 0; i < x; i++) {
00027         pow *= 2;
00028     }
00029     return pow;
00030 }
00031 
00032 void motion_detect(int &direction) {
00033         if(MMA.y() < -0.5) {
00034             if(direction != DOWN) {
00035                 direction = UP;
00036             }
00037             for(int i = 0; i < 4; i++) {
00038                 myleds = pow(i);
00039                 wait(0.05);
00040             }
00041             myleds = 0;
00042         }
00043         
00044         if(MMA.y() > 0.5) {
00045             if(direction != UP) {
00046                 direction = DOWN;
00047             }
00048             for(int i = 3; i >= 0; i--) {
00049                 myleds = pow(i);
00050                 wait(0.05);
00051             }
00052             myleds = 0;
00053         }
00054         
00055         if(MMA.x() < -0.5) {
00056             if(direction != LEFT) {
00057                 direction = RIGHT;
00058             }
00059             myleds = 6;
00060             wait(0.05);
00061             myleds = 9;
00062             wait(0.05);
00063             myleds = 0;
00064         }
00065         
00066         if(MMA.x() > 0.5) {
00067             if(direction != RIGHT) {
00068                 direction = LEFT;
00069             }
00070             myleds = 9;
00071             wait(0.05);
00072             myleds = 6;
00073             wait(0.05);
00074             myleds = 0;
00075         }
00076 }
00077         
00078 void validity() {
00079     lcd.cls(); //clear LCD screen
00080     if (MMA.testConnection())
00081         myleds = 15; //Accelerometer init OK
00082 }