18 April 2021

Friday Night Funkin' Game Controller

 




My controller was designed to play a rythm game known as "Friday Night Funkin'". When I first started thinking of how to code it I thought to myself, "It'd be cool if you could play it with tilt controls." So that's what I aimed to do. I made it so that when you tilted the remote upwards it would input the "W" key which is up in the game, downwards was "S" key which was down in the game, right was the "D" key which is right in the game, and left was the "A" key which was left in the game. After I finished doing that through a lot of trial and error, especially with choosing how long I should delay each input (if I should at all) to hit the note that I had to hold down and then making it reset (aka using the Keyboard.releaseAll() function) when the CPE was being held upright and completely horizontal, took me some time. After I did that I wanted to do more so I said what if I made it so I thought "What if I used a potentiometer to control the volume of the game?" The game uses "+" and "-" keys to control the volume but raising it and lowering it respectfully. I tried to do that but at the end of the day I couldnt get the potentiometer to work so I scrapped the idea. The next thing I thought would be funny was what if when you yell it selects something on the menu (presses the "SPACE" key) after many tries, that space key worked in every other game and program but this one, but I kept it in because I found it neat for other games. The last thing I did, and most inconvenient thing to learn, was programming the light sensor to detect when your finger was on top of it (when the light value turned to 0) and to then press the "ESC" key. I tried to make it so that I could navigate the menus without the keyboard and "ESC" allowed me to go backwards. Turns out the tissue box I used is very dark so that value of 0 is constantly being detected which means "ESC" was being pressed every second, and arduino (that I know of) can't press 2 keys at once, so I couldn't play the game when the sensor detected darkness. To fix this, instead of completely scrapping it, for some reason, I put my phone's flashlight on and put it on top of the CPE which kept the light value higher than 0. To press "ESC" all I had to do was remove the phone's flashlight. At that point I decided that was enough and that's how I made my controller. 

My question would be:
Should I remove the light sensor feature or change it to do something else?

#include <Keyboard.h>
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <math.h>

const int Second = 1000;
const int mSecond = 100;

const int dark = 0;

int light;

float sound;
float loud = 105;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  CircuitPlayground.begin();
  Keyboard.begin();
}

void loop() {
  // put your main code here, to run repeatedly:

  //Get values of accelerometer, assign each value to each arrow key on the keyboard
  
  //data on xAxis
  float xAxis = CircuitPlayground.motionX();
  Serial.print(xAxis);
  Serial.print("\t");

  //data on yAxis
  float yAxis = CircuitPlayground.motionY();
  Serial.print(yAxis);
  Serial.print("\t");


  if (yAxis >= 6) {
    Keyboard.press('s');
    delay(mSecond);
  } else {
    Keyboard.releaseAll();
  }

   if (xAxis >= 6) {
    Keyboard.press('a');
    delay(mSecond);
  } else {
    Keyboard.releaseAll();
  }

   if (yAxis <= -6) {
    Keyboard.press('w');
    delay(mSecond);
  } else {
    Keyboard.releaseAll();
  }

   if (xAxis <= -6) {
    Keyboard.press('d');
    delay(mSecond);
  } else {
    Keyboard.releaseAll();
  }

  if (xAxis > -6 && xAxis < 6 && yAxis > -6 && yAxis < 6) {
    Keyboard.releaseAll();
    delay(mSecond);
  }
  
  
  //Get value of light senor, if light value is darker than a certain value, input "esc" on the keyboard.

  light = CircuitPlayground.lightSensor();
  
  Serial.print("Light Sensor: ");
  Serial.println(light);

    if (light <= dark) {
    Keyboard.press(KEY_ESC);
    Serial.println("esc");
    delay(mSecond);
    Keyboard.release(KEY_ESC);
    delay(Second);
 
  }


  //Get value of sound sensor, whenever the value hits a big number that is equivalent to a scream or loud blow, press space

  sound = CircuitPlayground.mic.soundPressureLevel(10);
  
  Serial.print("Sound Sensor SPL: ");
  Serial.println(sound);

  if (sound >= loud) {
    Keyboard.write(' ');
    Serial.println("SPACE");
    delay(Second);
  }

  
}


No comments:

Post a Comment

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