When to use libraries,classes,etc.

It's obvious if using a LCD or servo etc. that I would want to use a library
and I see everybody using them.

I also see a lot of libraries for simpler functions(elapsedmillis(), button(),etc.)
and I don't see many using those.

I am curious what the advantages and more importantly disadvantages to using a
library vs writing the code myself.

On the other hand at what point do you create a library,class,etc.

for example I just finished a sketch that controls a single window and door lock for my car.
I am now writing it into a class so that I can call 4 instances of it to handle all the windows and locks.

This is but the first part of a command center? which will include data logging ,tracking etc. etc.
I am a long long ways away from that at the moment.

I don't really think this needs to be a library but at what point does one consider doing so.

EDIT
Doh!! I thought I was in the programming section

Create a class when you want to encapsulate something. Like your example with the widows, if you have 1 window you dont really need a class but with 4 Windows it lets you write code once and use it 4 times.

Libraries are for when you want to use the same code across multiple projects. The arduino community sort of abuses this by writing libraries for things like buttons where they are really just trying to hide how something really works so a newbie can still write code without learning how it works.

But for something like an LCD, who wants to write all of that out each time you want to add an LCD to a project. So put it in a library and you can use it over and over in many varied projects.

Hutkikz:
I am curious what the advantages and more importantly disadvantages to using a library vs writing the code myself.

When you use a library someone else wrote you are automatically doubling the number of people who have fixed bugs; the original author and you. Use published libraries to reduce the number of bugs.

However, using a library entails learning how to use that library. It is sometimes less time consuming to just write the code.

On the other hand at what point do you create a library...

At the point you make a third copy of any piece of code.

for example I just finished a sketch that controls a single window and door lock for my car.
I am now writing it into a class so that I can call 4 instances of it to handle all the windows and locks.

Not ready to be a library.

I don't really think this needs to be a library but at what point does one consider doing so.

Three is the magic number.

I see classes as very different from libraries. If I wanted to make a computer model of a bee farm it would make sense to make a class for "bee" because I will need many instances of it. I might also make a class for "beehive" because I will probably need several of them. But I would not make a class for "beeFarm" because I only have one of them.

Libraries, on the other hand, can be convenient ways to distribute code so others can use it.

I see no point in going to the trouble of packaging my own code into a library. If I want to reuse something I can just include a copy of the relevant function in my next project. But this is a matter of personal preference.

The big complaint I have about many libraries is their poor (usually non-existent) documentation. In particular the authors never tell you the circumstances in which their library is NOT appropriate and I don't know how many times I have seen long discussions about how to get a Library to do something.

In effect the user spends valuable time working on the Library (trying to understand it, or trying to modify it) which would have been better spent working on the user's own project.

This is not just an Arduino problem, but it can be exacerbated in the Arduino because Libraries can conflict with each other over the use of the limited resources available. For example the SoftwareSerial library and the Servo library fight with each other, even though both work well on their own.

...R