what an earth is this bit ?

hi guys i have been looking at starting to use joysticks with arduino and come across this on the arduino page, i under stand the code to a certain extent, but there is one part that makes anbsalutely no sense to me at all, and was wondering if anyone could shed some light.

this bit - ( int treatValue(int data) { return (data * 9 / 1024) + 48; )

 /* 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
  Serial.begin(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);
  Serial.print('J');
  Serial.print(treatValue(value1));
  Serial.println(treatValue(value2));
 }

That is a function definition. It takes an int as an argument, does some math with it, and returns the result.

SO if you were to write:

int someVariable = treatValue(200);

then someVariable would now have the value 49. (200 * 9 = 1800 then 1800 / 1024 = 1 then 1 + 48 = 49.

It is a fairly poorly written function it seems. Looks like some of that should have involved some floating point calculation.

What that function tries to do is convert the entire range of the ADC reading of 0 to 1023, into an ASCII character from 0 to 9. It might, for example, send the message J5148. But it will never produce a 9. If a 9 is also to be included, the function should be this
data * 10 / 1024

It also uses the raw ADC readings as the value of delays when flashing the LED. The maximum ADC reading is, of course, 1023 which means that the rate at which the ADCs are read is highly variable and depends upon the values of the ADCs themselves.

Pete

marcjwebb:
hi guys i have been looking at starting to use joysticks with arduino and come across this on the arduino page, i under stand the code to a certain extent, but there is one part that makes anbsalutely no sense to me at all, and was wondering if anyone could shed some light.

this bit - ( int treatValue(int data) { return (data * 9 / 1024) + 48; )

As data is an analogRead() value in the range 0..1023, the integer expression (data * 9 / 1024) is an integer value in the range 0..8, and if you add 48, the returned value is in the range 48..56, which is the range of the ASCII-characters '0'...'8'.

The comment shows, that the format for sending should be "Jxy\r\n", so that possibly ASCII chars should be sent like perhaps a string like "J01\r\n".

But this is not true with that code:

Serial.print('J');
  Serial.print(treatValue(value1));
  Serial.println(treatValue(value2));

and possibly returned int value==48 (ASCII code '0') and returned int value==49 (ASCII code '1') it is actually sent "J4849\r\n"