/* Input Sensor Box Simplified
 * ---------------------------
 *
 * Uses Arduino as a sensor box external to the computer
 * it allows to read both the digital ports, and the analog 
 * ones. By default it makes a polling of all the inputs and 
 * retrieves them to the serial port. It will be sent in the 
 * format:
 *
 * {XXXXXXXXXXNNNNNN}
 *
 * X --> H|L, value for pins 2 - 12
 * N --> 0..8, value for the input
 * H --> High
 * L --> Low
 * 0..8 --> We encode any analog sensor in values between 0 and 8
 *          (this can be changed until 1024 values per sensor)
 *
 * http://www.0j0.org
 * copyleft 2005 DojoDave for DojoCorp
 */
 
// FIXME: the analog pins are shifted one position, 0 is 6 ... why? with the joystick it works fine!!

int ledPin = 13;
int digRead = 0;
int anaRead = 0;

void setup() 
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  beginSerial(9600);
}


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

void serialEOLN(void) {
  serialWrite(10);
  serialWrite(13);
}

void blinkLED(void) {
  digitalWrite(ledPin, HIGH);
  delay(250);
  digitalWrite(ledPin, LOW);
}

void loop() 
{
  for (int count = 2; count <= 9; count++) {
    digRead = digitalRead(count);
    if (digRead == HIGH) {
      serialWrite('H');
    } else {
      serialWrite('L');
    }
  }
  for (int count = 0; count <= 2; count++) {
    digRead = digitalRead(count + 10);
    if (digRead == HIGH) {
      serialWrite('H');
    } else {
      serialWrite('L');
    }
  }
  for (int count = 0; count <= 5; count++) {
    anaRead = analogRead(count);
    delay(100);
    serialWrite(treatValue(anaRead));
  }
  serialEOLN();
  delay(500);
  blinkLED();
}