PROJECT Nº 2: Program the joystick

Build and code a circuit to control the analog joystick included in the Arm Robot kit. Move the joystick in both of its axis and when you release it, its position values will be displayed on the screen.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE:  20 min.

MATERIALS:

  • 1 Joystick
  • 1 Build&Code UNO board
  • 1 Sensor Shield

What is an analog joystick?

The joystick is an analog controller that can be moved around 360º and allows you to control many devices. It is mainly used in video game consoles but you can also use it to easily maneuver robots or other kind of machines.

A joystick is composed of a sway system with 2 orthogonal axis attached to 2 potentiometers. These potentiometers measure the joystick X and Y axis position.

One of the axis is set over a micro switch, which allows you to detect when the joystick is pressed down.

A joystick gives an analog signal for each axis and a digital signal when the it is pressed.

joystick-analogico-actividad-2

CONNECTIONS:

  1. Connect the joystick to the Build&Code sensor shield, which should be connected to the Build&Code UNO board, as indicated in step 14 of the Arm Robot user manual.
  2. In order to give power to the joystick, connect the Vcc output (red cable) to the V pin of the A0 analog port. Also, connect the GND output to the G pin of the Build&Code analog port.
  3. Connect the VRx output to the S pin of the A0 analog port (yellow cable). Also, connect the VRy output to the S pin located in the A1 analog port of the Build&Code UNO (green cable)
  4. In order to check that the connections are correct, go to the Connections section, and Joysticks Connection subsection of the Arm Robot Manual.

joystick-analogico-actividad-2-colores

joystick-activity-2

PROGRAMMING CODE

You can do this project using the Arduino program or a visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code

Before starting to assemble the Arm Robot, follow these steps:

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following program in it:
    #include <Servo.h>
    
    int valueX = 0; // X-AXIS LECTURE
    int valueY = 0; // Y-AXIS LECTURE
    int pinJX = A0; // X-AXIS CONNECTED TO ANALOG PIN A0
    
    
    int pinJY = A1;  // Y-AXIS CONNECTED TO ANALOG PIN A1
    
    void setup() {
    
    Serial.begin (9600); // ACTIVATE COMMUNICATION BY SERIAL PORT
    
    }
    void loop() {
    
    
    valueX = analogRead ( pinJX);// ANALOG A0 READING FOR X-AXIS
    valueY = analogRead ( pinJY); // ANALOG A1 READING FOR Y-AXIS
    
    Serial.print ("X: ");
    Serial.print (valueX); // PRINT ONSCREEN THE X-AXIS VALUES FROM 0 TO 1023
    Serial.print("\t");
    Serial.print ("Y: ");
    Serial.println (valueY);// PRINT ONSCREEN THE Y-AXIS VALUES FROM 0 TO 1023
    }
    
  3. To see the values on screen, click on the lens icon and a window will open:


    If you move the joystick to the right, left, up or down, you will see how the X and Y values change from 0 to 1023.Example:

Code for the visual programming software by blocks compatible

  1. Download and install the program.
    1.1 Open the software.
    1.2 Configure the program to save code into the Build&Code UNO board. You will find the instructions in the Arm Robot First Steps guide.
  2. Open the program and copy the following code. Use the following image as a guide:

RESULT OF THE EXERCISE

When moving the joystick to the right, left, up and down, you will see how the variables in X and Y change between 0 and 1023.

This is the first step to understand how to program the joystick and which kind of data are we receiving, to program the Arm Robot so that is makes the movements we want.

1 0

0