18 April 2022

My Controller(Kyle Spence)


 For my controller I went with a simple motion control setup for Super Mario Brothers classic. In this game the controls are WASD and with the tilt of the controller the titular character moves to the right or left. If the CPE is moved Up or Down, Mario jumps and squats. I went with motion controls to simulate the innovation of the Wii remote that came decades later from Nintendo. I wanted to give a level of immersion for the player by simply taking away the need for someone to constantly press the arrow keys on the original NES controller, I thought that by giving motion the player would enjoy moving the character and maybe get rid of some frustrations from the physical stress that may be caused.  I simply used a familiar box to house all the electronics within them even though there are better housing equipment. There’s even a potentiometer that controls Mario’s sprint/fire ball off to the side. So nothing really too complicated, I wanted to simply use a twist because while the player is continuously moving the controller they also can manage the need to attack the opposing enemies. I had trouble figuring out the  bluetooth within the CPE with this particular project, and the design was also last minute as well. The electronics also were kinda of complicated because I was tossing so many design back and forth, plus getting the materials was also kind of difficult. The signature JUMP that Mario has was interesting because I had to constantly figure out what the delay and the orientation of the CPE needed to be to successfully do different jump heights. 











//Libraries
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <bluefruit.h>

BLEDis bledis;
BLEHidAdafruit blehid;
int debounce = 100;



void setup() {
  // setup code here, to run once:
  //Init Library
  CircuitPlayground.begin();
  Serial.begin(9600);
  delay(1000);
  Serial.println("hello");
  Bluefruit.begin();
  Bluefruit.setTxPower(4);
  // Configure and Start Device info Service
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather 52");
  bledis.begin();
  // Start HID
  blehid.begin();
  //Start advertising device function
  Serial.println("pre start");
  startAdv();
  Serial.println("setup complete");

}
//CPE setup

void loop() {
  if (Bluefruit.connected() && digitalRead(7))
{
  // Accelerometer readings
  float x = CircuitPlayground.motionX();
  float y = CircuitPlayground.motionY();
  float z = CircuitPlayground.motionZ();
 int p = analogRead(A6);//Might delete
 //Check values/Check readings
  Serial.print("Bluefruit: ");
  Serial.print(Bluefruit.connected());
   Serial.print(digitalRead(7));
Serial.print(" Button: ");
   Serial.print(y);
   Serial.print("\t"); 
   Serial.print(x);
 Serial.print("\t");
 Serial.println();

   //Press W to JUMP, values from accelerometer mess with # for neutral state
 if ( y > -6) {
   blehid.keyPress('w');
   blehid.keyRelease();
   delay(5);
   //check readings
   Serial.println("In Y if");

  }
  //Press D to move RIGHT mess with # for neutral state
  if (x > 2 && y > -10 && y < -8 && z > -2 && z < 2) {
    blehid.keyPress('d'); 
     blehid.keyRelease();
    delay(5);
    //check readings
    }
    //Press A to move LEFT
    if (x > -2 && x < 2 && y > -10 && y < -8 && z < -2) {
    blehid.keyPress('a');
     blehid.keyRelease();
     //check readings
    delay(5);
    //
    }
    //potentiometer reading
    if(analogRead(A4) > 500) {
    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)
    keycode[0] = HID_KEY_ENTER; // HID_KEY_ARROW_LEFT this is the one being changed!
    blehid.keyboardReport(report_id, modifier, keycode);
    //check readings
 
  //if(analogRead(A4) > 500) { //then character will sprint/fireball

 
  }
 }
 else {Serial.println("no BLE");
 }
}
//Notes specific keys(hold) go to delay

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.