Somatosensory game controller
From Microduino Wiki
Language: | English • 中文 |
---|
ContentsObjectiveYou can control games by the somatosensory analog keyboard--W, A, S and D. PrincipleDetect 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
Hardware Buildup
Software DebuggingCode Description
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");
}
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);
} ResultOpen 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 |