18 April 2022

Project: Game Controller - Dig Dug Pump Controller

 

DIG DUG PUMP



For this assignment, I created a controller for the game Dig Dug with a conceptual model based on the pump the player uses in the game. In Dig Dug, the player is tasked with defeating enemies using their pump to inflate them until they burst, all the while digging through the ground in order to get to them.

In order to simulate this, the player must pump the plunger at the top of the controller in order to send out their pump, mapped to the X key for the emulator's controls. The player must use the potentiometer knobs on the front of the enclosure in order to move vertically (left/red knob) and horizontally (right/blue knob), mapped to the up and down arrow keys and the left and right arrow keys respectively for the emulator's controls.

The enclosure has clear signifiers telling the player what to do in order to use it, with the pump itself reflecting the conceptual model of a standard real-life pump leading the player to pump it for the pumping mechanic, and the potentiometers labeled clearly as vertical and horizontal arrows representing the arrow keys and also the literal directions in which the player will move.


The back of the enclosure has two rudimentary access doors in order to troubleshoot and later potentially disassemble the controller and retrieve my supplies in the event that I need to reuse any internal elements.

The bottom half of the enclosure is where the CPB resides, with any wiring connecting to it attached here. This is also where the wire connecting the controller to the computer and, by extension, power source, leads out of the controller.



The top half of the enclosure is where the breadboard and other wiring reside atop a platform in the middle of the enclosure, with the potentiometers also glued into the enclosure here. The pump works by closing a circuit using two pieces of aluminum foil- one connected to positive voltage and one connected to ground- using a breadboarding wire attached to each respective piece of foil.




Code:
// Jacqueline Pavlat
// Final Project: Game Controller
// Dig Dug Pump


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

// Keyboard Setup

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
  TUD_HID_REPORT_DESC_KEYBOARD()
};
 
// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);

//Debouncing
const int debounce = 100;

// Variables
int VPot = 0;
int HPot = 0;
int Vertical = 0;
int Horizontal = 0;

void setup()
{
 
  Serial.begin(9600);
  delay(1000);
  CircuitPlayground.begin();

  //PinOuts
  pinMode(A1, INPUT_PULLUP); // Pump
  pinMode(A2, INPUT_PULLDOWN); // Blue/Right Knob (Horizontal Movement)
  pinMode(A3, INPUT_PULLDOWN); // Red/Left Knob (Vertical Movement)

  pinMode(CPLAY_REDLED, OUTPUT); //LED

  usb_hid.begin();

}

void loop()
{

//Keyboard Setup

  uint8_t const report_id = 0; //unsigned 8bit (1 byte) int
  uint8_t const modifier = 0; //used by libraries to save space
  uint8_t keycode[6] = { 0 }; //normal int is 16 bit (4 bytes)

  //Pump
  if(digitalRead(A1) == 0)
  {
    //Maps Pump to X
    keycode[0] = HID_KEY_X;
    usb_hid.keyboardReport(report_id, modifier, keycode);
    delay(debounce);
    usb_hid.keyboardRelease(0);
  }

  //Horizontal Movement

  //Move Left
  HPot = analogRead(A2);
  Horizontal = map(HPot, 0, 1023, 0, 10);
  
  if(Horizontal == 10)
  {
    keycode[0] = HID_KEY_ARROW_LEFT;
    usb_hid.keyboardReport(report_id, modifier, keycode);
    delay(debounce);
  }

  //Stop Moving Horizontally
  if(Horizontal != 0 && Horizontal != 10)
  {
    usb_hid.keyboardRelease(0);
  }

  //Move Right
  if(Horizontal == 0)
  {
    keycode[0] = HID_KEY_ARROW_RIGHT;
    usb_hid.keyboardReport(report_id, modifier, keycode);
    delay(debounce);
  }

  //Vertical Movement

  VPot = analogRead(A3);
  Vertical = map(VPot, 0, 1023, 0, 10);
  
  if(Vertical == 10)
  {
    keycode[0] = HID_KEY_ARROW_UP;
    usb_hid.keyboardReport(report_id, modifier, keycode);
    delay(debounce);
  }

  //Stop Moving Vertically
  if(Vertical != 0 && Horizontal != 10)
  {
    usb_hid.keyboardRelease(0);
  }

  //Move Down
  if(Vertical == 0)
  {
    keycode[0] = HID_KEY_ARROW_DOWN;
    usb_hid.keyboardReport(report_id, modifier, keycode);
    delay(debounce);
  }
  
}

Unfortunately, during the process of creating this controller, my CPB began to malfunction and is no longer able to be recognized by my computer. Even with extra time, I was unable to fix my Circuit Playground. As a result, I am unable to demonstrate the controller's functionality, however, at the very least this can be gathered via the wiring and code presented.

Review Question:

  • How would you improve the code relating to the potentiometers?

No comments:

Post a Comment

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