Why does the code work this way?

.......... Uh oh, deleted the whole first post. Now I must fear the wrath of PaulS


Moderator edit: Code replaced from my copy. Thread locked.

#include <SNESpaduino.h>

int isChangeMode=0; //Used to wait until user stops holding mode change buttons before continuing.
int currentMode=0; //Mode 0 is KEYBOARD mode, Mode 1 is JOYPAD mode.
SNESpaduino pad(2, 3, 4);
uint16_t btns;
JoyState_t joySt;

/* SETUP */
void setup()
{  
  Keyboard.begin();

  joySt.xAxis = 127;
  joySt.yAxis = 127;
  joySt.buttons = 0;
}




void releaseKey()
{
  joySt.buttons = joySt.buttons & 254;
  joySt.buttons = joySt.buttons & 253;
  joySt.buttons = joySt.buttons & 252;  
  joySt.buttons = joySt.buttons & 251;

  joySt.buttons = joySt.buttons & 250;
  joySt.buttons = joySt.buttons & 249;
  joySt.buttons = joySt.buttons & 255;
  joySt.buttons = joySt.buttons & 256;

  joySt.yAxis = 127;
  joySt.xAxis = 127;
}



void loop()
{

  btns = pad.getButtons();

  if ((btns & BTN_UP) && (btns & BTN_SELECT)) { //If the change-mode key combo is pressed
    if(isChangeMode==0){
      if(currentMode==0) {
        Keyboard.releaseAll();
        currentMode=1;
        isChangeMode=1;
      }
      else {
        Keyboard.releaseAll();
        currentMode=0;
        isChangeMode=1;
      }
    }
    isChangeMode=1;
  }
  else {
    isChangeMode=0;
  }

































  //UP
  if(btns & BTN_UP){
    if(currentMode==0)
    {
      Keyboard.press(KEY_UP_ARROW);
    }
    else 
    {
      joySt.yAxis = 0;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release(KEY_UP_ARROW);
    }
  }


  //DOWN
  if(btns & BTN_DOWN){
    if(currentMode==0)
    {
      Keyboard.press(KEY_DOWN_ARROW);
    }
    else 
    {
      joySt.yAxis = 255;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release(KEY_DOWN_ARROW);
    }
  }  


  //LEFT
  if(btns & BTN_LEFT){
    if(currentMode==0)
    {
      Keyboard.press(KEY_LEFT_ARROW);
    }
    else 
    {
      joySt.xAxis = 0;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release(KEY_LEFT_ARROW);
    }
  }   


  //RIGHT
  if(btns & BTN_RIGHT){
    if(currentMode==0)
    {
      Keyboard.press(KEY_RIGHT_ARROW);
    }
    else 
    {
      joySt.xAxis = 255;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release(KEY_RIGHT_ARROW);
    }
  }


  //SELECT
  if(btns & BTN_SELECT){
    if(currentMode==0)
    {
      Keyboard.press(',');
    }
    else 
    {
      joySt.buttons = joySt.buttons | 1;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release(',');
    }
  }


  //START
  if(btns & BTN_START){
    if(currentMode==0)
    {
      Keyboard.press('.');
    }
    else 
    {
      joySt.buttons = joySt.buttons | 2;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release('.');
    }
  }


  //A
  if(btns & BTN_A){
    if(currentMode==0)
    {
      Keyboard.press('x');
    }
    else 
    {
      joySt.buttons = joySt.buttons | 4;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release('x');
    }
  }


  //B
  if(btns & BTN_B){
    if(currentMode==0)
    {
      Keyboard.press('z');
    }
    else 
    {
      joySt.buttons = joySt.buttons | 8;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release('z');
    }
  }


  //X
  if(btns & BTN_X){
    if(currentMode==0)
    {
      Keyboard.press('v');
    }
    else 
    {
      joySt.buttons = joySt.buttons | 16;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release('v');
    }
  }


  //Y
  if(btns & BTN_Y){
    if(currentMode==0)
    {
      Keyboard.press('c');
    }
    else 
    {
      joySt.buttons = joySt.buttons | 32;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release('c');
    }
  }


  //L
  if(btns & BTN_L){
    if(currentMode==0)
    {
      Keyboard.press('l');
    }
    else 
    {
      joySt.buttons = joySt.buttons | 64;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release('l');
    }
  }


  //R
  if(btns & BTN_R){
    if(currentMode==0)
    {
      Keyboard.press('r');
    }
    else 
    {
      joySt.buttons = joySt.buttons | 128;
    }
  }
  else{
    if(currentMode==0)
    {
      Keyboard.release('r');
    }
  }





  //Key combinations

  if (btns & BTN_SELECT) 
  {

    if (btns & BTN_START)
    {
      Keyboard.press(KEY_LEFT_GUI);
      Keyboard.press('r');
      Keyboard.release('r');
      Keyboard.release(KEY_LEFT_GUI);
      delay(500);
      Keyboard.print("W:");
      Keyboard.write(47);
      Keyboard.print("Arcade");
      Keyboard.write(47);
      Keyboard.print("Program.exe");
      delay(1500);
      Keyboard.write(179);
      Keyboard.write(32);

    }

    else 
    {
      Keyboard.release('r');
      Keyboard.release(KEY_LEFT_GUI);
    }

    if (btns & BTN_SELECT) 
    {

      if (btns & BTN_L)
      {
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press(KEY_F4);
      }

      else 
      {
        Keyboard.release(KEY_F4);
        Keyboard.release(KEY_LEFT_ALT);
      }     
    }

    if (btns & BTN_SELECT) 
    {

      if (btns & BTN_R)
      {
        Keyboard.press(KEY_ESC);

      }

      else 
      {
        Keyboard.release(KEY_ESC);

      }     
    }  



  }



  // Send joystick state
  Joystick.setState(&joySt);

  //Release Buttons
  releaseKey();
}

