Difference between revisions of "Arduino joystick"

From Geeetech Wiki
Jump to: navigation, search
Line 14: Line 14:
  
  
  Relating code;
+
  Relating code
  
 
  int sensorPin = 7;
 
  int sensorPin = 7;

Revision as of 08:24, 2 March 2012

Getting started with Arduino joystick module

This is the first time I come into contact with Arduino, which is an open-source electrics platform based on flexible, easy-to-use hardware and software. As a beginner to these stuffs which can perform so many useful functions is a fascinating matter. Arduino also has an open-source software which is similar to C Language.

No matter who you are, a learner or an engineer, you can use it to achieve your creative desire.Making a string of led flicker for a set period of time is quite a good project for the beginners to learn how it functions. But here we do another small experiment as a beginning in which we can read the 3-axis dimensions from a ps2 joystick module in console by USB serial port.

All we need is a joystick module, some jumper wires and most important Arduino UNO which is a great starter Arduino, it provides a solid foundation for those just getting started. We can connect the module to the Arduino board according to the photo below.

As the joystick is moved in any direction, X port and Y port on the joystick board ouput X and Y dimension of analog signal as voltage whose value is in the range of 0 to 5V, the Readanalog() command can convert the analog value to digital value in the range of 0 to 1023. Z port on joystick board output Z dimension of digital signal when pressed. So X port and Y port of the joystick need to be connected to the analog input pin0 and pin1 on UNO respectively. Z port needs to be connected to the digital pin7.

X, Y and Z dimension input to the computer is by means of USB port. Now we can upload and click the serial monitor later to see what’s going on, X,Y and Z dimension from joystick module is showing up in the monitor constantly.

Serial.begin(9600) command means that we setup Arduino with the transfer rate, in the case 9600bits per second. Please notice the board and serial port should be correctly selected in tools menu. And the baud rate we select in monitor must be the same as the baud in the code above, otherwise the monitor would show a series of messy codes


Relating code
int sensorPin = 7;
int value = 0;
void setup() {
pinMode(sensorPin, 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(sensorPin);
Serial.println(value, DEC);
Serial.print(” | Z: “);
delay(300);
}