0

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?

15
  • 1
    This is not c, althought most arduino programmers think it is apparently. Commented Feb 3, 2016 at 17:36
  • @iharob Ok, I changed the tag to "processing" programming language. Commented Feb 3, 2016 at 17:41
  • Change Serial.write(incomingByte), to Serial.print(incomingByte). Does it works? write expects single character. Commented Feb 3, 2016 at 17:44
  • 1
    My mistake. In the else clause, separate the string from the variable, use two separate Serial println and Serial print. The way you want to do would need a sprintf in between and would consume more resources. Commented Feb 3, 2016 at 17:55
  • 1
    @CPA, Arduino is really similar to C, so I recommend learning C. I believe the most important difference is that while you can practice C in your computer that has almost unlimited resources, your Arduino can be maxed pretty fast, requiring some caution in the development. Also you usually can find tutorials, materials, and courses on C easier than the same material on Arduino and usually they are more clear when explaining why and how things works. Commented Feb 3, 2016 at 18:32

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.