JOYSTICK

What is a joystick?

A joystick is a type of cap that turns 360 degrees and enables us to control a multitude of devices. They are commonly found on many devices, especially video consoles. They come in very handy for simple control of our robots or other projects! Want to know how they work? Let´s go!

joystick

It may seem like a highly complicated component but actually it isn´t. Let´s do something… grab your joystick and take off the plastic cap that allows you to control it. Then take a good look:

joysticksincapucha

Don´t you remember these two black components? They look a lot like your potentiometer! Yes, that´s right – a joystick is nothing more than two potentiometers joined together. One to control of the X axis and another to control the Y axis. This joystick also has a button so that if you press the cap, even more things can happen.

Two potentiometers and one push-button! And we can program them exactly how we like! Let´s find out how!

Today you will need:

  1. A joystick module
  2. An LCD screen
  3. A ZUM BT 328 controller board or one compatible with Arduino UNO
  4. A USB cable

Example 1

Displaying the joystick values on screen

We will use the LCD screen to display the values of the joystick; the X axis and the Y axis of its button.  To do this, connect the joystick and the LCD as follows:

ZUM-LCD-Joystick

Now look carefully at how the program was made in bitbloq:

As with other sensors we have looked at before, we need to assign a variable to the joystick to store its values. In the example, we have called it joystick and this variable is a bit special… It stores three values! These variables are known as arrays or vectors  and they can be used to store various values in one variable, such as the X, Y and Z coordinates on a graph, the hours, minutes and seconds of a clock, or in our case the X and Y coordinates and the button of our joystick.

As you can see, we declare and assign the variable to the joystick and then we can start writing the information on the LCD screen. To access the X and Y coordinates or the button value of the joystick, we need to access one of the three elements of the variable. For this, you can use the var variable[index] block where the index corresponds to the element that we want to access.

There´s something really important to remember when working with vectors. The first element of a vector corresponds not to index 1, but in fact, to index 0. This means that if you want to access the first element of a vector,  you need to put 0 instead of 1, and if you want to access the second, you put 1 instead of 2, etc. The diagram will help make this clearer for you:

Pay attention to the values of your joystick, which, in theory, should give you values between 0 and 1023, as with the potentiometer. However, it is actually very difficult to reach these values and it will depend on your joystick. This is due to the physical limitations of the joystick itself. Remember to take the maximum values of your joystick into account. If your project requires high precision, this will allow you to program it better.

Here´s another example using the joystick:

Example 2

Varying the blink speed of an LED with the joystick

Remember how we varied the blink speed using the potentiometer? Well, that time the waiting time between switching off and making the LED blink had to be the same… But now with the joystick, we have two potentiometers in one! So let´s carry out a little experiment so that value X determines how long the LED stays on for and value Y determines how long it remains off.

Connect the joystick as the shown in the previous diagram and add an LED to pin digital number 6. Have a go yourself! We recommend mapping the joystick value between 0 and 500 to make the effect stand out (though this is not strictly required). You can check out the solution below whenever you want.
reloj-de-arena-10375Did you manage it? It should end up looking something like this:

As you can see, the blinking times vary according to the position of the joystick. Play around with different positions and you can also change the mapping values to make it faster or slower.

Joysticks can be used for a wide variety of applications, from controlling motors or any other element or device. Experiment with it and it will soon be one of your favourite components.