New Library Tutorial [unofficial]

There has been quite a few posts about the Library tutorial. So, I've tried to write a new one.

Your end result will be: [A library equivalent of the Blink example sketch]

Tutorial: http://www.arduino.cc/playground/Code/Library

Hi - this is helpful - thanks! I've sorted through a lot of this on my own (the "error + trial") method.

One thing I have not been able to figure out is how to enable Library instances to access the serial port (Serial.print, etc). I tried including Serial.h, etc. but the thing is, I don't want each object to call Serial.begin() - I want them to use the connection opened by the main sketch.

I tried defining a method as 'extern' and made some progress with that but ran into some issues.

Is there a standard way for library instances (say for a motor controller - where you want to control multiple motors) to read/write to Serial?

--Roy

One thing I have not been able to figure out is how to enable Library instances to access the serial port (Serial.print, etc). I tried including Serial.h, etc. but the thing is, I don’t want each object to call Serial.begin() - I want them to use the connection opened by the main sketch.

I usually just assume that the user has done the Serial.begin() call.

The Serial is included by

#include <HardwareSerial.h>

Another approach is to do this:

void MyClass::mySerialWrite( Print& printer , char* message ){
  printer.print( message );
}

And then use:

MyClass object;

void setup(){
  Serial.begin(9600);
  object.mySerialWrite( Serial, "printme");
}