Julie Newcomb / Mbed 2 deprecated mbed_accelerometer

Dependencies:   LSM303DLHC-a PinDetect USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers acchello.cpp Source File

acchello.cpp

00001 #include "mbed.h"
00002 #include "vector.h"
00003 #include "LSM303DLHC.h"
00004 #include "USBKeyboard.h"
00005  
00006 Serial pc(USBTX, USBRX); // tx, rx
00007 USBKeyboard keyboard;
00008  // top face
00009 LSM303DLHC lsm(PTE0,PTE1);
00010 AnalogIn pU1(A0); // up
00011 AnalogIn pD1(A1); //down
00012 AnalogIn pR1(A2); //right
00013 AnalogIn pL1(A3); //left
00014 
00015 // bottom face
00016 AnalogIn pU(PTE20);     //up
00017 AnalogIn pD(PTE21);     //down
00018 AnalogIn pR(PTE22);   //right
00019 AnalogIn pL(PTE23);   //left
00020 
00021 //switches
00022 DigitalIn s1(D6);
00023 DigitalIn s2(D7);
00024 
00025 // Integers
00026 int counter = 0;
00027 
00028 // Levels for photocells
00029 float covered = 0.5; // maximum value to be considered covered
00030 float opened = 0.8; // minimum value to be considered open (ambient)
00031 
00032 // boolean variables for different wait states
00033 bool waitingUP = false;
00034 bool waitingDOWN = false;
00035 bool waitingRIGHT = false;
00036 bool waitingLEFT = false;
00037 
00038 class Orientation {
00039     vector a, m;
00040     bool rightSideUp, movingDown, movingUp, movedDown, movedUp;
00041     float top_threshold, bottom_threshold;
00042     public: Orientation() {
00043         top_threshold = -0.3;
00044         bottom_threshold = 0.3;
00045         rightSideUp = true;
00046         movingDown = false;
00047         movingUp = false;
00048         movedDown = false;
00049         movedUp = true;
00050     }
00051     bool isRightSideUp() {
00052         
00053         bool check = lsm.read(&a.x,&a.y,&a.z,&m.x,&m.y,&m.z);
00054         //pc.printf("x %f y %f z %f",a.z,a.y,a.z);
00055         pc.printf("{ \"x\": %f, \"y\": %f, \"z\": %f}\r\n",a.x,a.y,a.z);
00056         if (check) {
00057             if (a.x > top_threshold) {
00058                 movingDown = true;
00059                 movingUp = false;
00060             } else if (a.x < bottom_threshold) {
00061                 movingUp = true;
00062                 movingDown = false;
00063             }
00064             if (movingDown && a.x > bottom_threshold) {
00065                 movingDown = false;
00066                 movedUp = false;
00067                 movedDown = true;
00068             } else if (movingUp && a.x < top_threshold) {
00069                 movingUp = false;
00070                 movedDown = false;
00071                 movedUp = true;
00072             }
00073             if (!rightSideUp && movedUp) {
00074                 rightSideUp = true;
00075             } else if (rightSideUp && movedDown) {
00076                 rightSideUp = false;
00077             }
00078         }
00079         return rightSideUp;
00080     }
00081 };
00082 
00083 bool isCovered(AnalogIn p) {
00084     return p < covered;
00085 }
00086 
00087 bool isOpen(AnalogIn p) {
00088     return p > opened;
00089 }
00090 
00091 bool photoCellUp(AnalogIn pUp,AnalogIn pDown,AnalogIn pRight,AnalogIn pLeft) {
00092    // pc.printf("covered only up\r\n");
00093     return isCovered(pUp) && isOpen(pDown) && isOpen(pRight) && isOpen(pLeft);
00094 }
00095 
00096 bool photoCellDown(AnalogIn pUp,AnalogIn pDown,AnalogIn pRight,AnalogIn pLeft) {
00097     //pc.printf("covered only down\r\n");
00098     return isOpen(pUp) && isCovered(pDown) && isOpen(pRight) && isOpen(pLeft);
00099 }
00100 
00101 bool photoCellLeft(AnalogIn pUp,AnalogIn pDown,AnalogIn pRight,AnalogIn pLeft) {
00102     //pc.printf("covered only right\r\n");
00103     return isOpen(pUp) && isOpen(pDown) && isOpen(pRight) && isCovered(pLeft);
00104 }
00105 
00106 bool photoCellRight(AnalogIn pUp,AnalogIn pDown,AnalogIn pRight,AnalogIn pLeft) {
00107     //pc.printf("covered only left\r\n");
00108     return isOpen(pUp) && isOpen(pDown) && isCovered(pRight) && isOpen(pLeft);
00109 }
00110 
00111 
00112 int main() {
00113     pc.printf("hello\r\n");
00114     Orientation up;
00115     bool lastRightSideUp = true;
00116 
00117     while (1) {
00118             bool rightSideUp = up.isRightSideUp();
00119             if (rightSideUp != lastRightSideUp) {
00120                pc.printf("flipped\r\n");
00121                //keyboard.printf("f\r");
00122             }
00123             if (rightSideUp) {
00124                 pc.printf("RIGHT SIDE UP\r\n");
00125             } else {
00126                 pc.printf("UP SIDE DOWN\r\n");
00127             }
00128             lastRightSideUp = rightSideUp;
00129                 // LISTENING FOR UP & DOWN SWIPES
00130     if ((rightSideUp && photoCellUp(pU1,pD1,pR1,pL1)) || (!rightSideUp && photoCellUp(pU,pD,pR,pL)))
00131     {   
00132         if (waitingUP)
00133         {
00134             pc.printf("Swiped UP \r\n");
00135             //keyboard.printf("w\r");
00136             counter = 0;
00137             waitingUP = false;
00138             wait(0.2);
00139         }
00140             else
00141             {
00142                 waitingDOWN = true;
00143                // pc.printf("Waiting to Swipe Down \r\n");
00144                // keyboard.printf("Waiting to Swipe Down\r");
00145             }
00146         }
00147 
00148         else if ((rightSideUp && photoCellDown(pU1,pD1,pR1,pL1)) || (!rightSideUp && photoCellDown(pU,pD,pR,pL)))
00149         {
00150             if (waitingDOWN)
00151             {
00152                 pc.printf("Swiped DOWN \r\n");
00153                 //keyboard.printf("s\r");
00154                 counter = 0;
00155                 waitingDOWN = false;
00156                 wait(0.2);
00157             }
00158                 else
00159                 {
00160                     waitingUP = true;
00161                     //pc.printf("Waiting to Swipe Up \r\n");
00162                     //keyboard.printf("Waiting to Swipe Up\r");
00163                 }
00164         }
00165         else if ((rightSideUp && photoCellRight(pU1,pD1,pR1,pL1)) || (!rightSideUp && photoCellRight(pU,pD,pR,pL))) {
00166             if (waitingRIGHT)
00167             {
00168             pc.printf("Swiped RIGHT \r\n");
00169             //keyboard.printf("e\r");
00170             counter = 0;
00171             waitingRIGHT = false;
00172             wait(0.2);
00173             }
00174             else
00175             {
00176                 waitingLEFT = true;
00177                 //pc.printf("Waiting to Swipe LEFT \r\n");
00178                 //keyboard.printf("Waiting to Swipe Left\r");
00179             }
00180         } else if ((rightSideUp && photoCellLeft(pU1,pD1,pR1,pL1)) || (!rightSideUp && photoCellLeft(pU,pD,pR,pL))) {
00181             if (waitingLEFT)
00182             {
00183                 pc.printf("Swiped LEFT \r\n");
00184                 //keyboard.printf("w\r");
00185                 counter = 0;
00186                 waitingLEFT = false;
00187                 wait(0.2);
00188             }
00189             else
00190             {
00191                 waitingRIGHT = true;
00192                 //pc.printf("Waiting to Swipe RIGHT \r\n");
00193                 //keyboard.printf("Waiting to Swipe Right\r");
00194             }
00195         } else if ((rightSideUp && s1 == 0) || (!rightSideUp && s2 == 0)) {
00196             pc.printf("Pressed button!\r\n");
00197             // keyboard.printf("m\r");
00198         }
00199 
00200     if (counter > 40)
00201     {
00202         waitingUP = false;
00203         waitingDOWN = false;
00204         waitingRIGHT = false;
00205         waitingDOWN = false;
00206         counter = 0;
00207         //printf("Counter Reset \r\n");
00208     }
00209         wait(0.1); 
00210     }
00211     
00212     
00213 }