CPA
CPA

Reputation: 3053

String concatenation with arduino

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?

Upvotes: 0

Views: 1176

Answers (1)

CPA
CPA

Reputation: 3053

As @Elric and @Olaf mentioned in the comments, it is not possible to do that like in my posted code.

After reading the documentation again I found an "explanation":

Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:

int sensorValue = analogRead(A0); String stringOne = "Sensor value: "; String stringThree = stringOne + sensorValue;
Serial.println(stringThree);

results in "Sensor Value: 402" or whatever the analogRead() result is, but

int sensorValue = analogRead(A0); String stringThree = "Sensor value: " + sensorValue; Serial.println(stringThree);

gives unpredictable results because stringThree never got an initial value before you started concatenating different data types.

Firstly you have to initialize the String variable to assign two concatenated strings to it.

Upvotes: 1

Related Questions