PaulS:
But your suggestion is not to have any of the arduinos hooked up to the computer while these two are communicating between each other?
You'll have a hard time debugging anything that way. It was more of a hint for you to tell us how the XBees are connected to the Arduinos.
Really strange behavior when I tried to read from serial from the Xbee but finally I solved it..
I realized that along with the integers I sent the Xbee somehow read alot of other junk. Just by one reading from the serial buffer this lines popped up:
126
0
16
144
0
19
162
0
64
113
166
165
201
192
1
49
48
48
48
115
Anyway, before I sent my message,on the router-side I sent only one character "s", to indicate the start for my "message", and on the coordinator-side if the first thing in the buffer is an "s", then I read the whole incoming message. Code is below if anyone is intrested or have any similar problems.
My purpose with this project was to be able to control four different led's with Xbee communication and a glove with flexsensors on.
Router-side:
int flexSensorPin1 = A0;
int flexSensorPin2 = A1;
int flexSensorPin3 = A2;
int flexSensorPin4 = A3;
char s_str1[2];
char s_str2[2];
char s_str3[2];
char s_str4[2];
String str1,str2,str3,str4;
void setup(){
Serial.begin(9600);
}
void loop(){
int flexS1 = analogRead(flexSensorPin1);
int flexS2 = analogRead(flexSensorPin2);
int flexS3 = analogRead(flexSensorPin3);
int flexS4 = analogRead(flexSensorPin4);
if (flexS1 < 620) flexS1 = 620;
else if (flexS1 > 690) flexS1 = 690;
if (flexS2 < 390) flexS2 = 390;
else if (flexS2 > 450) flexS2 = 450;
if (flexS3 < 580) flexS3 = 580;
else if (flexS3 > 710) flexS3 = 710;
if (flexS4 < 640) flexS4 = 640;
else if (flexS4 > 690) flexS4 = 690;
int flex_1_0to100 = map(flexS1, 690, 620, 9, 0);
int flex_2_0to100 = map(flexS2, 450, 390, 9, 0);
int flex_3_0to100 = map(flexS3, 710, 580, 9, 0);
int flex_4_0to100 = map(flexS4, 690, 640, 9, 0);
str1=String(flex_1_0to100);
str1.toCharArray(s_str1,2);
str2=String(flex_2_0to100);
str2.toCharArray(s_str2,2);
str3=String(flex_3_0to100);
str3.toCharArray(s_str3,2);
str4=String(flex_3_0to100);
str4.toCharArray(s_str4,2);
Serial.print("s");
Serial.print(s_str1);
Serial.print(s_str2);
Serial.print(s_str3);
Serial.print(s_str4);
delay(100);
}
Coordinator-side
// Coordinator!
int ledPins[]={9,10,11,12};
int l_pin=sizeof(ledPins);
void setup()
{
for (int i=0;i<l_pin;i++){
pinMode(ledPins*, OUTPUT); *
-
}*
-
Serial.begin(9600);*
}
void loop()
{
-
if (Serial.available() >=5 ) {*
-
// read the oldest byte in the serial buffer:*
-
char incomingByte1 = Serial.read();*
-
char incomingByte2 = Serial.read();*
-
char incomingByte3 = Serial.read();*
-
char incomingByte4 = Serial.read();*
-
char incomingByte5 = Serial.read();*
// Serial.flush();
-
if(incomingByte1=='s'){*
-
int val_1 = incomingByte2-'0';*
-
int val_2 = incomingByte3-'0';*
-
int val_3 = incomingByte4-'0';*
-
int val_4 = incomingByte5-'0';*
-
Serial.println(val_1);*
-
Serial.println(val_2);*
-
Serial.println(val_3);*
-
Serial.println(val_4);*
-
Serial.println();*
-
if(val_1==0){*
-
analogWrite(ledPins[0],HIGH);*
-
}*
-
else{*
-
analogWrite(ledPins[0],LOW);*
-
}*
-
if(val_2==0){*
-
analogWrite(ledPins[1],HIGH);*
-
}*
-
else{*
-
analogWrite(ledPins[1],LOW);*
-
}*
-
if(val_3==0){*
-
analogWrite(ledPins[2],HIGH);*
-
}*
-
else{*
-
analogWrite(ledPins[2],LOW);*
-
}*
-
if(val_4==0){*
-
analogWrite(ledPins[3],HIGH);*
-
}*
-
else{*
-
analogWrite(ledPins[3],LOW);*
-
}*
-
}*
-
}*
-
delay(10);*
}