Bluetooth Game Controller with Arduino Micro

Hello

I want to make a Game Controller with the Arduino Micro with
12 Buttons
2 Joysticks
HC-05 Bluetooth

I already have the program for the gaming controller with kabel. The only problem is now the Bluetooth part.

My Code is:

#include <Gamepad.h>

int rightXcenter = 510;
int rightYcenter = 510;
int leftXcenter = 510;
int leftYcenter = 510;
double multiplierRX = 0.254; //127 / 500
double multiplierRY = 0.254;
double multiplierLX = 0.254;
double multiplierLY = 0.254;

Gamepad gp;

#define BTBAUD 384000
#define BTRX 2
#define BTTX 3
#include <SoftwareSerial.h>
SoftwareSerial btSerial(BTRX, BTTX);

void setup() {

pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(14, INPUT_PULLUP); //UPPER RIGHT
pinMode(15, INPUT_PULLUP); //UPPER LEFT
pinMode(1, INPUT_PULLUP); //LEFTBUTTON
pinMode(0, INPUT_PULLUP); //RIGHTBUTTON
pinMode(4, INPUT_PULLUP); //UP
pinMode(5, INPUT_PULLUP); //DOWN
pinMode(6, INPUT_PULLUP); //LEFT
pinMode(7, INPUT_PULLUP); //RIGHT
pinMode(8, INPUT_PULLUP); //Y
pinMode(9, INPUT_PULLUP); //X
pinMode(10, INPUT_PULLUP); //A
pinMode(16, INPUT_PULLUP); //B

calibrate();

Serial.begin(38400);
Serial.println(F("Konfigurations Sketch für das HC-05/HC-06 \n\r"));
Serial.println(F("HC-05: \n\r "CR und LN" für das HC-05 einstellen."));
btSerial.begin(BTBAUD);

}

void loop() {
int lx, ly, rx, ry;
lx = analogRead(A3);
ly = analogRead(A2);
rx = analogRead(A1);
ry = analogRead(A0);
//we need to convert a 0-1000 to -127 - 127
lx = floor((lx - leftXcenter) * multiplierLX);
ly = floor((ly - leftYcenter) * multiplierLY);
rx = floor((rx - rightXcenter) * multiplierRX);
ry = floor((ry - rightYcenter) * multiplierRY);
if(lx > 127) lx = 127;
if(ly > 127) ly = 127;
if(rx > 127) rx = 127;
if(ry > 127) ry = 127;
gp.setLeftXaxis(lx);
gp.setRightXaxis(rx);
gp.setLeftYaxis(ly);
gp.setRightYaxis(ry);

int UPLEFT, UPRIGHT, UP, DOWN, LEFT, RIGHT, RIGHTBUTTON, LEFTBUTTON, X, Y, A, B;
UPLEFT = digitalRead(14);
UPRIGHT = digitalRead(15);
RIGHTBUTTON = digitalRead(0);
LEFTBUTTON = digitalRead(1);
UP = digitalRead(4);
DOWN = digitalRead(5);
LEFT = digitalRead(6);
RIGHT = digitalRead(7);
X = digitalRead(8);
Y = digitalRead(9);
A = digitalRead(10);
B = digitalRead(16);

if(UPLEFT == LOW) //
gp.setButtonState(0, true);
else
gp.setButtonState(0, false);

if(UPRIGHT == LOW)
gp.setButtonState(1, true);
else
gp.setButtonState(1, false);

if(UP == LOW)
gp.setButtonState(2, true);
else
gp.setButtonState(2, false);

if(DOWN == LOW)
gp.setButtonState(3, true);
else
gp.setButtonState(3, false);

if(LEFT == LOW)
gp.setButtonState(4, true);
else
gp.setButtonState(4, false);

if(RIGHT == LOW)
gp.setButtonState(5, true);
else
gp.setButtonState(5, false);

if(RIGHTBUTTON == LOW)
gp.setButtonState(6, true);
else
gp.setButtonState(6, false);

if(LEFTBUTTON == LOW)
gp.setButtonState(7, true);
else
gp.setButtonState(7, false);

if(X == LOW)
gp.setButtonState(8, true);
else
gp.setButtonState(8, false);

if(Y == LOW)
gp.setButtonState(9, true);
else
gp.setButtonState(9, false);

if(A == LOW)
gp.setButtonState(10, true);
else
gp.setButtonState(10, false);

if(B == LOW)
gp.setButtonState(11, true);
else
gp.setButtonState(11, false);

delay(20);
}

void calibrate()
{
int lx, ly, rx, ry;
int i = 0;
while(i < 13)
{
lx = analogRead(A3);
ly = analogRead(A2);
rx = analogRead(A1);
ry = analogRead(A0);
bool validLX = lx > (leftXcenter - 20) && lx < (leftXcenter + 20);
bool validLY = ly > (leftYcenter - 20) && ly < (leftYcenter + 20);
bool validRX = rx > (rightXcenter - 20) && rx < (rightXcenter + 20);
bool validRY = ry > (rightYcenter - 20) && ry < (rightYcenter + 20);
if(validLX && validLY && validRX && validRY)
{
i++;
//nothing to do here!
}
else i = 0;
delay(20);
}
leftXcenter = lx;
leftYcenter = ly;
rightXcenter = rx;
rightYcenter = ry;
multiplierLX = (double)127 / (double)lx;
multiplierLY = (double)127 / (double)ly;
multiplierRX = (double)127 / (double)rx;
multiplierRY = (double)127 / (double)ry;

if (btSerial.available()) {
Serial.write(btSerial.read());

}

if (Serial.available()) {
btSerial.write(Serial.read());
}
}

Now is the only problem that the Controller cant speak to the PC.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.