charserialbuf[32];//This gives the incoming serial some room. Change it if you want a longer incoming.

voidsetup(){

pinMode(in_LED,OUTPUT);

pinMode(out_LED,OUTPUT);

pinMode(input,INPUT);

Serial.begin(9600);

xbee_serial.begin(9600);

xbee_serial.print("0,0,0.");

}

voidloop(){

if(xbee_serial.available()>0){

staticintbufpos=0;

charinchar=xbee_serial.read();

if(inchar!=EOPmarker){

serialbuf[bufpos]=inchar;

bufpos++;

}

else{

serialbuf[bufpos]=0;//restart the buff

bufpos=0;//restart the position of the buff

Serial.println(atoi(subStr(serialbuf,",",1)));

analogWrite(in_LED,atoi(subStr(serialbuf,",",1)));

sendval=map(analogRead(input),0,1023,0,255);

xbee_serial.print(sendval);// Value that it sends over the serial

xbee_serial.print(",");

xbee_serial.print("100");//This second byte is for the purpose of the program, it is not being used.

xbee_serial.print(".");//EOP marker

analogWrite(out_LED,sendval);

}

}

delay(10);

}

char*subStr(char*input_string,char*separator,intsegment_number){

char*act,*sub,*ptr;

staticcharcopy[MAX_STRING_LEN];

inti;

strcpy(copy,input_string);

for(i=1,act=copy;i<=segment_number;i++,act=NULL){

sub=strtok_r(act,separator,&ptr);

if(sub==NULL)break;

}

returnsub;

}

Hey! This post was written a long time ago, but I'm leaving it up on the off-chance it may help someone, but proceed with caution. It may not be a good idea to blindly integrate this code or work into your project, but instead use it as a starting point.