IDD Fall 2015 / Mbed 2 deprecated HW3

Dependencies:   DebounceIn MMA8451Q USBDevice mbed

Fork of HW3 by Joseph Marquis

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //#include "PinDetect.h"
00003 #include "MMA8451Q.h"
00004 #include "USBKeyboard.h"
00005 
00006 USBKeyboard key;
00007 
00008 // define I2C Pins and address for KL25Z. Taken from default sample code.
00009 PinName const SDA = PTE25;
00010 PinName const SCL = PTE24;
00011 AnalogIn j(A0);
00012 AnalogIn n(A5);
00013 AnalogIn k(A2);
00014 //PinDetect P0(D2);
00015 
00016 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00017 
00018 //serial connection to PC via USB
00019 //Serial pc(USBTX, USBRX);
00020 
00021 int main(void)
00022 {
00023     //configure on-board I2C accelerometer on KL25Z
00024     MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 
00025     //map read acceleration to PWM output on red status LED
00026         float p;
00027        float q;
00028       float r;
00029       
00030       state = 1;
00031     while (true) {
00032         float x, y, z;
00033             x = acc.getAccX();
00034             y = acc.getAccY();
00035             wait(0.05f);
00036             p= j.read(); 
00037              q= n.read();
00038              r= k.read();
00039         
00040 if (p>.2) {
00041     if (q<.2){
00042          if(r<.2){
00043              key.keyCode('1',KEY_CTRL);
00044                  }
00045              }
00046     if (q>.2){
00047          if (r<.2) {
00048             key.keyCode('2',KEY_CTRL);
00049                     }
00050          if (r>.2) {
00051             key.keyCode('3',KEY_CTRL);
00052                    }
00053              }
00054           }    
00055             
00056              
00057  
00058             
00059             while (y<-0.2){            
00060                 key.keyCode(LEFT_ARROW);
00061                 x = acc.getAccX();
00062                 y = acc.getAccY();
00063                 //key.printf("left arrow\n");
00064                 if (x<-1.9)
00065                     {
00066                     //   key.printf("Salto\n");
00067                     key.keyCode(UP_ARROW);
00068                     wait(0.3f);                  
00069                     }
00070                 }
00071             while (y>0.2){
00072                 key.keyCode(RIGHT_ARROW);
00073                 x = acc.getAccX();
00074                 y = acc.getAccY();
00075                 //key.printf("right arrow\n");
00076                 if (x<-1.9)
00077                     {
00078                     //   key.printf("Salto\n");
00079                     key.keyCode(UP_ARROW);
00080                     wait(0.3f);
00081                     }
00082                 }      
00083             if (x<-1.9)
00084                 {
00085              //   key.printf("Salto\n");
00086                 key.keyCode(UP_ARROW);
00087                 x = acc.getAccX();
00088                 wait(0.3f);
00089                }
00090             //wait(0.5);*/
00091         }
00092     }