Gamepad with PSP Stick on Arduino Script

General GBZ-related chat goes here. Share ideas, tips and tricks, or ask questions that don't fit into the hardware/software help forums.
Post Reply
Laen
Posts: 5
Joined: Sun Apr 14, 2019 9:31 am

Gamepad with PSP Stick on Arduino Script

Post by Laen » Sun Apr 14, 2019 11:39 am

Hi want to Build a GB Zero with a Raspberry Pi.
How i build an solder it is Clear to me.

But the Arduino Script thing is not my Thing.
I want to Use the Arduino Leonardo as A USB Gamepad.
When i understand Correct it must connect with the usb Cable on the Pie USB.

All Buttons and the PSP Analog Stick musst Solder on die Arduino.

But the Script is my Problem.

I found This one in the Forum
SpoilerShow

Code: Select all

#include <Gamepad.h>
Gamepad gb;

#define USE_JOYSTICK true
#define ANA_Y A2
#define ANA_X A3

// EBAY PRO MINI CLONE BLUE PCB CONFIG
#define BTN_UP 2
#define BTN_DOWN 3
#define BTN_LEFT 4
#define BTN_RIGHT 5
#define BTN_A 6
#define BTN_B 7
#define BTN_X 8
#define BTN_Y 9
#define BTN_START 0
#define BTN_SELECT 1
#define BTN_L1 10
#define BTN_R1 16
#define BTN_L2 14
#define BTN_R2 15
#define BTN_L3 A0
#define BTN_R3 A1


void setup() {
  Serial.begin(9600);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  pinMode(A0, INPUT_PULLUP);  
  pinMode(A1, INPUT_PULLUP);
  for (int x = 0; x < 13; x++) {
    pinMode(x, INPUT_PULLUP);
  }
  #if USE_JOYSTICK
  pinMode(ANA_Y, INPUT_PULLUP);
  pinMode(ANA_X, INPUT_PULLUP);
  #endif
}


void loop() {
  
    bool downSet  = digitalRead(BTN_DOWN);
    bool upSet    = digitalRead(BTN_UP); // No pun intended
    bool leftSet  = digitalRead(BTN_LEFT);
    bool rightSet = digitalRead(BTN_RIGHT);

    #if USE_JOYSTICK
      /** REPLACE VALUE WITH THE ONES FOR YOUR ANALOG STICK ! **/
      int x = map(analogRead(ANA_X), 263, 919, -127, 127);
      int y = map(analogRead(ANA_Y), 230, 891, -127, 127);
      gb.setLeftXaxis(x);
      gb.setLeftYaxis(y);
    #else
      gb.setLeftYaxis(!/*Don't be*/ upSet ? -127 : !downSet ? 127 : 0); //hahaha chill
      gb.setLeftXaxis(!leftSet ? -127 : !rightSet ? 127 : 0);
    #endif
    gb.setButtonState(0, !digitalRead(BTN_A));
    gb.setButtonState(1, !digitalRead(BTN_B));
    gb.setButtonState(2, !digitalRead(BTN_X));
    gb.setButtonState(3, !digitalRead(BTN_Y));
    gb.setButtonState(4, !digitalRead(BTN_START));
    gb.setButtonState(5, !digitalRead(BTN_SELECT));
    gb.setButtonState(6, !digitalRead(BTN_L1));
    gb.setButtonState(7, !digitalRead(BTN_R1));
    gb.setButtonState(8, !digitalRead(BTN_L2));
    gb.setButtonState(9, !digitalRead(BTN_R2));
    gb.setButtonState(10, !digitalRead(BTN_L3));
    gb.setButtonState(11, !digitalRead(BTN_R3));
    
    #if USE_JOYSTICK
      gb.setButtonState(12, !digitalRead(BTN_DOWN));
      gb.setButtonState(13, !digitalRead(BTN_UP));
      gb.setButtonState(14, !digitalRead(BTN_LEFT));
      gb.setButtonState(15, !digitalRead(BTN_RIGHT));
    #endif

    gb.sendUpdate();
}
Is this Ok or is there A better one?


The One that i found on a German Website gives me an error when i compile it.
SpoilerShow

Code: Select all

/* Teensy mit zwei Modi: Gamepad mit Analogstick und Tastatur mit Maus (PC Modus)
*
* Standardmäßig ist der Gamepad-Modus aktiviert.
* Durch die Tastenkombination: R1, R2, R1, R2, R1 lässt sich zwischen den Modi wechseln
* Im PC-Modus wird der Analogstick als Maus erkannt und die normalen Knöpfe werden als Buchstaben ausgegeben.
* In beiden Modi fungieren L2 und R2 als Escape- und Enter-Taste.
*/
#include
#define NUM_KEYS 15

struct Key {
char keycode;
Bounce* bounce;
};

Key keys[NUM_KEYS];

Key key(char keycode, int pin) {
Key *ret = new Key;
ret->keycode = keycode;
ret->bounce = new Bounce(pin, 10);
pinMode(pin, INPUT_PULLUP);
return *ret;
}

//Keyboard-Tastenbelegung
void setupKeys() {
keys[0] = key('a', 0);
keys[1] = key('w', 1);
keys[2] = key('s', 2);
keys[3] = key('d', 3);
keys[4] = key('m', 4);
keys[5] = key('n', 5);
keys[6] = key('b', 6);
keys[7] = key('v', 7);
keys[8] = key('y', 8);
keys[9] = key('x', 9);
keys[10] = key('q',10);
keys[11] = key('e',11);
keys[12] = key('u',12);
keys[13] = key('l',13);
keys[14] = key('k',14);
}

