Is it possible to reconfigure joystick + button code to use pins out of order?

Status
Not open for further replies.
The joystick is operating as a hat switch now but it has some extremely goofy mechanics :). My default it is not neutral but rather up + left diagonal, and the only other states it goes in are diagonals, and I think they are all backwards. So basically if I rotate my joystick in a clockwise circle coming from neutral it goes from up + left counter clockwise only hitting diagonals, really strange stuff. Everything being backwards is probably my fault, I think I wrote down the switch directions incorrectly so I can fix that myself, but I have no idea what to do about the weird neutral and diagonals.
 
Fingers crossed. You pay peanuts you get monkeys

Code:
const int numButtons = 10; 
const uint8_t buttons[numButtons] = { 21, 0, 1, 20, 18, 19, 3, 2, 17, 16 }; // Changes order of buttons to be the specific pins wired
const uint8_t joy[] = {12,13,14,15};
byte allButtons[numButtons];
byte prevButtons[numButtons];
void buttonsrw ();
void hatrw();

void setup() {
  Serial.begin(9600);
  Joystick.useManualSend(true);
  for (int i=0; i<numButtons; i++) {
    pinMode(buttons[i], INPUT_PULLUP);
  }
for (int i=0; i<4; i++) {
    pinMode(joy[i], INPUT_PULLUP);
  }
 // Serial.println("Begin Complete Joystick Test");
}

void loop() 
{
  buttonsrw ();
  hatrw ();
  Joystick.send_now();
  delay(1);
}

void buttonsrw ()
{
for (int i=0; i<numButtons; i++) {
    if (digitalRead(buttons[i])) {
      allButtons[i] = 0;
    } else {
      allButtons[i] = 1;
    }
    Joystick.button(i + 1, allButtons[i]);
}
}

void hatrw()
{
uint8_t read = 0;
for (int i=0; i<4; i++) {
    read |= digitalRead(joy[i])  << i;
  }
switch (read)
{
case 14:
Joystick.hat(90);
break;
case 13:
Joystick.hat(270);
break;
case 11:
Joystick.hat(180);
break;
case 7:
Joystick.hat(0);
break;
case 6:
Joystick.hat(45);
break;
case 10:
Joystick.hat(135);
break;
case 5:
Joystick.hat(315);
break;
case 9:
Joystick.hat(225);
break;
default:
Joystick.hat(-1);
}
}
 
Last edited:
Fingers crossed. You pay peanuts you get monkeys

I'm willing to try and do it myself, I just need something to work off of because I have no idea about most of this code lol.

edit: checked it out, hat switch works almost flawlessly except that for some reason up + left, and down + left diagonals don't work, although all other diagonals work and pure left works. I looked through the code for a missing angle or something that would cause that but I'm not really sure what to look for, I think all 8 angles are there properly.
 
Last edited:
I'll check my maths. probably added up wrong. Good to know that we are almost there.

Edit ... I found a maths error ... try it now.. hopefully that will fix it.. four plus 1 = 5, not 3!!! I'm a bit mystified why down left is not working?? Hopefully the edit I made to the code above should fix it
 
Last edited:
Found the other mistake .... left out break ... edited code above post #53. what a schwizz
 
Everything works flawlessly! Thanks a lot seriously! Would have taken me probably a year to do myself. Now all I have to do is paint it, mount the pcb, do some sanding, and T-mold it, and my project is complete.
 
Status
Not open for further replies.
Back
Top