This could be used to create a more complex game, or maybe even to try and make an Arduino based mini-console. Eventually, I will get a video up of the game in action, but I dont know when.
Step 1: Materials Needed
-1 small breadboard
-1 old SNES controller that you wouldn't mind tearing up
-10-15 Jumper wires(I use 11)
Step 2: Hook Up All the Wires for the Arduino
Pin 2: Strobe Pin. Connects to Orange wire from controller, so I would make the Jumper Pink or Orange.
Pin 3: Clock Pin. Connects to Yellow wire from controller, so I recommend a Yellow Jumper.
Pin 4: Data Pin. Red wire on controller, so a red jumper would be best.
Pin 5: Power for top led.
Pin 6: Power for right led.
Pin 7: Power for bottom led.
Pin 8: Power for left led.
Pins 5-8 should have same colored jumper wire. I choose Dark Green.
Pin 9: Power for game indicator led. I used Light green.
5V: Power pin, connects to white wire from controller, so i used a white jumper.
GND: Brown wire from controller, I used a black jumper.
GND: Used to connect all grounds from breadboard back to arduino, i use blue for all the ground wires.
Step 3: Setting Up LEDs and Breadboard
For the game indicator LED, just put it away from the others, hook up its anode to Pin 9, and its cathode to the ground rail.
Step 4: Hooking Up the Controller
First tape the controller cord to the desk by the arduino. If you let it move, you will have a hard time making good connections. My jumper wires slip right out of the little metal things, so i took a pocket knife, and pried the ends apart(see pictures)so that i could stick the jumper wire perpendicular into the socket thing. When hooking the 5 wires up to the controller, make sure they do not touch eachother, and that you are matching the colors correctly.
Step 6: The Code, Part 1
So the first 7 lines are pretty simple, because they just assign names to the 3 controller pins and the game indicator pin.
Lines 8-20 declare the hex data used to detect which button is pressed on the controller. Each hex value turns on a separate bit in the data stream, so that way you can just use bit shifts to determine if a certain button is pressed, or just use a bitwise and.
The variable 'array' is just an array of integers that is 30 ints long. This stores the sequence of presses for the game.
'pos' is a position variable. More on that later.
'timeForPress' is how long you have to press the buttons.
Up next is the setup() method. We start by seeding the random number generator, and then setting the pin mode for each of the pins 2-9. Next we open up serial communication, for debugging purposes, and set the sequence of buttons to all '4's. I did this b/c the numbering system for the LEDs only goes up to 3, so the 4 is essentially just a placeholder. Now we set timeForPress to 1000, and call the genSequence() method, which fills our array with random integers between 0 and 3(inclusive). Next, it sets our position to 0, to show that it hasn't started the game yet, and then it flashes the game indicator to show that the game is about to begin.
Step 7: The Code, Part 2
I think I got a little bit of this code from this instructable. Not entirely sure though.
There are three methods used to check the controller input:
The buttons() method first sets up a return variable and a place counter. It then strobes (using strobe()), and enters a for() loop. In the for loop, it reads and stores the data one bit at a time using the shiftin() method and bitwise operators.
The method strobe() is pretty straightforward. Basically just sets the strobe pin to HIGH, then immediately back to LOW. Pretty simple stuff. This tells the controller to begin transmitting data.
The shiftin() method actually reads the data. It takes the data from the data pin, then toggles the clock pin, which tells the controller to send the next bit over.
Using just these three methods, you can read the state of the controller. You should never have to directly call strobe() or shiftin(). On a final note, when checking to see if a button is being pressed, you have to use 2 bitwise operators. The ~ (tilde) is a bitwise not, and inverts the data, then the & checks the state vs whatever button you want pressed.
Step 8: The Code, Part 3
The loop() method starts out by serially printing the state of the controller. It then plays the sequence stored in the array, then checks the user input with the readAndCheckInput method, which essentially just a for loop with some if statements in it. If you get one wrong, it breaks out of the loop and does the blinking thing then ends.
I guess that means this tutorial is over! If y'all make any cool games, please comment and put a picture, as I would love to see them!