Streaming

Streaming C++-style Output with Operator <<

Note: Streaming 5 is now Arduino 1.0 compatible.

New users sometimes wonder why the “Arduino language” doesn’t provide the kind of concatenation or streaming operations they have become accustomed to in Java/VB/C#/C++, etc.

// This doesn't work in Arduino.  Too bad.
lcd.print("The button was pressed " + counter + " times");

Meanwhile, experienced programmers chafe at having to synthesize streams with clumsy blocks of repetitive code like this:

lcd.print("GPS #");
lcd.print(gpsno);
lcd.print(" date: ");
lcd.print(day);
lcd.print("-");
lcd.print(month);
lcd.print("-");
lcd.println(year); // ugh!!

The Streaming library gives you the option of compressing those into “insertion style” code that, if not exactly the same, is reminiscent of the concatenation above:

lcd &lt;&lt; "GPS #" &lt;&lt; gpsno &lt;&lt; " date: " &lt;&lt;
    day &lt;&lt; "-" &lt;&lt; month &lt;&lt; "-" &lt;&lt; year &lt;&lt; endl;

This library works for any class that derives from Print:

Serial &lt;&lt; "Counter: " &lt;&lt; counter;
lcd &lt;&lt; "Temp: " &lt;&lt; t.get_temperature() &lt;&lt; " degrees";
my_pstring &lt;&lt; "Hi Mom!" &lt;&lt; endl;

With the new library you can also use formatting manipulators like this:

Serial &lt;&lt; "Byte value: " &lt;&lt; _HEX(b) &lt;&lt; endl;
lcd &lt;&lt; "The key pressed was " &lt;&lt; _BYTE(c) &lt;&lt; endl;

This syntax is familiar to many, is easy to read and learn, and, importantly, consumes no resources. (Because the operator functions are essentially just inline aliases for their print() counterparts, no sketch gets larger or consumes more RAM as a result of their inclusion.)

I hope someday that this simple template will become part of the Arduino core as a stylistic option to writing series of prints. In the meanwhile, I use it in every sketch I build now!

Download

The latest version of Streaming is available at Streaming5.zip.

Version

You can get the current version number of the library by inspecting STREAMING_LIBRARY_VERSION.

Page last updated on July 31, 2024 at 8:06 am
115 Responses → “Streaming”

  1. Mikal

    9 years ago

    Many thanks @Adam!!


  2. yair

    8 years ago

    hi Mikal, please add it to the lib repository so it appears in the manage lib at arduino IDE. its just to good. there is an alternative called printEx which is buggy on my tests.


  3. Bill

    7 years ago

    streaming.cpp seems to be missing from the streaming5.zip file download. Thanks


  4. Raf

    7 years ago

    How to print ” sign ?


  5. Mikal

    6 years ago

    @Bill, Streaming is an unusual library: no .cpp!


  6. Solar_eta

    6 years ago

    Presumably this will also work instead of client.print(“blabla”); I assume that you need to open an Ethernet client first.
    Similarly for dataFile.print(“blabla”); once you’ve opened the file on the Sd cars with if (dataFile.open(fileName, O_WRITE | O_APPEND )) { etc.


  7. Mikal

    6 years ago

    @Solar_eta, I would expect it to. Let us know…!


  8. Hans Roark

    6 years ago

    Keep up the good work!


  9. Mike_Malaysia

    6 years ago

    Dear Mikal.
    Thank you for this. It’s fantastic.
    Did you made a “cin >>” equivalent header?? If so that fantastic^2
    (and if you did, what could one find it?)


  10. Ted Timmons

    6 years ago

    Looks like using _DEC(254) instead of char(254) takes up ~400 bytes of flash, as long as I replace all references to one or the other. On the other hand, using << streaming syntax (again, universally, instead of lcd.print and lcd.write) saved me about the same number of bytes and maybe some memory too. Neat.


  11. Raju

    6 years ago

    Hi I have the following code which also includes the Streaming library which i have downloaded from Streaming5.zip. I have used the Arduino Zero device to test the AnalogReadFast code. But the streaming library seems not helping to print out the serial print on the serial monitor. Can you please help me why the following code is not giving any serial print output on the serial monitor?

    Code:
    #include
    #include
    #include
    #include “avdweb_AnalogReadFast.h”

    const byte adcPin = A0;

    void setup()
    { Serial.begin(9600);
    while (!Serial);
    Serial << "\nanalogRead_10bit us1 analogRead_12bit us2 analogReadFast_10bit us3 analogReadFast_12bit us4" << endl;
    for (int i = 0; i < 10; i++) testAnalogRead();
    }

    void testAnalogRead()
    { static Stopwatch stopwatch(micros);
    //delay(1000);

    stopwatch.start();
    int adc1 = analogRead(adcPin); // default resolution 10bit 425us on SAMD21
    stopwatch.stop(); int t1 = stopwatch.interval;

    analogReadResolution(12);
    stopwatch.start();
    int adc2 = analogRead(adcPin); // 425us on SAMD21
    stopwatch.stop(); int t2 = stopwatch.interval;

    analogReadResolution(10);
    stopwatch.start();
    int adc3 = analogReadFast(adcPin); // 23us on SAMD21
    stopwatch.stop(); int t3 = stopwatch.interval;

    analogReadResolution(12);
    stopwatch.start();
    int adc4 = analogReadFast(adcPin); // 24us on SAMD21
    stopwatch.stop(); int t4 = stopwatch.interval;

    Serial << "adc1, t1, adc2, t2, adc3, t3, adc4, t4 << endl";
    //Serial << adc1, t1, adc2, t2, adc3, t3, adc4, t4;
    analogReadResolution(10); // restore default
    }

    void loop()
    {
    }

    Note: This code compiles in the Arduino Zero but does not give the serial output


  12. Guilherme Wiethaus

    2 months ago


  13. Mikal

    2 months ago

    Fixed! Thanks

2 Trackbacks For This Post
  1. Arduino :: TOOLS OPTIMALISASI ARDUINO (file presentasi .ppt) | Fajar Ridikc

    […] ĀšArduiniana Streaming, library untuk streaming […]

  2. Arduino – Language and Libraries – eletx

    […] Streaming– a method to simplify print statements […]

Leave a Reply