Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Serial debug output looking messy

I've got this quite basic code running on an Arduino Mega 2560, and the output is looking like I have the serial settings wrong, though I can't work out what to change.

void setup() 
{
 Serial.begin(9600);
 Serial.println("test123");
}
void loop()
{
 Serial.println("test" + millis());
}

The output looks like this:

×ばつ¤¿ïßûÏûýoÏ¿îãõI
õfwi÷`p¬1⁄2Ïa»QáÀa»¦·ð¦·Z©÷
test123
D
4ñjü ̄÷~μÞMz1Ý~1⁄4
Ûþ}iû}ÒÖ­¦Ç¿÷ÿÂï1⁄2Õ3⁄4;îö

I'm using the inbuilt "Serial Monitor" in the Arduino suite with settings 9600 baud and no line endings.

I see that the first println in setup() is sending the data as expected but all other comms are coming through as nonsense.

Any suggestions of what to do?

Answer*

Draft saved
Draft discarded
Cancel
4
  • Great, thank you. Can you also explain why string concatenation per arduino.cc/en/Tutorial/StringAdditionOperator isn't working how I expected it would in this instance, when one of their examples on the page is specifically stringThree = stringOne + millis();? Commented Dec 25, 2014 at 10:38
  • 2
    @bdx You can concatenate String objects like that because they overload the plus (+) operator. String literals in C/C++ (anything in double quotation marks "like this") work differently. You can read more about the difference here: arduino.cc/en/Reference/String Commented Dec 25, 2014 at 17:33
  • I see. My assumption that a string literal would create a String object was false, it creates a null-terminated character array, and the String type also has an overload on the = operator to coerce a char[] to a String. Commented Dec 25, 2014 at 20:34
  • You can explicitly construct a String object by doing this: String("test"). It uses more memory than a string literal alone though, so it's important to be careful how you use it. Commented Dec 25, 2014 at 22:49

AltStyle によって変換されたページ (->オリジナル) /