A Handy Serial Library

Introduction

Arduino hadn’t debugging function, maybe this is the first impression of some software engineers. In fact, often we just use the Arduino to do some simple application, even there’s no debugging, and it does not matter. If you want to use the Arduino to do some complex application, such as face recognition, then maybe you have to rethink if you had chosen the wrong platform.

As for some simple application, we can use serial print to debug, and Arduino had provided a very easy to use serial print function.

void setup()
{
    Serial.begin(115200);

    Serial.println("hello world");
}

void loop()
{
    // add code here
}

Besides, there is Serial.print, Serial.write and so on. When you are familiar with these functions, you will find that these function is not so friendly actually, just have a look at the following code:

void setup()
{
    Serial.begin(115200);

    Serial.print("a[");
    Serial.print(3);
    Serial.print("] = ");
    Serial.println(5);

}

void loop()
{
    // add code here
}

To print a[3]=5, it can take 4 lines of code, troublesom? Remember the C language lessons, it takes only one line of code:

printf("a[%d] = %d", 3, 5);

As for C++, one line is enough also:

cout << "a[" << 3 << "] = " << 5 << endl;

I will glad that if Arduino has the function such as printf or cout, it’s really convenient. It’s lucky that some guy had written such library, you can refer to http://arduiniana.org/libraries/streaming/ I made some small change to this library, you can download here: https://github.com/loovee/Streaming , why not have a try?

Resources

Help us make it better

Welcome to the new documentation system of Seeed Studio. We have made a lot of progress comparing to the old wiki system and will continue to improve it to make it more user friendly and helpful. The improvement can't be done without your kindly feedback. If you have any suggestions or findings, you are most welcome to submit the amended version as our contributor via Github or give us suggestions in the survey below, it would be more appreciated if you could leave your email so that we can reply to you. Happy Hacking!