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

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

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

Scroll down until you see 'class'

E.G. class Joystick_

What is the class name (the string after 'class'')

The only other possible cause that I can think of is that you have a conflicting library that also contains Joystick.h

But this Joystick.h does not contain 'class Joystick_'

If you are not using the conflicting library then just delete the folder.

If not you will have to rename your Joystick library.

Been there done that with an SD library that conflicted with the one that comes with Arduino ID!

In this case the only remedy I came up with as to rename the SD library I wanted to use to 'MySD'.

It's a bloody pain because I had to rename SD.h and SD.cpp tp MySD.h and MySD.cpp

Then I had to rename to library folder from 'sd' to 'MySD'.

Then I had to rename the library in 'library.properties'

Then I had to change #include "sd.h" to #include "MySD.h" in all the example sketches and MySD.cpp

On the upside renaming the SD library meant that IDE library updates could not wipe out changes to 'MySD'

Ok I moved Joystick_ Joystick underneath #include Joystick.h line and it now compiles

However buttons are not acting as they should, it looks like the switches are pressed as default. so ive changed digitalread to LOW and that seems to fix that but, it is not acting as a momentary press, the button is flickering on and off when pressed, and doesn't always return to off when released, its intermittent.

GreenMamba:
Ok I moved Joystick_ Joystick underneath #include Joystick.h line and it now compiles

However buttons are not acting as they should, it looks like the switches are pressed as default. so ive changed digitalread to LOW and that seems to fix that but, it is not acting as a momentary press, the button is flickering on and off when pressed, and doesn't always return to off when released, its intermittent.

Well I have never played with a joystick with Arduino so I am not sure what is going on there.

Might the the joystick buttons might have some sort of repeat feature like you keyboard does in windows.

I.E. When you press 'N' you get a the letter N repeated in the text editor until you take your finger off the button.

The previous code before using the getButton it would just stay pressed when button held, where as now it is flicking on and off really fast.

See that video I just uploaded and how intermittent the button is and is flickering on and off really fast when pressed.

Now wait just a minute!

Why are you even doing this at all 'if (digitalRead(11) == HIGH)'?

I would have though that any interaction with the joystick should be through the class and not separate digital reads.

What are the digitalReads(...) actually reading on the joystick?

What joystick hardware are you using?

they are set to LOW

I’m using a momentary switch

Digitalread 9 is joystick button 0

// 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>

Joystick_ Joystick;

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) == LOW)
  {
      Joystick.setButton(0, !Joystick.getButton(0));
  }
  if (digitalRead(10) == LOW)
  {
      Joystick.setButton(1, !Joystick.getButton(1));
  }
  if (digitalRead(11) == LOW)
  {
      Joystick.setButton(2, !Joystick.getButton(2));
  }
  if (digitalRead(12) == LOW)
  {
      Joystick.setButton(3, !Joystick.getButton(3));
  }

  delay(50);
}

GreenMamba:
they are set to LOW

I’m using a momentary switch

Digitalread 9 is joystick button 0

// 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>

Joystick_ Joystick;

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) == LOW)
  {
      Joystick.setButton(0, !Joystick.getButton(0));
  }
  if (digitalRead(10) == LOW)
  {
      Joystick.setButton(1, !Joystick.getButton(1));
  }
  if (digitalRead(11) == LOW)
  {
      Joystick.setButton(2, !Joystick.getButton(2));
  }
  if (digitalRead(12) == LOW)
  {
      Joystick.setButton(3, !Joystick.getButton(3));
  }

delay(50);
}

Show me an image of the joystick you are using, i.e. google an image of it.

I am not clear why you are even using digitalRead(…) when all interaction with the hardware is normally done through the library class interface.

Did you watch the youtube link I posted?

Its a momentary switch which I’m going to be building as a game controller

GreenMamba:
Did you watch the youtube link I posted?

Its a momentary switch which I'm going to be building as a game controller

That thing is just a standard push button from the likes of Jaycar?

In which case I don't even understand why you are using the joystick library at all?

That Joystick library is meant for an Arduino gamepad.

If you intention is to design your own gamepad then I would be writing my own specific library for it rather than trying to use some one else's library.

So let me understand your intentions correctly....

Ultimately you want to have 12(?) push buttons, associated with 12 LEDs and you want the appropriate LED to toggle on an off with each button press.

Is that correct?

Or do you want the appropriate LED to light up when ever the button is in a pressed state?

Ultimately you want to have 12(?) push buttons, associated with 12 LEDs and you want the appropriate LED to toggle on an off with each button press.

This is correct.

In fact, I only need 10 buttons and 10 LEDs (as I believe that is the limit of the the Arduino Micro)

GreenMamba:
Ultimately you want to have 12(?) push buttons, associated with 12 LEDs and you want the appropriate LED to toggle on an off with each button press.

This is correct.

In fact, I only need 10 buttons and 10 LEDs (as I believe that is the limit of the the Arduino Micro)

Well I have written a few libraries for accelerometers as an exercise in learning.

So I will set it up for you how I would do it.

You can then fiddle around with it yourself.

I am going to use Microsoft Foundation Class style of Hungarian notation.

And I am going to put the library files in with the sketch so that you can edit them in the IDE.

If you want to relocate the library source files to an actual library then you can learn how to do that later.

Give me about 20 minutes or so.

appreciate your support!

The way that joystick library deals with button states is as single bits rather than an array of bools.

Bitwise button states is far more memory efficient.

You want 10 buttons so a 16 bit/2 byte integer is sufficient to store the states of all 10 buttons.

1010101010101010 - each one of those bits (0 or 1) represents the state of one of your 10 buttons.

There will be 6 unused bits.

Arduino has bit functions available to make this easy - look them up.

bitSet(…)
bitReset(…)
bitRead(…)

Try this. Obviously I can’t test it to make sure I have everything right.

So I will have to leave that to you.

As you add more bits to your gamepad, such as the hat switch, you can extend class CGamepad accordingly.

DIYGamepad.zip (1.97 KB)

Thanks looks like a much neater way of doing this, however it doesn't get recognised as a joystick after compiling

GreenMamba:
Thanks looks like a much neater way of doing this, however it doesn’t get recognised as a joystick after compiling

Doesn’t get recognized as a joystick? You have this thing attached to an Arduino don’t you?

As far Arduino is concerned what kind of device it is is irrelevant.

Are you creating a device that you can plug into a windows PC?

I.E. Making something like this from scratch based on a Leonardo?

If so then that is somewhat different to creating a gamepad that does something on an Arduino with an LCD screen or whatever.

Up until now I have been assuming you are attempting the latter.

Assuming you are attempting to create a gamepad that works with MS Windows…

That Joystick library seems to be ‘dumb’ in the sense that it ONLY reports to Windows the state of buttons and hats on the joystick device.

It has no idea what pins on the leonardo (or whatever you are using) the buttons are connected to.

So this adds another layer of complexity.

The library I started writing will have to ‘record’ what pins which buttons are connected to, deal with the digitalRead(…) calls to obtain the button states and then report those to Windows through the Joystick library.

In other words it will have to be a ‘wrapper’ for the joystick library.

Does this make sense to you?

Try the attached sketch + wrapper library and see if it does what you are expecting.

Without being able to test it myself I am making educated guesses.

If that works with the buttons, perhaps you could have a crack at adding the analog hat to the wrapper library.

You will need to look in the Joystick library examples and see which function you are meant to use to report the state of the hat.

And then extend class CGamepad and CGamepad::poll() to include the hat switch.

DIYGamepad.zip (1.51 KB)