new to me: cout streaming for Teensy 2, 3

Status
Not open for further replies.

stevech

Well-known member
This code with cout that works for Teensy 2, 3.x.
I just encountered this library though it may have been around a while.
Demo example of use of library:

Code:
#include <Streaming.h>

void setup()
{
  Serial.begin(9600);
  delay(5000); // <<<<<<<<<<<<<<<<<<<<<<<< I added this to give time to open serial terminal/monitor
  int lettera = 'A';
  int month = 4, day = 17, year = 2009;
  
  Serial << "This is an example of the new streaming" << endl;
  Serial << "library.  This allows you to print variables" << endl;
  Serial << "and strings without having to type line after" << endl;
  Serial << "line of Serial.print() calls.  Examples: " << endl;
  
  Serial << "A is " << lettera << "." << endl;
  Serial << "The current date is " << day << "-" << month << "-" << year << "." << endl;
  
  Serial << "You can use modifiers too, for example:" << endl;
  Serial << _BYTE(lettera) << " is " << _HEX(lettera) << " in hex. " << endl;
}

void loop()
{}

The library for Teensy/Arduino is explained at
http://arduiniana.org/libraries/streaming/

The latest version of Streaming is available http://arduiniana.org/Streaming/Streaming5.zip

Download, drop the folder in the main IDE's library folder and try the example.
Note that you can use, instead of "Serial", "Serial1" etc within the classes derived from Print.
 
A little trick I learned as a replacement for the delay(5000) at the start of your code: while(!Serial){}

Though, it's a poor choice if you would like it to run independently of having to open the serial monitor!

Also, I think a similar function to this exists in the SDFat library, for those of you who already run it:

Code:
#include <SdFat.h>
#include <SdFatUtil.h>

ArduinoOutStream cout(Serial);

void setup(){
  while(!Serial){}
  cout << "Hello, World!" << endl;
}
void loop(){}
 
regarding
while(!Serial){}

I and others have had intermittent trouble with the loop never-ending. So I have this delay habit.
 
Status
Not open for further replies.
Back
Top