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;
}
}
}