A library question

Hi all

Why is it that with certain libraries (e.g. Servo) one has to write this:

#include <Servo.h>
Servo servo1;  //<---------this line

Whilst on others like Serial, it's not required and we can just write say:

Serial.writeln("Hello there");

Does it have something to do with the fact that multiple instances of Servo can be used and only one for Serial?
Thanks.

Does it have something to do with the fact that multiple instances of Servo can be used and only one for Serial?

Exactly.

If you have a look at the SoftwareSerial library you will see that you can create multiple serial interfaces, each with their own name and with their own Tx and Rx pins defined unlike the normal (hardware) serial that uses fixed pins and of which there can only be one instance on most Arduino boards.

Thank you UKHeliBob

I wasn't too sure, hence my question.

If you look at HardwareSerial.cpp and HardwareSerial.h, you'll see that an instance of the class is created, and that the instance is called Serial, for the devices with just on hardware serial port. For other devices, such as the Leonardo and Mega that have multiple hardware serial ports, multiple instance of the HardwareSerial class are created.

@UKHeliBob

You are completely WRONG it has nothing to do with the number of instances and eveything to do with the IDE's own "helpfull" pre processor.

You use

Servo servo1;

to create an instance of servo. The IDE adds the equivalent for Serial. It also adds a whole list of includes and other bits. Always keep in mind the language is C/C++.

Mark