Somatosensory game controller

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文

Objective

You can control games by the somatosensory analog keyboard--W, A, S and D.

Principle

Detect game controller's motion change with three-axis acceleration sensor MPU6050 and then use Microduino-CoreUSB analog function to simulate keys of W, A, S and D.


Equipment

Module Number Function
Microduino-CoreUSB 1 Core board
Microduino-10DOF 1 Vector sensor module

Hardware Buildup

  • Setup 1:Connect CoreUSB to the computer, open program examples, select the right board and serial port, then download the program.

Motion_Handle

  • Setup 2:Stack CoreUSB and 10DOF.
  • Setup 3:Connect CoreUSB to the computer which will identify a keyboard and display " HID Keyboard Device " in the device manager.
Microduino-sensorhub rule.JPG

Software Debugging

Code Description

  • Control variable initialization:
MPU6050 accelgyro;   //Three-axis acceleration sensor 
int ax,ay,az;       //Three-axis acceleration sensor variable
float Ax,Ay,Az;

void setup(){
  Serial.begin(9600);
  Keyboard.begin();      //Keyboard simulation begins.
  Serial.println("Initializing I2C devices...");
  accelgyro.initialize();     //Accelerator initializes. 
    // verify connection 
  Serial.println("Testing device connections...");
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
}
  • Judge the controller's motion status. Moving towards left, right, front and back corresponds to the key A, D, W and S respectively.
   void loop() {
    accelgyro.getAcceleration(&ax, &ay, &az); 
    Ax = ax/16384.00;         //Calculate X-axis's acceleration 
    Ay = ay/16384.00;         // Calculate Y-axis's acceleration
    Az = az/16384.00;         // Calculate Z-axis's acceleration
    if(Ay >= 0.6 )            //Turn the controller leftwards 
    {
      Keyboard.press('A');    //Keyboard 'A'     
    }
    else if(Ay <= -0.6)       //Turn the controller rightwards 
    {
      Keyboard.press('D');   //Keyboard 'D' 
    }
    else if(Ax >= 0.6)       //Turn forward 
    {
      Keyboard.press('S');   //Keyboard 'S' 
    }
    else if(Ax <= -0.6)     //Turn backward 

    {
       Keyboard.press('W'); //Keyboard 'W'
    } 
    else
    {
        Keyboard.releaseAll();  //Release the key 
    }   
    delay(200); 
}

Result

Open NotePad on the computer and connect to Coreusb. By turning the controller leftwards, you'll see the character "A" is printed on the NotePad while by turning the controller back to the original position and you'll see the NotePad stops printing. That also works for the other keys.

Video