Joystick Shield V1.A

Introduction:

The joystick Shield V1.A will let you turn your Arduino board into a controller like the game console system.  It can be used to control anything from a remote control car to anything that you can think of.  It also provides a lot of different interfaces!

Difficulty:

Beginner

Before you start:

Make sure you have installed the Arduino IDE software!  You can find more info here!!

Features:

  • Two-axis thumb joystick like the one from playstation that connects to two analog pins
  • 4 round buttons, 2 small buttons that connect to 6 digital pins
  • nRF2401 RF interface
  • Nokia 5110 LCD interface
  • bluetooth interface
  • I2C interface
  • RS232 interface
  • use 2,3,4,5,6,and 7 digital pin
  • use 0,  and 1 analog pin

Where can I get one?

How do I put it together with a arduino board?

All of the pins go into the arduino board.  Make sure they go all in!!!!

 

Example Code:

Once you connect the joystick shield to an arduino board.  You can load the below code and bring up the serial monitor.  As soon as you press any buttons and move the joystick, it will show the changes in the monitor.

/*********************************************************************
** Device: Joystick shield -- Duncan Lam v1.0 **
** File: sketch_jostick_1pa **
** **
** Created by Duncan Lam 4/22/2015 **
** **
** Description: **
** This file is a sample code for you to test the joystick shield **
** 1.A it has the correct pins **
** look at the code below for the correct pin accosicate with the **
** different digital pins and the analog stick with the analog pins**
*********************************************************************/
// Select button is triggered when joystick is pressed
const byte PIN_BUTTON_A = 2;
const byte PIN_BUTTON_B = 3;
const byte PIN_BUTTON_C = 4;
const byte PIN_BUTTON_D = 5;
const byte PIN_BUTTON_E = 6;
const byte PIN_BUTTON_F = 7;
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
boolean pressed_a = false;
boolean pressed_b = false;
boolean pressed_c = false;
boolean pressed_d = false;
boolean pressed_e = false;
boolean pressed_f = false;
int analog_x = 350;
int analog_y = 350;
void setup() {
Serial.begin(9600);
pinMode(PIN_BUTTON_A, INPUT);
digitalWrite(PIN_BUTTON_A, HIGH);
pinMode(PIN_BUTTON_B, INPUT);
digitalWrite(PIN_BUTTON_B, HIGH);
pinMode(PIN_BUTTON_C, INPUT);
digitalWrite(PIN_BUTTON_C, HIGH);
pinMode(PIN_BUTTON_D, INPUT);
digitalWrite(PIN_BUTTON_D, HIGH);
pinMode(PIN_BUTTON_E, INPUT);
digitalWrite(PIN_BUTTON_E, HIGH);
pinMode(PIN_BUTTON_F, INPUT);
digitalWrite(PIN_BUTTON_F, HIGH);
}
void loop() {
if(pressed_a == digitalRead(PIN_BUTTON_A)){
Serial.print("a pressed");
Serial.println();
}
if(pressed_b == digitalRead(PIN_BUTTON_B)){
Serial.print("b pressed");
Serial.println();
}
if(pressed_c == digitalRead(PIN_BUTTON_C)){
Serial.print("c pressed");
Serial.println();
}
if(pressed_d == digitalRead(PIN_BUTTON_D)){
Serial.print("d pressed");
Serial.println();
}
if(pressed_e == digitalRead(PIN_BUTTON_E)){
Serial.print("e pressed");
Serial.println();
}
if(pressed_f == digitalRead(PIN_BUTTON_F)){
Serial.print("f pressed");
Serial.println();
}
if(pressed_a == digitalRead(PIN_BUTTON_A)){
Serial.print("a pressed");
Serial.println();
}
if( !( ( analog_x + 2 >= analogRead(PIN_ANALOG_X ) ) && ( analog_x - 2 <= analogRead(PIN_ANALOG_X ) ) ) ){ analog_x = analogRead(PIN_ANALOG_X); Serial.print("controller x = "); Serial.print(analog_x); Serial.println(); } if( !( ( analog_y + 2 >= analogRead(PIN_ANALOG_Y ) ) && ( analog_y - 2 <= analogRead(PIN_ANALOG_Y ) ) ) ){
analog_y = analogRead(PIN_ANALOG_Y);
Serial.print("controller y = ");
Serial.print(analog_y);
Serial.println();
}
delay(100);
}

