TriOmega
Published

Castlevania (NES) Motion Controller

A strange controller designed to use motion to control the original Castlevania for the NES. Made for my Physical Computing class.

AdvancedShowcase (no instructions)59
Castlevania (NES) Motion Controller

Story

Read more

Code

Castlevania_Motion_Controller.ino

Arduino
#include <Keyboard.h>
#include <Adafruit_CircuitPlayground.h>

bool rightButtonPressed;
bool leftButtonPressed = true;
bool hasPauseBeenPressed = false;
bool isWhipping = false;
int pauseBufferCounter = 0;
int lightValue;

float X, Y, Z;

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
  Keyboard.begin();
}

void loop() {
  X = CircuitPlayground.motionX();
  Y = CircuitPlayground.motionY();
  Z = CircuitPlayground.motionZ();

  rightButtonPressed = CircuitPlayground.rightButton();
  leftButtonPressed = CircuitPlayground.leftButton();

  if(rightButtonPressed || leftButtonPressed) {    
    if (hasPauseBeenPressed == false) {
      hasPauseBeenPressed = true;
      InputKey('s', false);
      Serial.println("Pause pressed!");
      Keyboard.releaseAll();
    }
  }
  else if (hasPauseBeenPressed == true) {
    pauseBufferCounter++;    
    Serial.print("Pause Buffer: ");
    Serial.println(pauseBufferCounter);
    if (pauseBufferCounter == 42) {
      hasPauseBeenPressed = false;
      pauseBufferCounter = 0;      
    }
  }
  
  if (X <= -18.0) {
    isWhipping = true;
    CircuitPlayground.playTone(450, 20);
  }
  
  if (WithinMarginOfError(X, -2, 3) && WithinMarginOfError (Y, 6.5, 2.8)){
    if (Z > 4) {
      Keyboard.release(KEY_RIGHT_ARROW);
      InputKey(KEY_LEFT_ARROW, true);
    }
    else if (Z < -2) {
      Keyboard.release(KEY_LEFT_ARROW);
      InputKey(KEY_RIGHT_ARROW, true);
    }
  }

  if (WithinMarginOfError(Y, 9.8, 1)){
    lightValue = CircuitPlayground.lightSensor();
    if(lightValue == 0) {
      InputKey(KEY_UP_ARROW, true);
    } 
  }
  else {
    Keyboard.release(KEY_UP_ARROW);
  }

  if (WithinMarginOfError(Y, -9.8, 1)){
    Keyboard.release('d');
    isWhipping = false;

    lightValue = CircuitPlayground.lightSensor();
    if(lightValue == 0) {
      InputKey(KEY_DOWN_ARROW, true);
    } 
  }
  else {
    Keyboard.release(KEY_DOWN_ARROW);
  }

  if (Y > 14) {
    InputKey('f', false);
    Serial.println("Jump pressed!");
    Keyboard.release('f');
  }

  if (isWhipping) {
    InputKey('d', false);
  }
  
  //PrintAcceleration();
}

void PrintAcceleration() {
  Serial.print("X: ");
  Serial.print(X);
  Serial.print("  Y: ");
  Serial.print(Y);
  Serial.print("  Z: ");
  Serial.println(Z);
  delay(200);
}

bool WithinMarginOfError(float value, float target, float margin) {
  float floor = target - margin;
  float ceiling = target + margin;
  if (value < ceiling && value > floor) {
    return true;
  }
  else {
    return false;
  }
}

void InputKey (char character, bool press) {
  int numberOfInputs = 21;
  if (press) {
      Keyboard.press(character);
  }
  else {
    for(int i = 0; i < numberOfInputs; i++){
    Keyboard.write(character);
    }
  }
}

Credits

TriOmega

TriOmega

4 projects • 0 followers

Comments