/* 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 PC: 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
 * 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 / 256) + 48;
}

void loop() 
{
  value1 = analogRead(joyPin1);   // reads the value of the variable resistor 
  delay(100);			  // this small pause is needed between reading two
				  // analog pins, otherwise we get the same value twice
  value2 = analogRead(joyPin2);   // reads the value of the variable resistor 
                                     
  digitalWrite(ledPin, HIGH);           
  delay(value1);
  digitalWrite(ledPin, LOW);
  delay(value2);
  serialWrite('J');
  serialWrite(treatValue(value1));
  serialWrite(treatValue(value2));
  serialWrite(10);
  serialWrite(13);
}