Funduino Joystick Shield Reaction Game

DSCN4221.JPG

The idea of making video games on an Arduino intrigued me – I have seen many games being made on a Raspberry Pi  but never really on an Arduino.

Pictured above is the ‘Funduino’ Joystick Shield, a cheap controller-style shield for Arduino with 6 buttons (a directional pad and the start/back buttons if this were a real gaming controller) and one rather large joystick.  I’m super excited to get started using this shield, as I ordered it weeks ago and it has only just arrived.

I bought mine from here for about £5, but you can pick them up on most electronics websites for about the same price.

To test this shield and to see how it works, I made an example sketch which utilizes all the functions of this shield.

This sketch (which I will show you at the end of the post) records the average time the user takes to press a random button stated in the serial monitor.

In This Tutorial

  1. How the Joystick Shield Works.
  2. How to program it.
  3. Reaction game project (code tutorial).
  4. Conclusion.

1. How the Joystick Shield Works

The joystick shield is made up of various different components.dscn4214

Firstly, on the left, we have the enormous analog joystick that sends its exact position to A0 (x-axis) and A1 (y-axis) on the Arduino. This makes it useful for controlling servo motors, r/c cars or pretty anything moving device. As well as being able to move this joystick around, you can press it down, which acts the same as one of the buttons on the right.

On the right, we have 4 giant pushbuttons with plastic caps over the top to make them easier to press. One thing I found slightly annoying was how easily the plastic caps fall off, but that was no big deal really. They work well and send their state (whether they are pressed or not) to digital pins 2 (up), 3 (right), 4 (down) and 5 (left)

In the middle we have two more buttons; the ‘start’ button and the ‘select’ button. These are very small making them hard to press and are positioned in a very awkward position just above the pin headers. I would avoid using these buttons if possible. These send their state to digital pins 6 (start) and 7 (select).

Untitled.pngThere are a few other things on the board worth pointing out – it supports an I2C interface
and has some pins for a Nokia 5110 display, which could be very useful for certain projects, especially if you need a basic screen for a game you are programming on it.

2. How to Program it

There are two ways of programming this shield – you can use the joystick shield library for it or not use it. Personally, I wouldn’t use the library as I think it makes it more confusing than just coding it normally, but you can use it if you want to. I have written a tutorial for both, so that you can choose.

Using the Library

If you’re going to use the library, you will have to download it from this link. After you have downloaded the zip file, import it into your Arduino IDEUntitled.png

Once imported, you can see 2 example sketches showing you how it works. One of the examples, ‘SimpleExample’shows every function working in one program, so you may want to check that out.

Below is an example I have made to demonstrate how this works. This sketch will light up the Arduino’s on-board LED on pin 13 when you press the ‘up’ button.

This sketch should be fairly understandable for beginners, so I am not going to do a line-by-line code explanation.

#include <JoystickShield.h> 

JoystickShield joystickShield; // create an instance of JoystickShield object

int ledPin = 13; //Arduino's onboard LED

void setup() {
 pinMode(ledPin, OUTPUT);
 joystickShield.calibrateJoystick();
 // predefined Joystick to Pins 0 and 1.
 // Change it if you are using a different shield
 // setJoystickPins(0, 1);
}

void loop() {
 joystickShield.processEvents(); 
 if (joystickShield.isUp()) {
   digitalWrite(ledPin, HIGH) ;
 }
}

As you can see from this program, the joystick shield library has various different functions. I will quickly explain what each of them do.

  • joystickShield.calibrateJoystick() – this is apparently a new function that has just been added to the library and isn’t entirely necessary – the code will still work fine without it. This function simply calibrates the joystick so that it is very accurate.
  • setJoystickPins() – you will only need to use this for certain models/versions of the shield where the Joystick uses different pins. Most shields will use the ones that are predefined and you will not need to use this.
  • joystickShield.processEvents() – this makes the Arduino start processing which buttons have been pressed on the shield.
  • joystickShield.is[see possible values below]() – checks to see if a certain button is pressed or not. For example, joystickShield.isUp() will check if the Joystick has been moved upwards.

Possible values for [] in joystickShield.is are:

CHECK JOYSTICK: Up, RightUp, Right, RightDown, Down, LeftDown, Left, LeftUp, JoystickButton.

CHECK BUTTONS: LeftButton, RightButton, DownButton, UpButton.

SELECT/START BUTTONS: EButton, FButton.

Not Using the Library

If you are not going to bother with using the library, you read the digital and analog pins just like they were individual buttons/a joystick on a breadboard.

These are the pins that the buttons/joystick are set to:

Up Button – Digital Pin 2

Right Button – Digital Pin 3

Down Button – Digital Pin 4

Left Button – Digital Pin 5

Start Button – Digital Pin 6

Select Button – Digital Pin 7

Analog Button – Digital Pin 8

Joystick x-axis – Analog Pin 0

Joystick y-axis – Analog Pin 1

