The game I chose to make a controller for is the iconic arcade game, Galaga. The controller will be in the shape of the ship that you play as, with a circular groove cut out from the underside so that the Circuit Playground Bluefruit can be nested inside. I feel like this design will be a good fit as it will feel like you are physically moving the ship. The controller will work by using the accelerometer, where tilting left and right will make you strafe left or right in the game and tilting back towards the player will shoot. I also binded the onboard buttons of the CPB to enter so that you can start and pause the game.
Questions for Review:
1: Are there any inputs that you think would be more intuitive to use for this design?
2: Are there anyways I could improve the code to have this function better?
#include "Adafruit_TinyUSB.h"
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
//Author: Hiram Sun
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_KEYBOARD()
};
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);
//https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h
const int debounce = 25;
void setup() {
// put your setup code here, to run once:
CircuitPlayground.begin();
pinMode(CPLAY_LEFTBUTTON, INPUT_PULLDOWN); //button A
pinMode(CPLAY_RIGHTBUTTON, INPUT_PULLDOWN); //button B
usb_hid.begin();
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
uint8_t const report_id = 0;
uint8_t const modifier = 0;
uint8_t keycode[6] = { 0 };
//Variables for Smoothing
int numReads = 5;
float xSum = 0;
float ySum = 0;
//Get Accelerometer Orientation and smooth the value
for (int i = 0; i < numReads; i++){
xSum += CircuitPlayground.motionX();
ySum += CircuitPlayground.motionY();
delay(1);
}
float x = avgValue(xSum, numReads);
float y = avgValue(ySum, numReads);
float rad = atan2(y, x);
//Buttons for starting or pausing game
if(digitalRead(CPLAY_LEFTBUTTON) || digitalRead(CPLAY_RIGHTBUTTON)){
keycode[0] = HID_KEY_ENTER;
usb_hid.keyboardReport(report_id, modifier, keycode);
delay(200);
usb_hid.keyboardRelease(0);
}
//Strafe Left
if(rad < -2.05 || rad > 2.85){
keycode[0] = HID_KEY_A;
usb_hid.keyboardReport(report_id, modifier, keycode);
delay(debounce);
usb_hid.keyboardRelease(0);
}
//Strafe Right
if(rad > -1.05 && rad < 0.3){
keycode[0] = HID_KEY_D;
usb_hid.keyboardReport(report_id, modifier, keycode);
delay(debounce);
usb_hid.keyboardRelease(0);
}
//Shoot
if(rad < -0.55 && rad > -2.55){
keycode[0] = HID_KEY_H;
usb_hid.keyboardReport(report_id, modifier, keycode);
delay(debounce);
usb_hid.keyboardRelease(0);
}
/*
Serial.println(rad);
delay(100);
*/
}
//average the inputted value
float avgValue(float sumValue, int divisor){
float result = sumValue / divisor;
return result;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.