HW 3 game

Dependencies:   DebounceIn MMA8451Q USBDevice mbed

Fork of HW3 by Elena Duran

main.cpp

Committer:
JMarquis
Date:
2015-09-19
Revision:
1:716678407ac4
Parent:
0:a80df48a0372

File content as of revision 1:716678407ac4:

#include "mbed.h"
//#include "PinDetect.h"
#include "MMA8451Q.h"
#include "USBKeyboard.h"

USBKeyboard key;

// define I2C Pins and address for KL25Z. Taken from default sample code.
PinName const SDA = PTE25;
PinName const SCL = PTE24;
AnalogIn j(A0);
AnalogIn n(A5);
AnalogIn k(A2);
//PinDetect P0(D2);

#define MMA8451_I2C_ADDRESS (0x1d<<1)

//serial connection to PC via USB
//Serial pc(USBTX, USBRX);

int main(void)
{
    //configure on-board I2C accelerometer on KL25Z
    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 
    //map read acceleration to PWM output on red status LED
        float p;
       float q;
      float r;
      
      state = 1;
    while (true) {
        float x, y, z;
            x = acc.getAccX();
            y = acc.getAccY();
            wait(0.05f);
            p= j.read(); 
             q= n.read();
             r= k.read();
        
if (p>.2) {
    if (q<.2){
         if(r<.2){
             key.keyCode('1',KEY_CTRL);
                 }
             }
    if (q>.2){
         if (r<.2) {
            key.keyCode('2',KEY_CTRL);
                    }
         if (r>.2) {
            key.keyCode('3',KEY_CTRL);
                   }
             }
          }    
            
             
 
            
            while (y<-0.2){            
                key.keyCode(LEFT_ARROW);
                x = acc.getAccX();
                y = acc.getAccY();
                //key.printf("left arrow\n");
                if (x<-1.9)
                    {
                    //   key.printf("Salto\n");
                    key.keyCode(UP_ARROW);
                    wait(0.3f);                  
                    }
                }
            while (y>0.2){
                key.keyCode(RIGHT_ARROW);
                x = acc.getAccX();
                y = acc.getAccY();
                //key.printf("right arrow\n");
                if (x<-1.9)
                    {
                    //   key.printf("Salto\n");
                    key.keyCode(UP_ARROW);
                    wait(0.3f);
                    }
                }      
            if (x<-1.9)
                {
             //   key.printf("Salto\n");
                key.keyCode(UP_ARROW);
                x = acc.getAccX();
                wait(0.3f);
               }
            //wait(0.5);*/
        }
    }