Last bit of code for button box has got me lost.

I'm having trouble getting the right code to make my buttons work as a game controller. I have a sketch that works as I want lighting LEDs but how to modify it to send the key presses to my PC has got me lost.I have looked at many sketches and tutorials trying to figure this out on my own, I was sure the code below would be the one, as I want generic button presses rather than keystrokes. But I couldn't get it to work.

I am using the Leonardo board. The PC recognises the board in game controllers but no key presses.

joystickbutton code

 {
        int currentButtonState = !digitalRead(index + pinToButtonMap);
        if (currentButtonState != lastButtonState[index])
        {
          Joystick.setButton(index, currentButtonState);
          lastButtonState[index] = currentButtonState;

After I add the code to my sketch I get:

'pintobuttonmap' was not declared in this scope

at this line:

for (int index = 0; index < 4; index++)

If I remove that line I get 'index was not declared in this scope' at this line:

int currentButtonState = !digitalRead(index + pinToButtonMap)

From this full sketch

#include <Joystick.h>
    
    void setup() {
      // Initialize Button Pins
      pinMode(9, INPUT_PULLUP);
      pinMode(10, INPUT_PULLUP);
      pinMode(11, INPUT_PULLUP);
      pinMode(12, INPUT_PULLUP);
    
      // Initialize Joystick Library
      Joystick.begin();
    }
    
    // Constant that maps the phyical pin to the joystick button.
    const int pinToButtonMap = 9;
    
    // Last state of the button
    int lastButtonState[4] = {0,0,0,0};
    
    void loop() {
    
      // Read pin values
      for (int index = 0; index < 4; index++)
      {
        int currentButtonState = !digitalRead(index + pinToButtonMap);
        if (currentButtonState != lastButtonState[index])
        {
          Joystick.setButton(index, currentButtonState);
          lastButtonState[index] = currentButtonState;
        }
      }
    
      delay(50);

My original sketch:

#include <Bounce2.h>
    #include <Keyboard.h>
    #include <Joystick.h>
    
    Joystick_ Joystick;
    
    const int     BUTTON_COUNT = 4;
    const int16_t DEBOUNCE_DELAY = 50;
    const uint8_t buttonPins[BUTTON_COUNT] = {2, 4, 6, 8};
    const uint8_t ledPins[BUTTON_COUNT] = {3, 5, 7, 9};
    
    Bounce buttons[BUTTON_COUNT];
    uint8_t ledStates[BUTTON_COUNT];
    
    void setup() {
        for (int i = 0; i < BUTTON_COUNT; i++) {
            buttons[i].attach(buttonPins[i], INPUT);
            buttons[i].interval(DEBOUNCE_DELAY);
            pinMode(ledPins[i], OUTPUT);
            ledStates[i] = LOW;
            digitalWrite(ledPins[i], ledStates[i]);
        
          Keyboard.begin();
          Joystick.begin();
          
        }
    }
    
    void loop() {
    
        // Update the debouncers.
        for (int i = 0; i < BUTTON_COUNT; i++)
            buttons[i].update();
    
        // When the first button is pressed:
        if (buttons[0].rose()) {
    
            // Light up the first LED.
            digitalWrite(ledPins[0], HIGH);
    
            // and turn off all the others.
            for (int i = 1; i < BUTTON_COUNT; i++) {
                ledStates[i] = LOW;
                digitalWrite(ledPins[i], ledStates[i]);
            }
        }
    
        // When the first button is released:
        if (buttons[0].fell()) {
    
            // turn off the first LED.
            digitalWrite(ledPins[0], LOW);
        }
    
        // When any other button is pressed:
        for (int i = 1; i < BUTTON_COUNT; i++) if (buttons[i].rose()) {
    
            // Toggle its state.
            ledStates[i] = !ledStates[i];
            digitalWrite(ledPins[i], ledStates[i]);
        }
    }

My tweaked sketch

#include <Bounce2.h>
    #include <Keyboard.h>
    #include <Joystick.h>
    
    // Create Joystick
    Joystick_ Joystick;
    
    const int     BUTTON_COUNT = 4;
    const int16_t DEBOUNCE_DELAY = 50;
    const uint8_t buttonPins[BUTTON_COUNT] = {2, 4, 6, 8};
    const uint8_t ledPins[BUTTON_COUNT] = {3, 5, 7, 9};
    
    Bounce buttons[BUTTON_COUNT];
    uint8_t ledStates[BUTTON_COUNT];
    
    void setup() {
        for (int i = 0; i < BUTTON_COUNT; i++) {
            buttons[i].attach(buttonPins[i], INPUT);
            buttons[i].interval(DEBOUNCE_DELAY);
            pinMode(ledPins[i], OUTPUT);
            ledStates[i] = LOW;
            digitalWrite(ledPins[i], ledStates[i]);
        
          Keyboard.begin();
          Joystick.begin();
          
        }
    }
    
    void loop() {
    
        // Update the debouncers.
        for (int i = 0; i < BUTTON_COUNT; i++)
            buttons[i].update();
    
        // When the first button is pressed:
        if (buttons[0].rose()) {
    
            // Light up the first LED.
            digitalWrite(ledPins[0], HIGH);
    
            // and turn off all the others.
            for (int i = 1; i < BUTTON_COUNT; i++) {
                ledStates[i] = LOW;
                digitalWrite(ledPins[i], ledStates[i]);
            }
        }
    
        // When the first button is released:
        if (buttons[0].fell()) {
    
            // turn off the first LED.
            digitalWrite(ledPins[0], LOW);
        }
    
        // When any other button is pressed:
        for (int i = 1; i < BUTTON_COUNT; i++) if (buttons[i].rose()) {
    
            // Toggle its state.
            ledStates[i] = !ledStates[i];
            digitalWrite(ledPins[i], ledStates[i]);
        
          // Read pin values
      for (int index = 0; index < 4; index++)
      {
        int currentButtonState = !digitalRead(index + pinToButtonMap);
        if (currentButtonState != lastButtonState[index])
        {
          Joystick.setButton(index, currentButtonState);
          lastButtonState[index] = currentButtonState;
        }
          
          
          
        }
    }

But I couldn't get it to work.

How very informative!

Please read and follow the helpful hints in the "How to use this forum" post. Or, consider having someone write code for you -- post in the Gigs & Collaborations section and be prepared to pay.

"I am trying, but it's a whole new language."
26JUL18
Programming Questions / Re: Leonardo Not Showing As Controller

"Here's the code I'm using."
24JUL18
Installation & Troubleshooting / Arduino Leonard problems

"It's for a latching push button"
23JUL18
Programming Questions / Hackster code giving error

"I'd basically like to replace the button and LED in the schematic with the illuminated button on the right."
20JUL18
General Electronics / Help with illuminated push button wiring

"Button1 is the problem here."
18JUL18
Programming Questions / Why does it takes several presses for my LED to light?

"I had the LEDs 2,3,4 turning on and off"
18JUL18
Programming Questions / Re: Help with simple problems please.

"I am getting close to what I want but I am a beginner and my brain is starting to melt. I need buttons 2,3,4 to light up LEDs"
16JUL18
Programming Questions / Help with sketch

"I'd like to make a button box for gaming using an arduino UNO or similar with momentary buttons and have the buttons light LEDs"
16JUN18
Project Guidance / Advice on LED Button Box feasibility for noob

"I tried the original sketch from the Arduino tutorial page, but added four more LEDs on pins on 8 9 10 11"
21MAY18
LEDs and Multiplexing / Help with sketch please

"Thanks, but I probably told you what a noob I am."
27JAN18
Installation & Troubleshooting / Re: Can I change the name on the board when in appears Game Controller control panel

"I want to control by IR 2 mid size servos and also power some led strips"
19NOV17
Motors, Mechanics, and Power / OK to power my Nano project with a 7.4v lipo?

"Thanks for the tip, I'll give that a try tomorrow."
06JUN18
Installation & Troubleshooting / Re: Help with pro mini

========================================

I'm amazed that after more than two years of cross posting and re-posting you still haven't got those LEDs working. I guess your on-going claim to be a Noob is justified.

You know, with all the effort you put in to that post, you probably could have solved my problem. And the LEDs are working perfectly, it's the buttons. :grin:

From Urban Dictionary:

troll

Noun
"A member of an internet forum who continually harangues and harasses others. Someone with nothing worthwhile to add to a certain conversation, but rather continually threadjacks or changes the subject".

DKWatson:
"I am trying, but it's a whole new language."
26JUL18
Programming Questions / Re: Leonardo Not Showing As Controller

"Here's the code I'm using."
24JUL18
Installation & Troubleshooting / Arduino Leonard problems

"It's for a latching push button"
23JUL18
Programming Questions / Hackster code giving error

"I'd basically like to replace the button and LED in the schematic with the illuminated button on the right."
20JUL18
General Electronics / Help with illuminated push button wiring

"Button1 is the problem here."
18JUL18
Programming Questions / Why does it takes several presses for my LED to light?

"I had the LEDs 2,3,4 turning on and off"
18JUL18
Programming Questions / Re: Help with simple problems please.

"I am getting close to what I want but I am a beginner and my brain is starting to melt. I need buttons 2,3,4 to light up LEDs"
16JUL18
Programming Questions / Help with sketch

"I'd like to make a button box for gaming using an arduino UNO or similar with momentary buttons and have the buttons light LEDs"
16JUN18
Project Guidance / Advice on LED Button Box feasibility for noob

"I tried the original sketch from the Arduino tutorial page, but added four more LEDs on pins on 8 9 10 11"
21MAY18
LEDs and Multiplexing / Help with sketch please

"Thanks, but I probably told you what a noob I am."
27JAN18
Installation & Troubleshooting / Re: Can I change the name on the board when in appears Game Controller control panel

"I want to control by IR 2 mid size servos and also power some led strips"
19NOV17
Motors, Mechanics, and Power / OK to power my Nano project with a 7.4v lipo?

"Thanks for the tip, I'll give that a try tomorrow."
06JUN18
Installation & Troubleshooting / Re: Help with pro mini

========================================

I'm amazed that after more than two years of cross posting and re-posting you still haven't got those LEDs working. I guess your on-going claim to be a Noob is justified.

I have updated my OP with much more info.

jremington:
How very informative!

Please read and follow the helpful hints in the "How to use this forum" post. Or, consider having someone write code for you -- post in the Gigs & Collaborations section and be prepared to pay.

And now you've edited your post. I'm sorry that you feel offended, I've merely re-posted your own text and claims. Do you find that offensive?

Yeah, shouldn't "waste your time on me", I'm always posting on forums, clogging them up, look at my huge 33 posts in 2 years, you only have......I'm sorry......how many........ 15,361!!! I lol. If you don't want to help, stay under your bridge.

Delta_G:
It's also like that on Arduino Stack Exchange as well. So check for his thread there before you waste any time on him here.

https://arduino.stackexchange.com/questions/54802/last-bit-of-code-for-button-box-has-got-me-lost

@life'o_b: do have a nice life!

.....Thanks.

But seriously, I understand the sarcasm, but all I did was respond to people trolling instead of helping. Nice that you joined in too though. I suppose if we were in the school yard you'd all be ganging up on me to take my lunch money lol.

jremington:
@life'o_b: Have a nice life!

The reason I post in multiple places has just been justified by this thread. The first answer was sarcasm, the second a summary of my posts, and yours just followed that.

Me "bitching about it", was a response to your trolling, you never made any constructive effort to help, that's trolling. It seems you do it a lot, these are "answers" you've posted in the last day or so.

"Is this that "new math" I keep hearing about?" - useless post mocking a mistake.

"Does google not work today?" - Really?

"There's a reason for that." - with no explanation why.

Very informative and helpful answers. Not!

And as far as consideration goes, do I not deserve any? All I did was ask questions looking for help and advice on forums, I'm pretty sure that's what they're for.

Delta_G:
I'm not trolling. I'm warning people not to waste time answering until they've checked to see that you don't already have the answer on any of the other places where you asked your question. If you had any consideration for the people here trying to help you then you would have mentioned that you have this posted in multiple places and that may be a possibility yourself.

But why don't you just keep bitching about it. I'm sure that makes people want to write code for you.

I like the cat on the top best.

I'm kinda partial to brunettes. I'll take the one on the bottom.

Well, now that you mention it, for me it was pretty much a tossup between the top and bottom cat.

You had your choice!!!!!

"That's why you didn't get anyone berating you." - No, just telling people not to waste their time on me.

"whiny little bitches" - I believe it was you that whined about me posting elsewhere(how dare I). I just called you out on it.

"you'd be surprised how many idiots come here" Yeah, you don't berate people.

"You're not worth the time or the worry in that case. Why should I bother myself googling things for kids who are too lazy to do it themselves?" - That sounds like you're berating me.

"What have you accomplished with this? Not a damned thing." - I've literally learned something new about Arduino every single day in the last week or so, (googling, youtube videos, trying different sketches) until this thread of course.

"Is it the kid who keeps bitching that he doesn't like how the teacher talks that gets the help?" - If the teacher told people not to waste their time on you, you would not say anything? Would a teacher say, "no I'm not answering that, go read a book"? I think the problem with so many on here is you expect us noobs to know more than we do. It's hard to explain a problem when you're not sure what the problem is.

"you have to behave the same as you would if you were in a group talking" - How should I behave if in real life, if I was in class and someone said not to waste their time trying to help me?

"You're doing this to yourself kid. It's kind of pathetic. " - You're not berating me again are you?

And talking of acting like you're talking face to face, I guarantee you don't call people those names to their face in real life. You're a hypocrite and a troll. That's what winds me up.

And after you claim you're not trolling me, you post kitten pictures lol.

So basically trolling and insults. "Idiot", "Kid"(I'm 40), "Pathetic", "Lazy", did I miss any?

Delta_G:
Sure you do. That's why you didn't get anyone berating you. You got people pointing out how difficult helping you has become. Why should anyone waste time on you when there are others here who want help and aren't going to be whiny little bitches about it. When you act like that nobody wants to do anything for you. And now look at all the time you've wasted by bitching. And you're no closer to getting your button thingy working. What have you accomplished with this? Not a damned thing.

You could have come back with something different and someone might help you and you'd have a working button thingy now with led lights and all that.

No, a forum is just a place for people to discuss. They are still just people talking and you have to behave the same as you would if you were in a group talking. Ever been in a class? Is it the kid who keeps bitching that he doesn't like how the teacher talks that gets the help? Or is it the one who takes what the teacher says to heart and then asks another intelligent question? Which one generally makes it through with the A and which one generally just gets frustrated?

You're doing this to yourself kid. It's kind of pathetic.

Top cat is the winner again!

Hi,

Can you please post your full code?
Can you please explain what you want it to do?
Can you please explain what it is doing?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png.

Thanks.. Tom.. :slight_smile: