Toggle LED on/off with momentary push switch for game controller

Hi, I have purchased an Arduino Micro in the hope to build a custom game controller, I am using the example code for the Joystick library, and have it working and recognised in windows with one switch currently (pin9) but I would like all 4 switches (9,10,11,12) to toggle their own LED on and off (push once and LED turns on, push again and LED turns off.

So I have two questions, could anyone point me in the right direction on how to code this? (this is my first time coding and using Arduino :wink:

and should each LED be wired to its own output pin, or is there a way to do this off the switch?

Code attached.

// Simple example application that shows how to read four Arduino
// digital pins and map them to the USB Joystick library.
//
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

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

Despite the </> at the top code tags are actually uses square brackets.

For a mere 4 buttons, a for loop seems like over kill to me.

<code>
// Simple example application that shows how to read four Arduino
// digital pins and map them to the USB Joystick library.
//
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

#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();
}

// Last state of the button
bool arrayButtonState[4] = {0,0,0,0};

void loop() 
{
  // Read pin values
  if (digitalRead(9) == HIGH)
  {
      arrayButtonState[0] = !arrayButtonState[0];
      Joystick.setButton(9, arrayButtonState[0]);
  }
  if (digitalRead(10) == HIGH)
  {
      arrayButtonState[1] = !arrayButtonState[1];
      Joystick.setButton(10, arrayButtonState[1]);
  }
  if (digitalRead(11) == HIGH)
  {
      arrayButtonState[2] = !arrayButtonState[2];
      Joystick.setButton(11, arrayButtonState[2]);
  }
  if (digitalRead(12) == HIGH)
  {
      arrayButtonState[3] = !arrayButtonState[3];
      Joystick.setButton(12, arrayButtonState[3]);
  }

  delay(50);
}

And if there is a Joystick.getButton(…) function then you could do away with your array completely.
When programming a microcontroller with limited memory you declare only those global variables that are absolutely necessary.

And if the joystick library has a Joystick.getButton(…) function then the button states are already being stored in some sort of data structure inside the library and you don’t need to duplicate the button states via your array.

 if (digitalRead(9) == HIGH)
      Joystick.setButton(9, !Joystick.getButton(9));

Just checked - there is no function Joystick.getButton(...) .

A pity.

It would be a simple function to add to the Joystick library for the greater convenience for everyone.

I’m just testing with 4 buttons, but my initial build will use 11. Just want to get it working with 4 to start with. So any idea on some code to get some LED’s to toggle with the buttons. Code updated to incorporate the 11 buttons (I think I did it right)

#include <Joystick.h>

void setup() {
  // Initialize Button Pins
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  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 = 2;

// Last state of the button
int lastButtonState[11] = {0,0,0,0,0,0,0,0,0,0,0};

void loop() {

  // Read pin values
  for (int index = 0; index < 11; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
}

I have taken the liberty of adding a getButton(…) function to the joystick library - see attached zip.

So this allows you to do away with your button state array and just use Joystick.getButton(9/etc/) instead.

For now I will assume you know where the libraries are located in the windows folder structure

Joystick.zip (18.7 KB)

Sorry I forgot a return value in the function I added. Fixed it in this version of the library.

Joystick.zip (18.8 KB)

So how do I add the LED command to that?

So any idea on some code to get some LED's to toggle with the buttons

You need to detect when a button becomes pressed or released. Look at the StateChangeDetection example in the IDE or use something like the Button2 library

// Simple example application that shows how to read four Arduino
// digital pins and map them to the USB Joystick library.
//
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

#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();
}

// Last state of the button
bool arrayButtonState[4] = {0,0,0,0};

void loop() 
{
  // Read pin values
  if (digitalRead(9) == HIGH)
  {
      Joystick.setButton(9, !Joystick.getButton(9));
  }
  if (digitalRead(10) == HIGH)
  {
      Joystick.setButton(10, !Joystick.getButton(10));
  }
  if (digitalRead(11) == HIGH)
  {
      Joystick.setButton(11, !Joystick.getButton(11));
  }
  if (digitalRead(12) == HIGH)
  {
      Joystick.setButton(12, !Joystick.getButton(12));
  }

  delay(50);
}

I’m getting Joystick was not declared in this scope, error

GreenMamba:

// Simple example application that shows how to read four Arduino

// digital pins and map them to the USB Joystick library.
//
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

#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();
}

// Last state of the button
bool arrayButtonState[4] = {0,0,0,0};

void loop()
{
 // Read pin values
 if (digitalRead(9) == HIGH)
 {
     Joystick.setButton(9, !Joystick.getButton(9));
 }
 if (digitalRead(10) == HIGH)
 {
     Joystick.setButton(10, !Joystick.getButton(10));
 }
 if (digitalRead(11) == HIGH)
 {
     Joystick.setButton(11, !Joystick.getButton(11));
 }
 if (digitalRead(12) == HIGH)
 {
     Joystick.setButton(12, !Joystick.getButton(12));
 }

delay(50);
}




I'm getting Joystick was not declared in this scope, error

Oh…it might not be the precise library you are using or not the same version.

According to one of the example sketches you have to do this at the top of your .ino for the library I found:

// Create Joystick
Joystick_ Joystick;

Or just cut and paste the added function from the library in the zip I gave you into the .h and .cpp file of the library you have. More than likely the libraries will be implemented in much the same way apart from perhaps precise variables names which are easy to look up in the .h file and correct in the added function.

adding that and it now gives error Joystick_ Joystick does not name a type

GreenMamba:
adding that and it now gives error Joystick_ Joystick does not name a type

Where did you download the joystick library that you were originally using from?

Perhaps easier to just extract the added function from my library and insert it into your library?

If you tell me where you downloaded it from I will just modify that joystick library with the added function for you.

GreenMamba:
https://github.com/MHeironimus/ArduinoJoystickLibrary

Yup same library.

If I put your code in a sketch and stick this at the very top 'Joystick_ Joystick;' then it compiles for me.

Perhaps I forgot to put a space in between Joystick_ and Joystick hence the reason you got that compile error.

See this is why I like to use MFC style Hungarian notation because it is less confusing.

Instead of Joystick_ Joystick it would have been CJoystick Joystick;

As in 'C' for Class Joystick

Should I just delete the library I downloaded from GitHub and replace with the one you zipped? or should I be copying certain files into the original library?

GreenMamba:
Should I just delete the library I downloaded from GitHub and replace with the one you zipped? or should I be copying certain files into the original library?

Just unzip it and overwrite the one you have.

I would keep a copy of the zip I gave you because, if you ever update that library from the IDE, you will lose the changes I made to the library.

If you have a copy of the zip I gave you then you can just cut and past my added function to the updated library and thus retain the author's official updates AND my added function.

A bit of a pain I know, but that library probably won't be updated very often.

Joystick_ Joystick;


// Simple example application that shows how to read four Arduino
// digital pins and map them to the USB Joystick library.
//
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

#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();
}

