Move the controller left and right to turn
- Jason Ron
- May 18, 2021
- 1 min read
Updated: Jun 2, 2021
This project is using an accelerometer taped onto an Xbox controller to turn. This is inspired by people who always lean the controller to turn when they play racing games. This project uses the MPU 6050 accelerometer and gyroscope. Like a Wii controller but you can play your old favorites like star wars episode 1 racer.
Please use the following resources to educate yourself on the sensor and download the following libraries. Remember to calibrate your sensor. I copied most of this code from the first link.
https://www.youtube.com/watch?v=USa3HFLnrlk&t=218s (minute 3:40) best info I got by far because this is how you get the equation to get the angle in using the y acceleration!!!
Circuit diagram

Hardware setup:

Arduino code: So simple yet it works so good! Copy the hard stuff !
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Keyboard.h>
int yang;
const int inputpin = 7;
int lastButtonState2 = 0;
int buttonState2 = 0;
int buttonPushCounter2 = 0;
MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy, vx_prec, vy_prec;
int count=0;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
if (!mpu.testConnection()) {
while (1);
}
}
void loop() {
buttonState2 = digitalRead(inputpin);
if (buttonState2 != lastButtonState2) {
if (buttonState2 == LOW) {
buttonPushCounter2++;
}
}
lastButtonState2 = buttonState2;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
yang = ay/182.04; // magic equation
Serial.print(" y angle = ");
Serial.println(yang);
delay(200);
if (buttonPushCounter2 == 1)
{
Keyboard.begin();
if(yang> 35 && yang <100 ){
//Send the message
Keyboard.press(KEY_LEFT_ARROW);
}
else if(yang< -35 && yang>-90 ){
//Send the message
Keyboard.press(KEY_RIGHT_ARROW);
}
else{
Keyboard.releaseAll();
}
}
Keyboard.end();
}
Comments