Tutorial.JoyStick History

Show minor edits - Show changes to markup

November 01, 2013, at 07:58 PM by Scott Fitzgerald -
Added lines 19-20:
Changed line 45 from:
  beginSerial(9600);
to:
  Serial.begin(9600);
Changed lines 65-69 from:
  serialWrite('J');
  serialWrite(treatValue(value1));
  serialWrite(treatValue(value2));
  serialWrite(10);
  serialWrite(13);
to:
  Serial.print('J');
  Serial.print(treatValue(value1));
  Serial.println(treatValue(value2));
Changed lines 69-70 from:
to:
September 22, 2005, at 09:49 AM by 195.178.229.112 -
Changed line 3 from:

The Joystick:

to:

The Joystick

Changed lines 6-7 from:

Schematic:

to:

Schematic

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.

The kind of program that we need to monitor the joystick has to make a polling to two of the analog pins. We can send these values back to the computer, but then we face the classic problem that the transmission over the communication port has to be made with 8bit values, while our DAC (Digital to Analog Converter - that is messuring the values from the potentiometers in the joystick) has a resolution of 10bits. In other words this means that our sensors are characterized with a value between 0 and 1024.

The following code includes a method called treatValue() that is transforming the sensor's messurement into a value between 0 and 9 and sends it in ASCII back to the computer. This allows to easily send the information into e.g. Flash and parse it inside your own code.

Finally we make the LED blink with the values read from the sensors as a direct visual feedback of how we control the joystick.

 /* Read Jostick
  * ------------
  *
  * Reads two analog pins that are supposed to be
  * connected to a jostick made of two potentiometers
  *
  * We send three bytes back to the comp: one header and two
  * with data as signed bytes, this will take the form:
  *		Jxy\r\n
  *
  * x and y are integers and sent in ASCII 
  * 
  * http://www.0j0.org | http://arduino.berlios.de
  * copyleft 2005 DojoDave for DojoCorp
  */

 int ledPin = 13;
 int joyPin1 = 0;                 // slider variable connecetd to analog pin 0
 int joyPin2 = 1;                 // slider variable connecetd to analog pin 1
 int value1 = 0;                  // variable to read the value from the analog pin 0
 int value2 = 0;                  // variable to read the value from the analog pin 1

 void setup() {
  pinMode(ledPin, OUTPUT);              // initializes digital pins 0 to 7 as outputs
  beginSerial(9600);
 }

 int treatValue(int data) {
  return (data * 9 / 1024) + 48;
 }

 void loop() {
  // reads the value of the variable resistor 
  value1 = analogRead(joyPin1);   
  // this small pause is needed between reading
  // analog pins, otherwise we get the same value twice
  delay(100);			  
  // reads the value of the variable resistor 
  value2 = analogRead(joyPin2);   

  digitalWrite(ledPin, HIGH);           
  delay(value1);
  digitalWrite(ledPin, LOW);
  delay(value2);
  serialWrite('J');
  serialWrite(treatValue(value1));
  serialWrite(treatValue(value2));
  serialWrite(10);
  serialWrite(13);
 }

@idea: the order of the blinking LED

@code: David Cuartielles

@pictures and graphics: Massimo Banzi

@date: 20050820 - Malmo - Sweden

August 28, 2005, at 03:45 PM by mbanzi -
Added lines 3-5:

The Joystick:

August 28, 2005, at 03:44 PM by mbanzi -
Added lines 1-4:

Interfacing a Joystick

Schematic:

Share