Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15287

Games controller not working correctly

$
0
0

Hello,

I'm using an atmega32 board as a games controller for a Raspeberry Pi4 running Retopie/MAME games. I've tested the board on my PC and all inputs seem to work fine but when I plug it into the Raspberry Pi it moves the menu left and right and seems to be uncontrollable.

I've tried unplugging all the buttons and the potentiometer(which i'm using for the steering) and it is still moves uncontrollably which leads me to think its bad code somewhere.

I have also tried a different board, different Raspberry Pi and different USB cables.

I'd be most grateful if anyone sees anything odd looking in the code below.

#include <Joystick.h>


#define joyButton1 9
#define joyButton2 8
#define joyButton3 7
#define joyButton4 6
#define joyButton5 5
#define joyButton6 4
#define joyButton7 3
#define joyButton8 2
#define joyButton9 1
#define joyButton10 10


//Setting up Buttons
//Updating a static variable gives greater stability than reading directly from the digital pin.
//Giving Default Values to the Buttons for later use
  int lastButton1State = 0;
  int lastButton2State = 0;
  int lastButton3State = 0;
  int lastButton4State = 0;
  int lastButton5State = 0;
  int lastButton6State = 0;
  int lastButton7State = 0;
  int lastButton8State = 0;
  int lastButton9State = 0;
  int lastButton10State = 0;

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 10, 0,true,true,false,false,false,false,false,false,false,false,false);

const bool initAutoSendState = true;

int xVal = 0;
int potPin = A0;
double xAxis = 0;
float multiplier = 1.4;

void setup() {
    //Initialize Buttons
  //Buttons set up between Digital Pin and Ground, following pin allocations from earlier on
  pinMode(joyButton1, INPUT_PULLUP);
  pinMode(joyButton2, INPUT_PULLUP);
  pinMode(joyButton3, INPUT_PULLUP);
  pinMode(joyButton4, INPUT_PULLUP);
  pinMode(joyButton5, INPUT_PULLUP);
  pinMode(joyButton6, INPUT_PULLUP);
  pinMode(joyButton7, INPUT_PULLUP);
  pinMode(joyButton8, INPUT_PULLUP);
  pinMode(joyButton9, INPUT_PULLUP);
  pinMode(joyButton10, INPUT_PULLUP);
  Joystick.setXAxisRange(-127, 127);
  Joystick.setYAxisRange(-127, 127);
  Joystick.begin();
//Serial.begin(9600);
}

void loop() {
   xVal = analogRead(potPin);
   xAxis = ((0.24828934506 * xVal) - 127) * multiplier;
   
   if (xAxis < -127){
    xAxis = -127;
   }
   else if (xAxis > 127){
    xAxis = 127;
   }
   
   Joystick.setXAxis(xAxis);
   Joystick.setYAxis(0);
   Joystick.sendState();

//Button Reading during Runtime
  //Setting Read functions for each button, using a state value for memory. Button 1 will be used as an example for explanation

  //Reading the current Button digital pin to the Current Button State for processing
  int currentButton1State = !digitalRead(joyButton1);
  //If loop - Check that the button has actually changed.
  if (currentButton1State != lastButton1State){
    //If the button has changed, set the specified HID button to the Current Button State
    Joystick.setButton(0, currentButton1State);
    //Update the Stored Button State
    lastButton1State = currentButton1State;
  }
    int currentButton2State = !digitalRead(joyButton2);
  if (currentButton2State != lastButton2State){
    Joystick.setButton(1, currentButton2State);
    lastButton2State = currentButton2State;
  }
    int currentButton3State = !digitalRead(joyButton3);
  if (currentButton3State != lastButton3State){
    Joystick.setButton(2, currentButton3State);
    lastButton3State = currentButton3State;
  }
      int currentButton4State = !digitalRead(joyButton4);
  if (currentButton4State != lastButton4State){
    Joystick.setButton(3, currentButton4State);
    lastButton4State = currentButton4State;
  }
      int currentButton5State = !digitalRead(joyButton5);
  if (currentButton5State != lastButton5State){
    Joystick.setButton(4, currentButton5State);
    lastButton5State = currentButton5State;
  }
      int currentButton6State = !digitalRead(joyButton6);
  if (currentButton6State != lastButton6State){
    Joystick.setButton(5, currentButton6State);
    lastButton6State = currentButton6State;
  }
      int currentButton7State = !digitalRead(joyButton7);
  if (currentButton7State != lastButton7State){
    Joystick.setButton(6, currentButton7State);
    lastButton7State = currentButton7State;
  }
      int currentButton8State = !digitalRead(joyButton8);
  if (currentButton8State != lastButton8State){
    Joystick.setButton(7, currentButton8State);
    lastButton8State = currentButton8State;
  }
      int currentButton9State = !digitalRead(joyButton9);
  if (currentButton9State != lastButton9State){
    Joystick.setButton(8, currentButton9State);
    lastButton9State = currentButton9State;
  }
     int currentButton10State = !digitalRead(joyButton10);
  if (currentButton10State != lastButton10State){
    Joystick.setButton(9, currentButton10State);
    lastButton10State = currentButton10State;
  }
  

//Pole Delay/Debounce
//To reduce unessecary processing, the frequency of the reading loop is delayed. The value(in ms) can be changed to match requirement
delay(10);
}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15287

Trending Articles