Minecraft Controller
My controller is designed for the game Minecraft. It is in the shape of a pickaxe for thematic purposes.
The pickaxe is the main tool you will use in Minecraft, as it involves mining. I decided to make the controller
a diamond pickaxe. To input your button presses, you must tap the head of the pickaxe on the table while pressing
a button. This is to signify doing an action in the game, similar to doing a similar action in real life. It also has
a joystick on the back to allow movement. The feedback on the controller is when you strike the table to cause
an action. The pickaxe has an end that naturally draws players to use it like a pickaxe. Tilting the head of the
pickaxe in any direction will cause the player to look around the screen. To achieve this, we used the
accelerometer in the Circuit Playground Express. This is intuitive and adds more action to the controller than
a mouse would. I wanted to make every button and analog fit on the handle of the pickaxe to be easy to reach
everything without adjusting, and while moving. I also planned to put lights on the pickaxe to add more feedback.
I may do this in the future.
Photo:
Code:
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <Keyboard.h>
#include <Mouse.h>
int i = 0;
//Button Prompts
const int ButtonPin1 = A1;
const int ButtonPin2 = A2;
const int ButtonPin3 = A3;
const int ButtonPin4 = A6;
//Joystick Variables
const int joyX = A5;
const int joyY = A4;
int center = 400;
int deadzoneJoy = 150;
int x = 512;
int y = 512;
//Mouse Variables
float deadzone = 3;
float sensitivity = 1;
float smoothX = 0;
float smoothY = 0;
float alpha = 0.2; // smoothing factor
//Sets up button pins, analog, and mouse and keyboard support
void setup() {
Serial.begin(9600);
CircuitPlayground.begin();
pinMode(ButtonPin1, INPUT);
pinMode(ButtonPin2, INPUT);
pinMode(ButtonPin3, INPUT);
pinMode(ButtonPin4, INPUT);
pinMode(joyX, INPUT);
pinMode(joyY, INPUT);
randomSeed(micros());
Mouse.begin();
//Keyboard.begin();
delay(2000);
}
void loop() {
//Calls Joystick Function
JoyStickControls();
//Mouse Control
float rawX = CircuitPlayground.motionX();
float rawY = CircuitPlayground.motionY();
// smoothing
smoothX = alpha * rawX + (1 - alpha) * smoothX;
smoothY = alpha * rawY + (1 - alpha) * smoothY;
int moveX = 0;
int moveY = 0;
//if smoothX is more than the deadzone or less than the negative of the deadzone, make moveX = smoothX and adjust sensetivity
if (abs(smoothX) > deadzone) {
moveX = (int)(smoothX * sensitivity);
}
if (abs(smoothY) > deadzone) {
moveY = (int)(smoothY * sensitivity);
}
//moves the mouse depending on which way the pickaxe is pointed using the accelorometer
if (moveY > -5) {
Mouse.move(moveX, -moveY, 0);
}
//if the fsr is pushed, then allow the buttons to be used
if (digitalRead(ButtonPin4)) {
float i = digitalRead(ButtonPin3);
Serial.println(i);
//button 1 holds down the mouse click and lets go when not pressed
if (digitalRead(ButtonPin1)) {
Serial.println("button 1 pressed");
//Keyboard.write('w');
Mouse.press();
//delay(50);
} else if (!digitalRead(ButtonPin1)) {
Mouse.release();
}
//button 2 presses e once
if (digitalRead(ButtonPin2)) {
Serial.println("button 2 pressed");
Keyboard.write('e');
delay(200);
}
//button 3 holds down the mouse right click and lets go when not pressed
if (digitalRead(ButtonPin3)) {
Mouse.press(MOUSE_RIGHT);
} else if (!digitalRead(ButtonPin3)) {
Mouse.release(MOUSE_RIGHT);
}
}
}
//Joystick Logic
void JoyStickControls() {
y = analogRead(joyX);
x = analogRead(joyY);
//joystick Y logic
if (y < center - deadzoneJoy) {
Keyboard.press('w');
} else if (y > center + deadzoneJoy) {
Keyboard.press('s');
} else if (y > center - deadzoneJoy && y < center + deadzoneJoy) {
Keyboard.release('s');
Keyboard.release('w');
}
//joystick x logic
if (x < center - deadzoneJoy) {
Keyboard.press('d');
} else if (x > center + deadzoneJoy) {
Keyboard.press('a');
} else if (x > center - deadzoneJoy && x < center + deadzoneJoy) {
Keyboard.release('a');
Keyboard.release('d');
}
Serial.print("X: ");
Serial.print(x);
Serial.print(" | Y: ");
Serial.println(y);
}
Schematic:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.