Close

Coding of accelerometer

A project log for Subway Surfer game controller

Immersive Subway Surfer controller: Stand on a real hoverboard to control the game with realistic motion!

ccinourCéciNour 12/10/2024 at 10:130 Comments

19/11/2024

Second code using Arduino uno: 

We needed to detect the tilting of the accelerometer and see if our code was working by printing 'right', 'left', 'up' and 'down', when the accelerometer reaches a certain value in the X-axis and Y-axis. The X-axis will be dedicated to 'right' and 'left'. The Y-axis will be dedicated to 'up' and 'down'.

#include <wire.h>
#include <MPU6050.h>
 
 
void setup() {
  Wire.begin();
  mpu.initialize();
 
  Serial.begin(9600);
  while (!Serial) {
    // Wait for the serial connection to establish
  }
 
  // Check if the MPU6050 is connected properly
  if (mpu.testConnection()) {
    Serial.println("MPU6050 connected successfully!");
  } else {
    Serial.println("MPU6050 connection failed!");
    while (1); // Halt the program
  }
}
 
void loop() {
  int16_t ax, ay, az;
 
  // Read acceleration values
  mpu.getAcceleration(&ax, &ay, &az);
 
  // Normalize accelerometer values to detect motion direction
  float normAx = ax / 16384.0; // Normalize to g (assuming ±2g sensitivity)
  float normAy = ay / 16384.0;
 
  // Determine direction
  if (normAx > 0.5) {
    Serial.println("Motion detected: RIGHT");
  } else if (normAx < -0.5) {
    Serial.println("Motion detected: LEFT");
  }
 
  if (normAy > 0.5) {
    Serial.println("Motion detected: DOWN");
  } else if (normAy < -0.5) {
    Serial.println("Motion detected: UP");
  }
 
  // Debug: Print raw acceleration values
  Serial.print("Ax: "); Serial.print(normAx);
  Serial.print(" | Ay: "); Serial.print(normAy);
  Serial.print(" | Az: "); Serial.println(az / 16384.0);
 
  delay(100); // Adjust for your desired update frequency
}

Adding the code to link the Arduino to a keyboard: 

We did the same but we linked it to a keyboard. When the accelerometer reaches a certain value on the X, Y axis, the keyboard should press down the corresponding arrow on the keyboard. 

The problem is that we needed it to release after pressing down, so we added keyboard.release ( ) if no motion was detected. 

if (normAx > 0.5) {

    // if motion X is positive, press right arrow

    Keyboard.press(KEY_RIGHT_ARROW);

  } else if (normAx < -0.5) {

    // if motion X is negative, press left arrow

    Keyboard.press(KEY_LEFT_ARROW);

  } else {

    // if no motion is detected, release arrows

    Keyboard.release(KEY_LEFT_ARROW);

    Keyboard.release(KEY_RIGHT_ARROW);

  }

  if (normAy > 0.5) {

    // if motion Y is positive, press down arrow

    Keyboard.press(KEY_DOWN_ARROW);

  } else if (normAy < -0.5) {

    //if motion Y is positive, press up arrow

    Keyboard.press(KEY_UP_ARROW);

  } else {

    //if no motion is detected, release arrows

    Keyboard.release(KEY_UP_ARROW);

    Keyboard.release(KEY_DOWN_ARROW);

  }

We have tried many time but it seems that the code wasn't working at all. 

Discussions