Game controller_v2: 091815. changed to read acceleration in x axis
Dependencies: DebounceIn MMA8451Q USBDevice mbed
Fork of HW3_Game_Controller by
gamecontroller2.cpp@1:f7b35d39b137, 2015-09-19 (annotated)
- Committer:
- jessielt
- Date:
- Sat Sep 19 01:03:09 2015 +0000
- Revision:
- 1:f7b35d39b137
Game controller_v2 091815
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jessielt | 1:f7b35d39b137 | 1 | #include "mbed.h" |
jessielt | 1:f7b35d39b137 | 2 | #include "DebounceIn.h" |
jessielt | 1:f7b35d39b137 | 3 | #include "USBKeyboard.h" |
jessielt | 1:f7b35d39b137 | 4 | #include "MMA8451Q.h" |
jessielt | 1:f7b35d39b137 | 5 | |
jessielt | 1:f7b35d39b137 | 6 | PinName const SDA = PTE25; |
jessielt | 1:f7b35d39b137 | 7 | PinName const SCL = PTE24; |
jessielt | 1:f7b35d39b137 | 8 | #define MMA8451_I2C_ADDRESS (0X1d<<1) |
jessielt | 1:f7b35d39b137 | 9 | Serial pc(USBTX, USBRX); |
jessielt | 1:f7b35d39b137 | 10 | |
jessielt | 1:f7b35d39b137 | 11 | USBKeyboard keyboard; |
jessielt | 1:f7b35d39b137 | 12 | |
jessielt | 1:f7b35d39b137 | 13 | DigitalOut ledr(LED_RED); |
jessielt | 1:f7b35d39b137 | 14 | DigitalOut ledg(LED_GREEN); |
jessielt | 1:f7b35d39b137 | 15 | DigitalOut ledb(LED_BLUE); |
jessielt | 1:f7b35d39b137 | 16 | |
jessielt | 1:f7b35d39b137 | 17 | DebounceIn pb1(D13); |
jessielt | 1:f7b35d39b137 | 18 | DebounceIn pb2(D3); |
jessielt | 1:f7b35d39b137 | 19 | DebounceIn pb3(D4); |
jessielt | 1:f7b35d39b137 | 20 | DebounceIn pb4(D5); |
jessielt | 1:f7b35d39b137 | 21 | DebounceIn pb5(D6); |
jessielt | 1:f7b35d39b137 | 22 | DebounceIn pb6(D7); |
jessielt | 1:f7b35d39b137 | 23 | DebounceIn pb7(D8); |
jessielt | 1:f7b35d39b137 | 24 | DebounceIn pbs(D9); |
jessielt | 1:f7b35d39b137 | 25 | DebounceIn pbm(D10); |
jessielt | 1:f7b35d39b137 | 26 | |
jessielt | 1:f7b35d39b137 | 27 | int main() |
jessielt | 1:f7b35d39b137 | 28 | { |
jessielt | 1:f7b35d39b137 | 29 | int m = 1; |
jessielt | 1:f7b35d39b137 | 30 | ledr = 1; |
jessielt | 1:f7b35d39b137 | 31 | ledg = 1; |
jessielt | 1:f7b35d39b137 | 32 | ledb = 1; |
jessielt | 1:f7b35d39b137 | 33 | // Use internal pullup for pushbutton |
jessielt | 1:f7b35d39b137 | 34 | pb1.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 35 | pb2.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 36 | pb3.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 37 | pb4.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 38 | pb5.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 39 | pb6.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 40 | pb7.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 41 | pbs.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 42 | pbm.mode(PullUp); |
jessielt | 1:f7b35d39b137 | 43 | |
jessielt | 1:f7b35d39b137 | 44 | // Delay for initial pullup to take effect |
jessielt | 1:f7b35d39b137 | 45 | wait(.001); |
jessielt | 1:f7b35d39b137 | 46 | |
jessielt | 1:f7b35d39b137 | 47 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
jessielt | 1:f7b35d39b137 | 48 | |
jessielt | 1:f7b35d39b137 | 49 | float x; |
jessielt | 1:f7b35d39b137 | 50 | float y; |
jessielt | 1:f7b35d39b137 | 51 | float z; |
jessielt | 1:f7b35d39b137 | 52 | |
jessielt | 1:f7b35d39b137 | 53 | while(true) |
jessielt | 1:f7b35d39b137 | 54 | { |
jessielt | 1:f7b35d39b137 | 55 | x = acc.getAccX(); |
jessielt | 1:f7b35d39b137 | 56 | y = acc.getAccY(); |
jessielt | 1:f7b35d39b137 | 57 | z = acc.getAccZ(); |
jessielt | 1:f7b35d39b137 | 58 | |
jessielt | 1:f7b35d39b137 | 59 | |
jessielt | 1:f7b35d39b137 | 60 | //keyboard.printf("X = %1.2f ", x); |
jessielt | 1:f7b35d39b137 | 61 | //keyboard.printf("Y = %1.2f ", y); |
jessielt | 1:f7b35d39b137 | 62 | //keyboard.printf("Z = ", z); |
jessielt | 1:f7b35d39b137 | 63 | |
jessielt | 1:f7b35d39b137 | 64 | if (x < -0.35) {keyboard.keyCode(LEFT_ARROW); |
jessielt | 1:f7b35d39b137 | 65 | wait(.2); |
jessielt | 1:f7b35d39b137 | 66 | |
jessielt | 1:f7b35d39b137 | 67 | } |
jessielt | 1:f7b35d39b137 | 68 | if (x > 0.35) {keyboard.keyCode(RIGHT_ARROW); |
jessielt | 1:f7b35d39b137 | 69 | wait(.2); |
jessielt | 1:f7b35d39b137 | 70 | } |
jessielt | 1:f7b35d39b137 | 71 | if (pb1==0) {keyboard.keyCode(UP_ARROW); //rotates tiles |
jessielt | 1:f7b35d39b137 | 72 | wait(.2); |
jessielt | 1:f7b35d39b137 | 73 | } |
jessielt | 1:f7b35d39b137 | 74 | if (z < 0.2) {keyboard.keyCode('d'); // drops tile |
jessielt | 1:f7b35d39b137 | 75 | wait(.2); |
jessielt | 1:f7b35d39b137 | 76 | } |
jessielt | 1:f7b35d39b137 | 77 | |
jessielt | 1:f7b35d39b137 | 78 | } |
jessielt | 1:f7b35d39b137 | 79 | |
jessielt | 1:f7b35d39b137 | 80 | } |