PiBoy Build Log 2. Arduino Pro Micro Analog Stick post
Not typical of a 'PiBoy' setup but I wanted some sort of analog stick action. Still WIP post as I'm undecided if I wanna rock one stick or two.
First step is to completely destroy your gameboy shell by melting holes in it with a soldering iron cos you don't really know any better (you do)
after then wire up the analogue to the Pro Micro - It's pretty straight forward, as per this diagram from adafruit. Wire Ground to ground, VCC to VCC and pick two Analog pins for X+Y pins, I used A2, A3.
After doing this I added support to my arduino script to support the stick and map it to 1,2,3,4 for up, down, left right
#include <Keyboard.h>
// Number of buttons to handle
const int buttonsCount = 12;
// Arduino PINs to use
const int pins[buttonsCount] = {
2,
3,
4,
5,
6,
7,
8,
9,
10,
16,
15,
14
};
// Keys to send (order has to match the pins array)
const byte keys[buttonsCount] = {
KEY_UP_ARROW, //2
KEY_DOWN_ARROW, //3
KEY_LEFT_ARROW, //4
KEY_RIGHT_ARROW, //5
KEY_F2, //6
KEY_F1, //7
'a', //8
'b', //9
'x', //10
'y', //16
'r', //15
'l' //14
};
//analog pins
const int analogPins[2][2] = {
{3, 2}, //right analog XY
{0, 1} //left analog XY
};
// Keys to send (for analogs)
const byte analogKeys[2][4] = {
{'1', '2', '3', '4'}, //right analog up, down, left, right
{'5', '6', '7', '8'} //left analog up, down, left, right
};
//analog thresholds
int thresholdX = 500;
int thresholdY = 500;
int tolerance = 150;
// Debounce delay
const long debounceDelay = 50;
bool status[buttonsCount] = {LOW};
long lastDebounces[buttonsCount] = {0};
void setup() {
for (int i = 0; i < buttonsCount; ++i) {
pinMode(pins[i], INPUT);
digitalWrite(pins[i], HIGH);
}
Serial.begin(9600);
Keyboard.begin();
}
void loop() {
for (int i = 0; i < buttonsCount; ++i) {
const int pinStatus = digitalRead(pins[i]);
if (pinStatus != status[i] && millis() - debounceDelay > lastDebounces[i]) {
status[i] = pinStatus;
if (pinStatus == LOW) {
Keyboard.press(keys[i]);
} else {
Keyboard.release(keys[i]);
}
lastDebounces[i] = millis();
}
}
//right analog X
if (analogRead(analogPins[0][0]) < (thresholdX - tolerance)){
Keyboard.release(analogKeys[0][2]);
Keyboard.press(analogKeys[0][3]); //right
}else if (analogRead(analogPins[0][0]) > (thresholdX + tolerance)){
Keyboard.press(analogKeys[0][2]); //left
Keyboard.release(analogKeys[0][3]);
}else{
Keyboard.release(analogKeys[0][2]);
Keyboard.release(analogKeys[0][3]);
}
//right analog Y
if (analogRead(analogPins[0][1]) < (thresholdY - tolerance)){
Keyboard.press(analogKeys[0][1]); //down
Keyboard.release(analogKeys[0][0]);
}else if (analogRead(analogPins[0][1]) > (thresholdY + tolerance)){
Keyboard.press(analogKeys[0][0]); //up
Keyboard.release(analogKeys[0][1]);
}else{
Keyboard.release(analogKeys[0][0]);
Keyboard.release(analogKeys[0][1]);
}
if (1==2){ //disabled
//left analog X
if (analogRead(analogPins[1][0]) < (thresholdX - tolerance)){
Keyboard.release(analogKeys[1][2]);
Keyboard.press(analogKeys[1][3]); //right
}else if (analogRead(analogPins[1][0]) > (thresholdX + tolerance)){
Keyboard.press(analogKeys[1][2]); //left
Keyboard.release(analogKeys[1][3]);
}else{
Keyboard.release(analogKeys[1][2]);
Keyboard.release(analogKeys[1][3]);
}
//left analog Y
if (analogRead(analogPins[1][1]) < (thresholdY - tolerance)){
Keyboard.press(analogKeys[1][1]); //down
Keyboard.release(analogKeys[1][0]);
}else if (analogRead(analogPins[1][1]) > (thresholdY + tolerance)){
Keyboard.press(analogKeys[1][0]); //up
Keyboard.release(analogKeys[1][1]);
}else{
Keyboard.release(analogKeys[1][0]);
Keyboard.release(analogKeys[1][1]);
}
}
}
Categories: ancient