27 November 2023

Paperboy Controller


    This is the eventual design I came up with for my game controller for the classic NES game Paperboy. It’s fairly straightforward and emulates exactly what you would do if you were actually in the game. This is similar to the Wii and its controllers, as the motions you made with the Wii remote simulated what was happening on screen such as swinging a tennis racket or throwing a bowling ball. Since the game I chose was Paperboy, the design is made to be a pair of scooter or bicycle handlebars, with a bell on top that most bicycles also have. The Circuit Playground Express is connected to an Adafruit LSM9DS1 that is kept inside a box attached to the stem of the handlebars. The LSM9DS1 is constantly sending rotational data to the Circuit Playground through the gyro that is configured within it. It’s split up into the X, Y, and Z direction but for the purpose of my controller I’m only tracking the Y direction. These values are measured in floats and when the Y value of the gyro goes above or below a certain threshold it triggers a key press on the keyboard and makes the player in-game turn left or right. The sound sensor on the Circuit Playground is also used to detect whenever the bell on top of the handlebars is rung. This triggers a button press that makes the player in-game throw a newspaper. I think the theme of my controller matches the game I chose really well and it’s honestly very fun to use. One thing I would like to know is how I could make my controller more ascetically pleasing, because right now it’s a lot of black because I used duct tape to hold a lot of the pieces together.



#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM9DS1.h>

#include <Keyboard.h>
#include <KeyboardLayout.h>
#include <Keyboard_da_DK.h>
#include <Keyboard_de_DE.h>
#include <Keyboard_es_ES.h>
#include <Keyboard_fr_FR.h>
#include
<Keyboard_it_IT.h>
#include <Keyboard_sv_SE.h>

#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>

Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();
#define LSM9DS1_SCK A5
#define LSM9DS1_MISO 12
#define LSM9DS1_MOSI A4
#define LSM9DS1_XGCS 6
#define LSM9DS1_MCS 5
//All of my libraries and a debounce
int debounce = 0;

void setupSensor()
{
  //This sets up the ranges and sensitivity of the LSM9DS1
  // 1.) Set the accelerometer range
  lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_2G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_4G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_8G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_16G);
 
  // 2.) Set the magnetometer sensitivity
  lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_8GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_12GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_16GAUSS);

  // 3.) Setup the gyroscope
  lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_245DPS);
  //lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_500DPS);
  //lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_2000DPS);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  CircuitPlayground.begin();
  Keyboard.begin();
  pinMode(CPLAY_SLIDESWITCHPIN, INPUT_PULLUP);
  setupSensor();
  delay(100);
}

void loop() {
  lsm.read();

  sensors_event_t a, m, g, temp;
  lsm.getEvent(&a, &m, &g, &temp);
  float soundLevel = CircuitPlayground.mic.soundPressureLevel(10);

  Serial.print("Accel X: "); Serial.print(a.acceleration.x); Serial.print(" m/s^2");
  Serial.print("\tY: "); Serial.print(a.acceleration.y);     Serial.print(" m/s^2 ");
  Serial.print("\tZ: "); Serial.print(a.acceleration.z);     Serial.println(" m/s^2 ");

  Serial.print("Mag X: "); Serial.print(m.magnetic.x);   Serial.print(" uT");
  Serial.print("\tY: "); Serial.print(m.magnetic.y);     Serial.print(" uT");
  Serial.print("\tZ: "); Serial.print(m.magnetic.z);     Serial.println(" uT");

  Serial.print("Gyro X: "); Serial.print(g.gyro.x);   Serial.print(" rad/s");
  Serial.print("\tY: "); Serial.print(g.gyro.y);      Serial.print(" rad/s");
  Serial.print("\tZ: "); Serial.print(g.gyro.z);      Serial.println(" rad/s");
  //The code above reads out all of the raw values for the accelerometer, gyro, and magnetometer in the serial monitor
  Serial.println();
  delay(100);

  // put your main code here, to run repeatedly:

  //Makes sure the circuit playground is switched on before you start playing

  if(digitalRead(CPLAY_SLIDESWITCHPIN))
  {
    //The two if statements below detect the gyro values as floats and turn the bicycle left and right by in game by pressing arrow keys when a certain threshold is reached
    //Also makes sure to release the keys when the handles are not turned in a direction
    if (g.gyro.y >= 30)
    {
      Keyboard.press(KEY_RIGHT_ARROW);
      Serial.println("Right Key Pressed");
      delay(debounce);
    }
    else
    {
      Keyboard.releaseAll();
    }
    if (g.gyro.y <= -30)
    {
      Keyboard.press(KEY_LEFT_ARROW);
      Serial.println("Left Key Pressed");
      delay(debounce);
    }
    else
    {
      Keyboard.releaseAll();
    }
    //This detects sound whenever the player rings a bell which makes the character throw a paper in game
    if(soundLevel >= 75)
    {
      Keyboard.press('X');
      Serial.println("X pressed");
      delay(debounce);
    }
    else
    {
      Keyboard.releaseAll();
    }
  }
}

 

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.