//L2 und R2
Bounce button9 = Bounce(15, 10);
Bounce button10 = Bounce(16, 10);
boolean pcMode = false;
int pcButtonCount = 0;
const int numButtons = 16; // 16 for Teensy, 32 for Teensy++

void setup() {

//Debug-Nachrichten über Serial.println("Nachricht") verschicken
Serial.begin(9600);

setupKeys();
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
Joystick.useManualSend(true);

for (int i=0; i<numButtons; i++) {
pinMode(i, INPUT_PULLUP);
}
}

byte allButtons[numButtons];
byte prevButtons[numButtons];
int mapX;
int mapY;
int deadzone=60;
void loop() {

//L2 und R2, Aktivierung des PC-/Gamepadmodus
button9.update();
button10.update();

if (button9.fallingEdge()) {
Keyboard.set_key1(KEY_ENTER);
Keyboard.send_now(); // send the button press
Keyboard.set_key1(0);
Keyboard.send_now(); // send the button release

if(pcButtonCount%2!=0){
Serial.println(pcButtonCount);
pcButtonCount=0;
}
if(pcButtonCount%2==0){
Serial.println(pcButtonCount);
pcButtonCount++;
}
if(pcButtonCount==5){
pcMode=!pcMode;
pcButtonCount=0;
}
}
if (button10.fallingEdge()) {
Keyboard.set_key1(KEY_ESC);
Keyboard.send_now(); // send the button press
Keyboard.set_key1(0);
Keyboard.send_now(); // send the button release
if(pcButtonCount%2==0){
Serial.println(pcButtonCount);
pcButtonCount=0;
}
if(pcButtonCount%2!=0){
Serial.println(pcButtonCount);
pcButtonCount++;
}
if(pcButtonCount==5){
pcMode=!pcMode;
pcButtonCount=0;
}
}

// Analog Stick Kalibrierung
// Zu faul meine Kommentare wieder einzudeutschen... :)

//Remap the data range
mapX=map(analogRead(23),235, 1259, 0, 1023); // My analogstick upper bounds where out of range,
mapY=map(analogRead(22),200, 1224, 0, 1023); // so I added 1024 to its minimum as an imaginary maximum.

//Deadzones
if(mapX<512+deadzone&&mapX>512-deadzone)mapX=512;
if(mapY<512+deadzone&&mapY>512-deadzone)mapY=512;
if(mapX<24)mapX=0;
if(mapY<24)mapY=0; //Non-linear correction if(mapX>512){
mapX=(int)(0.006*mapX*mapX-5.8788*mapX+1950); // This is needed since my upper bounds are out of Range. I basically scale all values
if(mapX>1023) mapX=1023; // greater than 512, so the analogstick reaches its full maximum of 1023. The math "adds" a little bit
} // more resolution when the stick is only moved a little.
if(mapY>512){
mapY=(int)(0.0037*mapY*mapY-3.2716*mapY+1225);
if(mapY>1023) mapY=1023;
}

//Send final values
Joystick.X(mapX);
Joystick.Y(mapY);

// PC mode

//Analogstick as Mouse
if(pcMode==true){
float X=(float)mapX;
float Y=(float)mapY;
float dX=6*((X-512)/512); //scale the mousespeed by 6.
float dY=6*((Y-512)/512);
Mouse.move(dX, dY, 0);

for (int i = 0; i < NUM_KEYS; i++) { keys[i].bounce->update();

if (keys[i].bounce->fallingEdge()) {
if(i==13) Mouse.press(MOUSE_RIGHT);
else if(i==14) Mouse.press(MOUSE_LEFT);
else Keyboard.press(keys[i].keycode);
}

else if (keys[i].bounce->risingEdge()) {
if(i==13) Mouse.release(MOUSE_RIGHT);
else if(i==14) Mouse.release(MOUSE_LEFT);
else Keyboard.release(keys[i].keycode);
}
}
}

// Gamepad mode
else{
// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(i)) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons[i] = 0;
}
else {
// when a pin reads low, the button is connecting to ground.
allButtons[i] = 1;
}
Joystick.button(i + 1, allButtons[i]);
}
Joystick.send_now();
}
} 

I hope someon Can help me with it.

User avatar
tinkerBOY
Posts: 710
Joined: Tue May 30, 2017 4:00 am
Has thanked: 294 times
Been thanked: 206 times

Re: Gamepad with PSP Stick on Arduino Script

Post by tinkerBOY » Mon Apr 15, 2019 7:11 am

New! tinkerBOY PowerSwitch v1.0 with Safe Shutdown
Game Boy Zero Controllers available @ tinkerBOY.xyz * Support
tinkerBOY Controller v3.0 - built-in usb controller, usb audio, and usb hub
tinkerBOY Controller v2.0 - GPIO buttons and builtin PWM Audio and Amplifier
tinkerBOY Controller v1.1 - simple pcb button
* DPI Adapter - better display for GBZ
* keyboard converters - IBM XT/AT Soarer's Converter | ADB to USB Converter

Laen
Posts: 5
Joined: Sun Apr 14, 2019 9:31 am

Re: Gamepad with PSP Stick on Arduino Script

Post by Laen » Mon Apr 15, 2019 8:32 am

ok thx i dont knew that site.

I think i must use the code for 6 button when i want L/R1 and L/R2

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest