Code for HW3 - Game Controller Mega Buster that interfaces with flash based Megaman - Megaman Project X

Dependencies:   MMA8451Q mbed

Committer:
bthov
Date:
Mon Sep 21 09:11:20 2015 +0000
Revision:
0:73fee9cd0094
Game Controller;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bthov 0:73fee9cd0094 1 // Bryan Thov and Yinglong Li
bthov 0:73fee9cd0094 2 // HW 3 - Game Controller
bthov 0:73fee9cd0094 3 // IDD Fall '15
bthov 0:73fee9cd0094 4
bthov 0:73fee9cd0094 5 #include "mbed.h"
bthov 0:73fee9cd0094 6 #include "MMA8451Q.h"
bthov 0:73fee9cd0094 7
bthov 0:73fee9cd0094 8 // Signals Sent
bthov 0:73fee9cd0094 9 // s = Button Press = shoot
bthov 0:73fee9cd0094 10 // j = Arm Raised = jump
bthov 0:73fee9cd0094 11 // d = Arm Down = dash
bthov 0:73fee9cd0094 12 // l = Arm turn left = left
bthov 0:73fee9cd0094 13 // r = Arm turn right = right
bthov 0:73fee9cd0094 14 // x = Null -> Not Active
bthov 0:73fee9cd0094 15
bthov 0:73fee9cd0094 16 // I2C
bthov 0:73fee9cd0094 17 PinName const SDA = PTE25;
bthov 0:73fee9cd0094 18 PinName const SCL = PTE24;
bthov 0:73fee9cd0094 19 #define MMA8451_I2C_ADDRESS (0x1d<<1)
bthov 0:73fee9cd0094 20
bthov 0:73fee9cd0094 21 // LED
bthov 0:73fee9cd0094 22 DigitalOut myled(LED_GREEN);
bthov 0:73fee9cd0094 23 DigitalOut myled1(LED_RED);
bthov 0:73fee9cd0094 24 DigitalOut myled2(LED_BLUE);
bthov 0:73fee9cd0094 25
bthov 0:73fee9cd0094 26 // Serial
bthov 0:73fee9cd0094 27 Serial pc(USBTX, USBRX);
bthov 0:73fee9cd0094 28
bthov 0:73fee9cd0094 29 // Game Inputs
bthov 0:73fee9cd0094 30 DigitalIn button(D9);
bthov 0:73fee9cd0094 31
bthov 0:73fee9cd0094 32 // Initialize Instruction String
bthov 0:73fee9cd0094 33 // _ _ _ _ _ \0 = j d l r s \0
bthov 0:73fee9cd0094 34 // If the instruction is not there, will be x, which is "Null"
bthov 0:73fee9cd0094 35 // \0 = Terminiating String
bthov 0:73fee9cd0094 36 char instr[7] = {'x', 'x', 'x', 'x', 'x' ,'a', '\0'};
bthov 0:73fee9cd0094 37
bthov 0:73fee9cd0094 38 int main() {
bthov 0:73fee9cd0094 39 // Set up accelerometer
bthov 0:73fee9cd0094 40 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
bthov 0:73fee9cd0094 41 float x, y, z;
bthov 0:73fee9cd0094 42
bthov 0:73fee9cd0094 43 // Turn off LEDs
bthov 0:73fee9cd0094 44 myled = !myled;
bthov 0:73fee9cd0094 45 myled1 = !myled1;
bthov 0:73fee9cd0094 46 myled2 = !myled2;
bthov 0:73fee9cd0094 47
bthov 0:73fee9cd0094 48 while(1) {
bthov 0:73fee9cd0094 49 // Get accelerometer values
bthov 0:73fee9cd0094 50 float x, y, z;
bthov 0:73fee9cd0094 51 x = acc.getAccX();
bthov 0:73fee9cd0094 52 y = acc.getAccY();
bthov 0:73fee9cd0094 53 z = acc.getAccZ();
bthov 0:73fee9cd0094 54 wait(0.05f);
bthov 0:73fee9cd0094 55 //pc.printf("%1.2f %1.2f %1.2f\r\n", x, y, z);
bthov 0:73fee9cd0094 56
bthov 0:73fee9cd0094 57 // Neglect calculating roll/pitch and use raw analog data = saves time
bthov 0:73fee9cd0094 58 // As x approaches 0 and y is positive, the controller is going up (jumping)
bthov 0:73fee9cd0094 59 // As x approaches 0 and y is negative, the controller is going down (dashing)
bthov 0:73fee9cd0094 60 // Threshold_Min = -0.7 (Tested -> We will finalize)
bthov 0:73fee9cd0094 61 // For y: Threshold_Max and Threshold_Min = +- 0.75 to +- 1.1
bthov 0:73fee9cd0094 62
bthov 0:73fee9cd0094 63 wait_ms(100);
bthov 0:73fee9cd0094 64 if (x > -0.7 & y > 0.75 & y < 1.1 & z > -0.2 & z < 0.2) {
bthov 0:73fee9cd0094 65 instr[0] = 'j';
bthov 0:73fee9cd0094 66 } else {
bthov 0:73fee9cd0094 67 instr[0] = 'x';
bthov 0:73fee9cd0094 68 }
bthov 0:73fee9cd0094 69
bthov 0:73fee9cd0094 70 if (x > -0.7 & y < -0.75 & y > -1.1 & z > -0.2 & z < 0.2) {
bthov 0:73fee9cd0094 71 instr[1] = 'd';
bthov 0:73fee9cd0094 72 } else {
bthov 0:73fee9cd0094 73 instr[1] = 'x';
bthov 0:73fee9cd0094 74 }
bthov 0:73fee9cd0094 75
bthov 0:73fee9cd0094 76 // For Directional Movement
bthov 0:73fee9cd0094 77 // Rest is when z = 0
bthov 0:73fee9cd0094 78 // As z approaches -1, the controller is going right
bthov 0:73fee9cd0094 79 // As z approaches 1, the controller is going left
bthov 0:73fee9cd0094 80 // Threshold_Max and Threshold_Min = +- 0.4
bthov 0:73fee9cd0094 81
bthov 0:73fee9cd0094 82 if (z > 0.4) {
bthov 0:73fee9cd0094 83 instr[2] = 'l';
bthov 0:73fee9cd0094 84 } else {
bthov 0:73fee9cd0094 85 instr[2] = 'x';
bthov 0:73fee9cd0094 86 }
bthov 0:73fee9cd0094 87
bthov 0:73fee9cd0094 88 if (z < -0.4) {
bthov 0:73fee9cd0094 89 instr[3] = 'r';
bthov 0:73fee9cd0094 90 } else {
bthov 0:73fee9cd0094 91 instr[3] = 'x';
bthov 0:73fee9cd0094 92 }
bthov 0:73fee9cd0094 93
bthov 0:73fee9cd0094 94 // Jump + R/L
bthov 0:73fee9cd0094 95 if (x > -0.7 & z < -0.4 & y > 0) {
bthov 0:73fee9cd0094 96 instr[0] = 'j';
bthov 0:73fee9cd0094 97 instr[3] = 'r';
bthov 0:73fee9cd0094 98 } else if (x > -0.7 & z > 0.4 & y > 0) {
bthov 0:73fee9cd0094 99 instr[0] = 'j';
bthov 0:73fee9cd0094 100 instr[2] = 'l';
bthov 0:73fee9cd0094 101 }
bthov 0:73fee9cd0094 102
bthov 0:73fee9cd0094 103 // Dash + R/L
bthov 0:73fee9cd0094 104 if (x > -0.7 & z < -0.4 & y < 0) {
bthov 0:73fee9cd0094 105 instr[0] = 'd';
bthov 0:73fee9cd0094 106 instr[3] = 'l';
bthov 0:73fee9cd0094 107 } else if (x > -0.7 & z > 0.4 & y < 0) {
bthov 0:73fee9cd0094 108 instr[0] = 'd';
bthov 0:73fee9cd0094 109 instr[2] = 'r';
bthov 0:73fee9cd0094 110 }
bthov 0:73fee9cd0094 111
bthov 0:73fee9cd0094 112 // See if button is pressed
bthov 0:73fee9cd0094 113 if(button == 0) {
bthov 0:73fee9cd0094 114 instr[4] = 's';
bthov 0:73fee9cd0094 115 } else {
bthov 0:73fee9cd0094 116 instr[4] = 'x';
bthov 0:73fee9cd0094 117 }
bthov 0:73fee9cd0094 118
bthov 0:73fee9cd0094 119 // Print out instruction string and reset rest
bthov 0:73fee9cd0094 120 pc.printf(instr);
bthov 0:73fee9cd0094 121 wait_ms(50);
bthov 0:73fee9cd0094 122 }
bthov 0:73fee9cd0094 123 }
bthov 0:73fee9cd0094 124