Close
0%
0%

Accelship an Accelerometer-Based LCD Game

Project to create a tiny game console controlled using an Accelerometer, and Arduino Nano

Similar projects worth following
The goal for this project was to create a tiny games console, and raise the stakes what if it is controlled completely using an accelerometer? The Accelship game uses an Arduino Nano, MPU9250, and a 64x128 LCD Display to create a classic arcade style game. This project uses a 3D printed housing, and over the course I experimented with numerous ideas including FDM stencil PCB's, bytecode manipulation to create a lightweight graphics and display code, and PCB Design using Autodesk Eagle.

The code for the project is into 4 main parts: ship, walls, collision detection, and accelerometer input. I found this easiest as each part of the program can operate on its own independent update rate. An example of this is for the wall code where the position of the wall updates based on the speed which is controlled by the player's current score.

See Wall Update Code Below:

void updateWall(){
  if (y_wall==0){
  y_wall = SCREEN_HEIGHT;
  x_wall = random(w_gap/2, SCREEN_WIDTH-w_gap/2);
  score++;
  sspeed = 100 - score*5;
  if (ttspeed<=5){sspeed =5;}
  }

  double c_time = millis();
  if ((c_time-prev_time) > sspeed){
  y_wall--;
  prev_time=c_time;
  }
}

void displayWall(){
  display.drawRect(0, y_wall , x_wall-w_gap/2,10, SSD1306_WHITE);
  display.drawRect(x_wall+w_gap/2, y_wall , SCREEN_WIDTH - (x_wall+w_gap/2),10, SSD1306_WHITE);
}

 Along with this was also the need to create code to control the ship, this is directly handled by the accelerometer the simplest way to explain this is that the Y-Axis acceleration is divided by the magnitude of the acceleration. This allows the game to determine the current direction the IMU Is rotated based on the direction of gravity. This also accounts for any movement the player makes as any stray acceleration may cause the sensors to read an acceleration which is not caused by gravity.

Ship and Accelerometer Code:

void updateAccelerometer(){
  uint8_t sensorId;
  int result;

  result = mySensor.readId(&sensorId);
  if (result == 0) {
    Serial.println("sensorId: " + String(sensorId));
  } else {
    Serial.println("Cannot read sensorId " + String(result));
  }

  result = mySensor.accelUpdate();
  if (result == 0) {
    aX = mySensor.accelX();
    aY = mySensor.accelY();
    aZ = mySensor.accelZ();
    aSqrt = mySensor.accelSqrt();
    Serial.println("accelX: " + String(aX));
    Serial.println("accelY: " + String(aY));
    Serial.println("accelZ: " + String(aZ));
    //Serial.println("accelSqrt: " + String(aSqrt));
  } else {
    //Serial.println("Cannod read accel values " + String(result));
  }
}

int initial_w_ship = 10;
int w_ship = 10;
int h_ship = 10;
int x_ship = SCREEN_WIDTH/2;
int y_ship = 0;

void updateShip()
{
  x_ship = -1*aY*SCREEN_WIDTH/2 + SCREEN_WIDTH/2 ;
  // use accelerometer data to update ship position
}

I will save the part of the program that handles the collision detection as it is relatively simple and uses a square bounding box to measure if the points representing the perimeter of the box pass within the barriers. This part also is used to reset the game in the case of a collision, resetting the score and game speed in the process.

TopCover.STL

STL File for 3D-Printable Case

Standard Tesselated Geometry - 496.37 kB - 09/10/2024 at 07:24

Download

AccelSpaceship.ino

Code to control LCD Screen, and MPU9250 Accelerometer over I2C port on Arduino Nano

ino - 5.04 kB - 09/10/2024 at 07:24

Download

  • 1 × Arduino Nano
  • 1 × MPU9250 IMU
  • 1 × SSD1306 64x128 Resolution LCD Screen
  • 1 × PLA+ Fllament

  • Lightweight Graphics Library

    rcrussel09/10/2024 at 07:42 0 comments

    One issue I encountered when trying to get the game running was that the 1306 Library took up too much space in the memory, originally, I had the idea to control the game using lidar to create a stop/start and pause feature. Unfortunately, the memory required to operate the VL53L0X exceeds the 32KB capacity of the Arduino Nano. After trying to write a custom library I found that unless I was able to create some very low-level optimized graphics functions it would be unlikely that it would be possible to use a lidar sensor with the project.

View project log

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates