NESC or Nintendo Entertainment System Controller Library

While revisiting an old project of mine (NEOPixel Snake game), in order to update and improve it, I decided that I wanted to change the game controls from a keyboard to something different. The main reason for this change was the fact that PS2 keyboards while still around are not as common as they used to be. I also wanted to come up with a control that I could use easily with other. With this in mind I decided to use the NES controller after watching a video from the 8-bit guy (link), and doing some Googling which lead me to an Arduino project also involving the NES controller (link). The availability of still manufactured 3rd party controllers also made the controller a good choice.

Using the same code snippet from the Wireless NES Controller
Using Arduino project, I set out to make a library that would allow quick and
easy interfacing with the NES controller. What I wanted was an easy way to
check the status of each button on the controller individually and as a group.
This would let me check if one of a group of buttons had been pressed and then
from that which one. The library also has the feature that allows you to check
when an exact combination is being held down. This is done by using logic
operators such as logic or “|” and logic ands “&”.

Usage:

When using this library, the first thing you need to do is
define the type of controller you are going to be using. You do this by using
the define statement “#define NES_CONTROLLER” before calling the import
statement for the library. This will define a normal NES controller and is the default
controller type. If desired it can be omitted as it will always be used if not
stated but could be helpful when writing code as you will say outright to
anyone reading your program what controller you intended to be used. The second
controller type you can define is the Super Nintendo Controller, to do this use
“#define SNES_CONTROLLER”. As of the writing of this article, the SNES
controller is not fully implemented nor tested as I don’t have the controller
to test with.

After you have setup the controller type you need to create
the NESC object and then call the begin function during the setup loop
passing in the Arduino pin number for Latch, Clock and Data in that order.

In order to get the state of the controller you call the update function, which will get the
current state of the controller and save it for it to be checked.

There are different ways to check the state of the
controller buttons, but if you only want to know if one button is pressed then
you can use its respected function. Below is a list of each button function
name, and when called they will return a true when pressed or false when
released.

These functions are good for checked individual buttons but
they are not the only way to check buttons. On top of the above function there
are also pressed,
released and exclusivePressed functions. You are able to pass each of these
functions keywords, which are listed below, using logic operators separating each
keyword. These functions will return a true if the condition is a match to what
you set, or a false when there is not a match.

For example, if you wanted to check if
either the A or B button was pressed then you would do the following, pressed(A_BUTTON | B_BUTTON). The same
is done for released and exclusivePressed with the different of exclusivePressed being that you pass
first the buttons you want to be pressed down and then the buttons that should
be released, exclusivePressed(A_BUTTON,
B_BUTTON | UP_BUTTON). This will return true if the A button is pressed but the
B and Up Button are both released. If you wanted to check that two or more
buttons where pressed without caring about the other button states you would use
exclusivePressed(A_BUTTON | B_BUTTON,0).

Every time you want to get the new state of the controller
you call the update function again
and you can perform more checks.

Conclusion:

With the exception of the SNES controller the library is fully functional with what I believe are all the features that someone would find useful. Also some care was put into the library being made pretty light weight and fast so in timing sensitive applications there would be minimal impact. Now all that’s left is getting an SNES controller and getting that working.

Download:

You can find the library on my github at https://github.com/M-tech-Creations/NESCOnce the SNES controller is implemented and I’m pretty sure the bugs have been worked out I’ll be working to have the library added to the Arduino Library Manager.