21 November 2023

Game Controller

Tennis Masters Controller

    This is my controller for the flash game "Tennis Masters". Tennis Masters is a free 2D tennis game that anyone can play online from any computer. The racket affords swinging and scrolling with the wheel. The connection between the racket and the game is self-explanatory, and the controls are fairly intuitive. Why play tennis by pushing buttons on a keyboard when you can swing around a real racket? This controller adds another dimension of coordination and skill to an otherwise simple game. These aspects really fit the spirit of a sports game. All of the components are well secured and protected under the hard plastic cover, but easily accessed if needed. The controller consists of a potentiometer wired to the Circuit Playground Bluefruit, powered by a rechargeable battery. The potentiometer is used to control the movement of the character, and the change in position of the accelerometer determines if the player swings, and in what direction. Unfortunately, I have had to rush this submission as I will not have access to a computer for the next week, I apologize for the lack of video quality. However, the controller itself is still fully functional. What areas could I improve on? should I make the cover red to match the racket? How could I improve the code to make it more intuitive?

https://youtu.be/D7ZVoY6oJzg

The Code:

#include <bluefruit.h>
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <Wire.h>
#include <SPI.h>

#define ANALOG_INPUT  A4
#define VALUE_MIN     0
#define VALUE_MAX     1023

BLEDis bledis;
BLEHidAdafruit blehid;
int debounce = 100;

void setup() {
  // put your setup code here, to run once:
CircuitPlayground.begin();
pinMode(CPLAY_SLIDESWITCHPIN, INPUT_PULLUP); // Sets up switch used to
disable/enable controller
 Serial.begin(9600);

 Bluefruit.begin(); // Bluetooth setup
  Bluefruit.setTxPower(4);
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather 52");
  bledis.begin();
  blehid.begin();
  startAdv();
  delay(1000);
}

void loop() {

  float value = analogRead(ANALOG_INPUT); // Reads input from the potentiometer
  Serial.println(value, DEC);

  if (value > 600) {
    blehid.keyPress('d'); // moves character to the right
    delay(10);
  }

if ((value < 550) && (value > 450)){
    blehid.keyRelease(); // stops all movement
    delay(50);
  }

  if (value < 400) {
    blehid.keyPress('a'); // moves character to the left
    delay(10);
  }

if(digitalRead(CPLAY_SLIDESWITCHPIN)) {
float Y = CircuitPlayground.motionY(); // gets y rotation
delay(100);
float X = CircuitPlayground.motionY(); // gets y rotation 1/10th of a second later

  if((X > (Y+3)) && Bluefruit.connected() && (X > 2) ) { //checks if
controller rotated downwards
    blehid.keyPress('z'); // z input gives an overhand swing in game
    delay(100);
    blehid.keyRelease();
    delay(300);
  }

  else if((X < (Y-3)) && Bluefruit.connected() && (X < -1)) { //checks if
controller rotated upwards
    blehid.keyPress('x'); // x input gives an underhand input in game
    delay(100);
    blehid.keyRelease();
    delay(300);
  }

else  {
    blehid.keyRelease(); // releases all keys if none of the above conditions
are met
    delay(10);
  }

Serial.println(Y);
}

}

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
}
(The rubric says to format the code differently, but I find this to be much more legible and clear)


No comments:

Post a Comment

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