// Last state of the button
bool arrayButtonState[4] = {0,0,0,0};

void loop() 
{
  // Read pin values
  if (digitalRead(9) == HIGH)
  {
      Joystick.setButton(9, !Joystick.getButton(9));
  }
  if (digitalRead(10) == HIGH)
  {
      Joystick.setButton(10, !Joystick.getButton(10));
  }
  if (digitalRead(11) == HIGH)
  {
      Joystick.setButton(11, !Joystick.getButton(11));
  }
  if (digitalRead(12) == HIGH)
  {
      Joystick.setButton(12, !Joystick.getButton(12));
  }

  delay(50);
}

Is that the same code you are getting it to compile with? I’m still getting Joystick_ does not name a type

If ever you have to do the above then just navigate your way to this folder:

C:\Users\Greg\Documents\Arduino\libraries\Joystick\src (replace Greg with your windows user name)

Assuming you are using ms windows of course.

In this folder you will find Joystick.cpp and Joystick.h

These are the two files I added my function to.

Have a look at the way you add a function to a class by searching for getButton on both files.

It is not particularly difficult and all you will be doing is a straight copy and paste.

GreenMamba:

Joystick_ Joystick;

// Simple example application that shows how to read four Arduino
// digital pins and map them to the USB Joystick library.
//
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

#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();
}

// Last state of the button
bool arrayButtonState[4] = {0,0,0,0};

void loop()
{
 // Read pin values
 if (digitalRead(9) == HIGH)
 {
     Joystick.setButton(9, !Joystick.getButton(9));
 }
 if (digitalRead(10) == HIGH)
 {
     Joystick.setButton(10, !Joystick.getButton(10));
 }
 if (digitalRead(11) == HIGH)
 {
     Joystick.setButton(11, !Joystick.getButton(11));
 }
 if (digitalRead(12) == HIGH)
 {
     Joystick.setButton(12, !Joystick.getButton(12));
 }

delay(50);
}




Is that the same code you are getting it to compile with? I'm still getting Joystick_ does not name a type

Yup, I copied your code, stuck it in a sketch, added that afore mentioned line and it compiled.

Once you have unzipped my zip file and overwritten your existing library, try exiting out of Arduino IDE and re-load your sketch.

Unfortunately Arduino IDE does not register changes in libraries on the fly.

no matter what I try its still giving me the same error, Joystick_ does not name a type