   if(p == -1) return;
   pinMode(p, INPUT);
   int rval = analogRead(p);
-  char m[8];
   char v [12];
-  sprintf(m, "%03d", rval);
-  sprintf(v,"#ar;%s;%i;!",pin,rval);
+  sprintf(v,"#ar,%s,%i!",pin,rval);
   Serial.println( v );
 }
 // Analog write

/*
 * JAAF es un micro-framework para comunicar placas Arduino
 * con computadoras mediante comunicación serial.
 */

#include <Servo.h>

Servo servos[48];

#define hash 0x23
#define semicolon 0x3b 
#define tagLength 5

int tagIndex = 0;

char com[tagLength], val[tagLength], pin[tagLength];

bool comComplete = false;
bool valComplete = false;
bool pinComplete = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("READY");
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0) {
    delay(15);
    readData();
  }
}

void readData(){
  char thisChar = Serial.read();
  
  if(thisChar == hash){
     while(comComplete == false && Serial.available() > 0){
        char thisChar = Serial.read();
        if(thisChar == semicolon){
           comComplete = true;
           break;
        }
        else {
          com[tagIndex] = thisChar;
          tagIndex++;
        }
     }
     tagIndex = 0;
     while(pinComplete == false && Serial.available() > 0){
        char thisChar = Serial.read();
        if(thisChar == semicolon){
          pinComplete = true;
          break;
        }
        else {
          pin[tagIndex] = thisChar;
          tagIndex++;
        }
     }
     tagIndex = 0;
     while(valComplete == false && Serial.available() > 0){
        char thisChar = Serial.read();
        if(thisChar == semicolon){
          valComplete = true;
          break;
        }
        else {
          val[tagIndex] = thisChar;
          tagIndex++;
        }
     }
     tagIndex = 0;
     
     if(comComplete && valComplete && pinComplete){
       
       if(strcmp(com,"pm")==0){    // pin mode
         pm(pin, val);             // 0 = input; 1 = output
       }
       
       if(strcmp(com,"dr")==0){    // digital read
         dr(pin);
       }
       if(strcmp(com,"dw")==0){    // digital write
         dw(pin, val);             // 0 = low; 1=high;
       }
       
       if(strcmp(com,"ar")==0){    // analog read
         ar(pin);
       }
       if(strcmp(com,"aw")==0){    // analog write
         aw(pin, val);
       }
       
       if(strcmp(com,"as")==0){    // attach servo
         as(pin);
       }
       
       if(strcmp(com,"ds")==0){    // detach servo
         ds(pin);
       }
       
       if(strcmp(com,"st")==0){    // servo to position
         st(pin, val);
       }
       
       // Serial.print("com: ");Serial.print(com);Serial.print("; pin: ");Serial.print(pin);Serial.print("; val: ");Serial.println(val);
     }
     for (int i=0;i<5;i++){
       com[i] = 0x00;
       pin[i] = 0x00;
       val[i] = 0x00;
     }
     
     comComplete = false;
     pinComplete = false;
     valComplete = false;
  }
}

// Pinmode
void pm(char *pin, char *val){

// Attach servo
void as(char *pin){
  int p = atoi(pin);
  j_servos[p].attach(p);
}
// Detach servo
void ds(char *pin){
  int p = atoi(pin);
  j_servos[p].detach();
}
// Servo to position
void st(char *pin, char *val){
  int p = atoi(pin);
  int pos = atoi(val);
  if(pos >= 0 && pos <= 180){
    j_servos[p].write(pos);
  }
}

