18 April 2022

Project: Game Controller

 


So for my game controller project, I decided to make a controller for Pokèmon Emerald. Since Pokèmon games ported to PC/Mac have the same controls, it could hypothetically work on any Pokèmon game besides Ranger or the Mystery Dungeon games. The goal of the controller was to have movement so players felt more immersed in the game, as that way a big part of Pokèmon Rangers' appeal and success amongst the fanbase. The controller itself was a 3D printed pokèball with a hole where the button would go. The CPE is inside of the controller and the hole is left open so the USB can connect to the CPE so it's powered. Once on, you'd rotate the controller to move one way and vice versa to move the opposite way. So right to move right and left to move left. The same applies to up and down. These controls are straightforward and not too complex, just like the base controls for Pokèmon games. To select you'd shake the Pokèball, which is sort of a callback to how the Pokèball shakes when attempting to capture a wild Pokèmon. My questions are related to problems I ran into when trying to use the CPE inside of the enclosure.


1. Since I ran into stability problems with the accelerometer once the CPE was inside, should I have printed an extra part like a platform to keep the CPE stable whilst inside the enclosure?

2. Are there any changes I could've made to the code to try and work around not having that platform part inside of the enclosure?


#include <Adafruit_Circuit_Playground.h>

#include <bluefruit.h>

BLEDis bledis;

BLEHidAdafruit blehid;


//Code by Justice Anderson


//boolean shook = false;

int shakeThresh = 25; //setting threshold for user shake

int debounce = 100;


void setup() {

  Bluefruit.begin();

  Bluefruit.setTxPower(4);

  bledis.setManufacturer("Adafruit Industries");

  bledis.setModel("Bluefruit Feather 52");

  bledis.begin();

  blehid.begin();

  startAdv();


  CircuitPlayground.begin();

  pinMode(4, INPUT_PULLDOWN);

  Serial.begin(9600);

  delay(1000);

}


void loop() {

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


 //Defining X-axis for the accelerometer

 float x = CircuitPlayground.motionX();

  if (x < -3 ) {

      blehid.keyPress('a');

      blehid.keyRelease();

      delay(100);

    } else if (x > -3) {

      blehid.keyRelease();

      delay(100);

    }    Serial.println (x);


//Defining Y-Axis for Accelerometer

   float y = CircuitPlayground.motionY();

   if (y < 0) {

      blehid.keyPress('d'); //Send D key to accelerometer

      blehid.keyRelease();

      delay(100);

    } else if (y > 0) {

      blehid.keyRelease();

      delay(100);

      }


//Defining Z-Axis for Accelerometer

 float z = CircuitPlayground.motionZ();

  if(z < -5) {

    blehid.keyPress('w'); //Send W key to keyboard if tilted

    blehid.keyRelease();

    delay(100);

  } else if(z > -5) {

    blehid.keyPress('s'); //Send S key to keyboard if tilted

    blehid.keyRelease();

    delay(100);

   }


Serial.println(z);


//Defining shake sensor for the CPE

   float shake = abs(CircuitPlayground.motionX())

    + abs(CircuitPlayground.motionY())

    + abs(CircuitPlayground.motionZ());

    if(shake > shakeThresh) {

        blehid.keyPress('z'); //Send z key to the keyboard if shake threshold is passed

        blehid.keyRelease();

      }

  

}




void startAdv(void)

{  

  // Advertising packet

  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);

  Bluefruit.Advertising.addTxPower();

  Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD);

  

  // Include BLE HID service

  Bluefruit.Advertising.addService(blehid);


  // There is enough room for the dev name in the advertising packet

  Bluefruit.Advertising.addName();

  

  /* Start Advertising

   * - Enable auto advertising if disconnected

   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms

   * - Timeout for fast mode is 30 seconds

   * - Start(timeout) with timeout = 0 will advertise forever (until connected)

   * 

   * For recommended advertising interval

   * https://developer.apple.com/library/content/qa/qa1931/_index.html   

   */

  Bluefruit.Advertising.restartOnDisconnect(true);

  Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms

  Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode

  Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds

}


No comments:

Post a Comment

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