Anyone that can tell me why this would happen?

You wrote the code wrong.

You appear to have given us slightly less information than required.

What info do you need?

The basic problem is that you know how your code is supposed to work and we don't. Trying to reverse-engineer code is a very time-consuming process.

You need to give us a plain-language explanation of what each piece of code is supposed to do. And often writing that out helps to show where there are problems.

You have an enormous amount of unnecessary repetition in your code which doesn't help in trying to figure it out.

...R

I got eye-ache reading your code, but I suspect if you formatted it correctly, it might show up your problem - use the auto-format tool in the IDE.

Print the value of btns after

btns = pad.getButtons();

Do you see the values expected when you press buttons ?

This doesn't make any sense at all:

  joySt.buttons = joySt.buttons & 254;
  joySt.buttons = joySt.buttons & 253;
  joySt.buttons = joySt.buttons & 252;  
  joySt.buttons = joySt.buttons & 251;

  joySt.buttons = joySt.buttons & 250;
  joySt.buttons = joySt.buttons & 249;
  joySt.buttons = joySt.buttons & 255;
  joySt.buttons = joySt.buttons & 256;

Try this test code:

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  unsigned int foo = 0xFFFF;
  
  foo = foo & 254;
  foo = foo & 253;
  foo = foo & 252;  
  foo = foo & 251;

  foo = foo & 250;
  foo = foo & 249;
  foo = foo & 255;
  foo = foo & 256;

  Serial.println (foo, HEX);
  }  // end of setup

void loop () { }

Whatever value foo starts with, it will end up being zero. Ditto for joySt.buttons.

I also made it where combination of two of the buttons would output the path to a file.

When I press the button combination it does indeed output the path. However it leaves out any keys that are assigned to the individual buttons. Anyone that can tell me why this would happen?

I don't see any files or paths being output in your code.

The Joy code is there to define the joypad buttons. It works with a modified USBAPI and HID file to give the Leonardo USB HID Joystick support.

The file path it is sending starts right after // Key combination. I have it press the Win key then r to bring up the run box and then input the path.

I am updating the code more with some comments. The joystick code works perfectly fine, the keyboard code works, just trying to figure out why it won't let me send keypresses that are assigned to the buttons.

Ehhh I am a retard. I actually just discovered it as I was looking over the code to hopefully make it easier for you all to read. Turns out using the start button(assigned the . key) as part of the combo doesn't work to well, can't quite press . for the file path when it is already pressed by the start button code.

I would still love to hear some input on better ways to write this.

I just updated my first post.

Don't do that again. You make everyone that comments on errors in the first post look like an idiot, which pisses some people off. I know that because I'm one of them.

Provide more information in replies, but do NOT modify your initial post.

Jeremy0203:
I just updated my first post.

And yet it still has crazy quantities of whitespace in it.

What PaulS said, and then some.

PaulS:
Don't do that again. You make everyone that comments on errors in the first post look like an idiot, which pisses some people off. I know that because I'm one of them.

Provide more information in replies, but do NOT modify your initial post.

  • I will do it if I want
  • No one made any comments on errors
  • I am glad you are pissed off, judging by your comment in this thread and by looking at a couple pages of your previous posts it seems that is the only reason you come around here. You seem to get off on trying to belittle people in their threads. So I don't see why you are pissed anyways since I so gladly started this thread which gave you such an opportunity to do so.
  • Plus, if a post/thread makes you so Hulk angry/pissed then please move along and don't post in it.
  • I will do it if I want

Fine. I'll remember YOU! And not fondly.

Thanks, I will remember you as well.

Close thread, situation resolved.

Jeremy0203:
.......... Uh oh, deleted the whole first post. Now I must fear the wrath of PaulS

Well, now I'm pissed off. This is discourteous behaviour. Continue with it and you can find another forum.

How to use this forum

I went to quite a bit of effort to analyze your code, find what seemed to be a problem, type it up and post it. And now my reply is meaningless. Don't do it again, before I reach for the "ban user" link.

I am glad you are pissed off, judging by your comment in this thread and by looking at a couple pages of your previous posts it seems that is the only reason you come around here.

No, PaulS helps people. A lot of them.