hmm… the code ran, but what the hell does the code mean?

Alight, here are the first few lines.  Basically, there are six buttons on the board, A, B, C, D, E and F.  They are connected to the digital pin, 2, 3, 4, 5, 6, and 7.  We are defining them as constant using the keyword “const”, as they are not going to change in the entire program.

const byte PIN_BUTTON_A = 2;
const byte PIN_BUTTON_B = 3;
const byte PIN_BUTTON_C = 4;
const byte PIN_BUTTON_D = 5;
const byte PIN_BUTTON_E = 6;
const byte PIN_BUTTON_F = 7;
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;

Alright, what’s next?  Setting up the serial port, so that you can send output through the serial port to the PC.

Serial.begin(9600);

This simply assigns the pin to the digital pin that you defined above.  For this case, we defined PIN_BUTTON_A as 2.  You are using the digital pin 2, as an input that is connected to button A.

pinMode(PIN_BUTTON_A, INPUT);

Well, the below code has to be programed that way because you are using push buttons.  You have to use the built in “pull-up” resistor.  Without going into all the details.  Whenever you use a similar button, you will have to use the keyword “HIGH” to enable the pull up resistor!

digitalWrite(PIN_BUTTON_A, HIGH);

This one is easy, remember we setup the serial port above?  Yea, the below code will send “a pressed” when the A button is pressed.

if(pressed_a == digitalRead(PIN_BUTTON_A)){
Serial.print("a pressed");
Serial.println();
}

You know in the “loop()”, it just keeps looping over and over again.  You don’t want to keep printing stuff out to the serial port.  You will be looking at too many text output from the board if you don’t do something smart.  So, the below code will print out the current joystick position ONLY when it changes positions more than “2” in the analog bit.

if( !( ( analog_x + 2 >= analogRead(PIN_ANALOG_X ) ) && ( analog_x - 2 <= analogRead(PIN_ANALOG_X ) ) ) ){
analog_x = analogRead(PIN_ANALOG_X);
Serial.print("controller x = ");
Serial.print(analog_x);
Serial.println();
}

that’s about it for the example code.
Wait, why are there dead zones in the joystick?

  
You reach the minimum value, in this case “0”, at this position about 30 degrees.  It will remain as “0” no matter how far you move the joystick.

  
yes, it does have dead zone!  As you can see from the above picture,  The joystick can go pretty far to the left about 40 degree.  It remains at value = “0”.

Looking at your output from the Serial Monitor:

Once you upload your sketch to your arduino board.  You can bring up the Serial Monitor.  Go to Tools -> Serial Monitor.

or you can press Ctrl+Shift+M all together.

serial_monitor

Once you press a button, or move the joystick, and you will see something like the picture above!

Feel free to send me any questions, and I will be glad to answer them!!

2 thoughts on “Joystick Shield V1.A

  1. I want to use the joystick on an Arduino joystick shield as an on/off switch. Could you pls. Show the hook-up and the sketch required?
    S.Browman
    Montreal,Canada

    Like

    • Hmmm.. I am not sure what you mean by on/off switch. The arduino has to be plugged in to USB or a power source in order to be used. If you are running it off battery, you can put a physical switch there. That’s probably not a good answer to your question. The best solution that I can think of is that using a button to trigger the interrupt to sleep mode. Hence, the arduino is still on, but it will save a lot of power. Let me know if this is what you want and I can come up a sketch pretty quickly.

      Like

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s