I have a very short example program running on an Arduino Fio device. This program is sending serial data. A connected Xbee router device is receiving this data and sending it to a Xbee coordinator device connected to my notebook. The program also reading data from the serial port. I can send a 1 or a 0 to switch the LED of the Fio device on or off.
Switching the LED on or off by sending a 1 or a 0 from a terminal on my notebook is working well.
But when I try to read the data which the Fio device is sending, I get this:
his direction works
is direction works
s direction works
direction works
direction works
irection works
rection works
ection works
ction works
... and so on.
But I'm expecting a string ("This direction works " + counter++;) as you can see in the following code example.
Here is the short Arduino sketch:
int incomingByte = 0; // for incoming serial data
int counter = 0;
void setup()
{
Serial.begin(57600);
pinMode(13,OUTPUT);
// blink twice at startup
digitalWrite(13, LOW);
delay(1000);
digitalWrite(13, HIGH); // first blink
delay(50);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH); // second blink
delay(50);
digitalWrite(13, LOW);
}
void loop()
{
// send data only when you receive data:
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
if(incomingByte == '0')
{
digitalWrite(13, LOW);
}
else if(incomingByte == '1')
{
digitalWrite(13, HIGH);
}
// say what you got:
Serial.print("Fio received: ");
Serial.write(incomingByte); // Arduino 1.0 compatibility
Serial.write(10); // send a line feed/new line, ascii 10
}
else
{
String sendData = "This direction works " + counter++;
Serial.println(sendData);
delay(1500);
}
}
What I'm doing wrong? Why do I don't get:
This direction works 0
This direction works 1
This direction works 2
This direction works 3
... and so on?