Arduino JoyStick Module

From Geeetech Wiki
Revision as of 00:58, 24 February 2012 by Admin (talk | contribs) (corresponding code)

Jump to: navigation, search

Introduction

Just like a joystick on game console. you can control x, y and z dimensions input by this joystick module. It can be considered as combination of potentiometers and one button. Data type of the x, y dimensions are analog input signals and z dimension is digital input signal. thus the x and y ports connect to analog pins of Sensor Shield, while z port connects to digital pin.

How this works

The joystick in the picture is nothing but two potentiometers that allow us to messure the movement of the stick in 2-D. Potentiometers are variable resistors and, in a way, they act as sensors providing us with a variable voltage depending on the rotation of the device around its shaft.


How to use

When using, it can connect with Arduino sensor shield,and connect Arduino corresponding pins through Arduino sensor cables. In the example below, the X axes and Y axes are respectively link to the analog input A1 and A0 ,and Z axis is connected to the digital I/O 7 pin:


corresponding code

int sensorPin = 5;
int value = 0;
void setup() {
 pinMode(7, OUTPUT);
 Serial.begin(9600);
}
void loop() {
 value = analogRead(0);
 Serial.print("X:");
 Serial.print(value, DEC);
 value = analogRead(1);
 Serial.print(" | Y:");
 Serial.print(value, DEC);
 value = digitalRead(7);
 Serial.print(" | Z: ");
 Serial.println(value, DEC);
 delay(100);
}

Referring Sample Code : int JoyStick_X = 0; //x int JoyStick_Y = 1; //y int JoyStick_Z = 3; //key void setup()

{ 
 pinMode(JoyStick_X, INPUT); 
 pinMode(JoyStick_Y, INPUT); 
 pinMode(JoyStick_Z, INPUT); 
 Serial.begin(9600); // 9600 bps

} void loop() {

     int x,y,z;
     x=analogRead(JoyStick_X);
     y=analogRead(JoyStick_Y);
     z=digitalRead(JoyStick_Z);
     Serial.print(x ,DEC);
     Serial.print(",");
     Serial.print(y ,DEC);
     Serial.print(",");
     Serial.println(z ,DEC);
     delay(100);

}