Joystick

Interfacing a Joystick

Hardware Required
  • ARDUINO UNO
  • Connecting Wires
  • Joystick
About ARDUINO
Arduino is a popular open-source development board for engineers and makers to develop electronics projects in an easy way. It consists of both a physical programmable development board (based on AVR series of microcontrollers) and a piece of software or IDE which runs on your computer and used to write and upload the code to the microcontroller board.

About Joystick
joystick is an input device consisting of a stick that pivots on a base and reports its angle or direction to the device it is controlling. A joystick, also known as the control column, is the principal control device in the cockpit of many civilian and military aircraft, either as a center stick or side-stick. It often has supplementary switches to control various aspects of the aircraft's flight.
Connections
  • Connect VCC to VCC
  • Connect GND to GND
  • Connect VRX to analog pin(A0)
  • Connect VRY to analog pin(A1)
Tutorial About
This tutorial is about using the joystick to move the robot forwards, backwards, right, left.

Circuit Diagram
Code

int x = 0;
int y = 0;
void setup() 
{
Serial.begin(9600);// initialize serial communication
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop()
 {
  x = analogRead(A1);   
   if (((( x ) > (1021) ) ) && ((( x ) < (1024) ) )) //Moving the robot forward
 digitalWrite(12, HIGH); 
  digitalWrite(17, LOW);
 digitalWrite(10, HIGH); 
digitalWrite(11, LOW);
 }
if (((( x ) > (501) ) ) && ((( x ) < (504) ) ))//Stopping the robot if the joystick isn't moved
 {
digitalWrite(12, HIGH);
digitalWrite(17, HIGH); 
digitalWrite(10, HIGH); 
digitalWrite(11, HIGH);
 } 
  if (((( y ) > (1021) ) ) && ((( y ) < (1024) ) ))//Moving the robot left
 { 
 digitalWrite(12, HIGH);
 digitalWrite(13, HIGH);
 digitalWrite(10, HIGH);
 digitalWrite(11, LOW);
 }
 if ((( y ) == (0) ) )//Moving the robot Right
  { 
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
digitalWrite(10, HIGH); 
digitalWrite(11, HIGH); 
  if (((( x ) > (1021) ) ) && ((( x ) < (1024) ) ))
 {
  digitalWrite(12, HIGH); 
  digitalWrite(13, LOW);
 digitalWrite(10, HIGH); 
digitalWrite(11, LOW);
 }
 if ((( x ) == (0) ) )// Moving the robot forward
  {  
digitalWrite(12, LOW);  
digitalWrite(13, HIGH); 
digitalWrite(10, LOW); 
digitalWrite(11, HIGH);
 }  
 y = analogRead(A2); 
  Serial.println("y:");
  Serial.println(y);  
}
Code explanation

In the loop, we'll declare that the variable x holds the input of the analog pin 0 and the variable y holds the input of analog pin 1. So whenever we want to give a condition to the controls of the joystick we can use the variables.
To know the value of the joystick of the selected position we'll serial print the values.
 Serial.println("y:");
 Serial.println(y);
  ^These commands are used for printing the values for y(horizontal controls). the same can be done for x(vertical control).


No comments:

Post a Comment