int ledPin = 13;//Arduino's onboard LED
int upButton = 2; //we will be using the up button on the Arduino

void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(upButton, INPUT);
}

void loop() {
 if (analogRead(upButton) == HIGH) {//when up button pressed, turn LED on
   digitalWrite(ledPin, HIGH) ;
 }
}

3. Reaction Game

This is the main part of today’s post – the reaction game. I wrote this sketch to test the full capability of the Funduino shield and to see all the buttons and the joystick used together in one project.

The idea of my game is simple. It records the average time the user takes to press a random button stated in the serial monitor. You press the button it says to in the serial monitor, then it records the time it takes you to press that button, and gives you another one to press and records the time it takes for that too. When you have had 2o presses, it tells you your average reaction time.

It’s a very basic program but does the job of testing out what the joystick shield can do.

PLEASE NOTE: There are bugs in this program I haven’t fixed yet (sorry).

//Program by PI AND CHIPS BLOG: piandchipsblog.wordpress.com
//Do whatever you want with it, I don't care
int wait = 0; //variable to count the number of random buttons pressed

//variables for the different buttons on the shield
int up_button = 2; 
int down_button = 4;
int left_button = 5;
int right_button = 3;
int start_button = 6;
int select_button = 7;
int analog_button = 8;
int x_axis = A0;
int y_axis = A1;

//an array for setting them as inputs easier
int buttons[] = {up_button, down_button, left_button, right_button, start_button, select_button, analog_button};
int randomNumber = 1;//code to generate a random number will come later, but for now set it to one
int total_time = 0; //variable to store the total time taken
int avg_time = 0; //variable to store the average time taken
void setup() {
 for (int i; i < 7; i++) //iterates through the buttons in the button array and sets them as inputs, and sets them as high
 {
 pinMode(buttons[i], INPUT);
 digitalWrite(buttons[i], HIGH);
 }
 Serial.begin(9600);//initialize the serial monitor
 Serial.println("Welcome to the Arduino Reaction Game!");
 delay(100);
}

void loop() {
 
 while(wait <= 20){ //while the number of buttons pressed is less than or equal to 20

 switch (randomNumber) {//switch statement means something different will happen depending on the random number chosen

 case 1: //if the random number is equal to one, which to start with, it will be
 Serial.println("LEFT BUTTON"); //tell the user to press the left button
 while(digitalRead(left_button) == 1){//until the left button is pressd
 delay(1);//wait 1 millisecond
 total_time++;//increase total_time by one each millisecond which will record the average time taken by the user to press the button
 }
 wait++; //when the user has pressed the button, increase the variable recording how many turns the user has had by one
 randomNumber = random(1, 7); //GENERATE A RANDOM NUMBER WHICH WILL DECIDE WHICH BUTTON THE USER WILL PRESS NEXT
 break;
 
//ALL THE OTHER CASE STATEMENTS ARE THE SAME AS ABOVE BUT WITH DIFFERENT BUTTONS
 case 2:
 Serial.println("RIGHT BUTTON");
 while(digitalRead(right_button) == 1){
 delay(1);
 total_time++;
 }
 wait++;
 randomNumber = random(1, 7);
 break;
 
 case 3:
 Serial.println("UP BUTTON");
 while(digitalRead(up_button) == 1){
 delay(1);
 total_time++;
 }
 wait++;
 randomNumber = random(1, 7);
 break;
 
 case 4:
 Serial.println("DOWN BUTTON");
 while(digitalRead(down_button) == 1){
 delay(1);
 total_time++;
 }
 wait++;
 randomNumber = random(1, 7);
 break;

case 5:
 Serial.println("START BUTTON");
 while(digitalRead(start_button) == 1){
 delay(1);
 total_time++;
 }
 wait++;
 randomNumber = random(1, 7);
 break;

case 6:
 Serial.println("SELECT BUTTON");
 while(digitalRead(select_button) == 1){
 delay(1);
 total_time++;
 }
 wait++;
 randomNumber = random(1, 7);
 break;

case 7:
 Serial.println("JOYSTICK BUTTON");
 while(digitalRead(analog_button) == 1){
 delay(1);
 total_time++;
 }
 wait++;
 randomNumber = random(1, 7);
 break;
 }
 
}
//when the user has had 20 button presses
avg_time = total_time / 20 /100 ;//work out averag time.
 Serial.println("Your average response time was ");
 Serial.println(avg_time);
 Serial.println("Seconds");
 Serial.print("THANKS FOR PLAYING");
 delay(10000);
}

4. Conclusion

DSCN4213.JPG

  • The gamepad shield is a very good and well-made shield for the Arduino that has 6 buttons and a joystick.
  • It is easy to program, with a dedicated library that has many built in functions making it easier to use.
  • There are some interesting features on this shield such as the Nokia 5110 screen output pins, making it easy to program your own real games with a proper lcd screen.
  • You can buy them from about £5 from places like Amazon or eBay.

Further Reading

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s