For beginners, you make variables and functions that use them and those can pile up and get long.
With C++ classes you can group variables and functions together under a name, like a variable, even make arrays like variables. They let you package data and code apart from the pile.
The C++ class object you probably first used is named Serial and has functions like begin() and print(). On the Arduino Mega2560 you also get Serial1, Serial2 and Serial3. Each one has data to run one of the 4 serial ports on that board and all the Serial Library functions. There is one class that 4 objects were made from.
I have a simple demo to get beginners started on classes, a led control class using the pin 13 led.
But first, about where the files go. I know where library files should go. That's the problem I ran into with the Arduino IDE. Arduino IDE will let me open tabs to edit .h and .cpp files in but try as I might, the saves always went to the sketch folder. So my answer is that while I am working on a library, keep it in the sketch folder. It's easier. That means any examples I provide meant to be open for users to learn from also have to be in the sketch folder.
So where is your sketch folder? Go to saveAs an IDE sketch, browse and find out but you don't have to know. In the IDE at the top of the edit window is a green bar. Once you have the project saved, at the right side of the green bar is a v button. It gives a popup menu. Pick New Tab and it will ask for a file name, make one for mylib.h and one for mylib.cpp then copy and paste from this post to the proper tabs. When you save those they will go to the sketch folder.
This is an example to show a simple roll your own library with just the basic first steps.
It makes one led object, led 13, and toggles it on/off every time that loop() counts 5 million.
The user is invited to connect other resistor+led to other pins and try things out.
Maybe even try adding values and functions to the led class. It's in the sketch folder, you can only break the sketch.
Updated 8/10/15, thanks PaulS! Karma to you!
// mylib demo
#include "mylib.h" // in "", the library is in the sketch folder
led myled13( 13 );
void setup( void )
{
Serial.begin( 115200 );
Serial.println( "\nserial connected..." );
myled13.start(); // must call to set pin mode
myled13.turnON();
}
void loop( void )
{
static long counter = 0;
if ( counter++ > 499999L )
{
counter = 0;
myled13.toggle();
}
}
// mylib.h demonstration of editing a library in the Arduino IDE
#ifndef mylib_h
#define mylib_h
class led
{
private:
unsigned char pin;
unsigned char state;
public:
led( unsigned char );
void start( ); // must run in setup to set pin mode
void turnON( );
void turnOFF( );
void toggle( );
void setLed( unsigned char );
};
#endif
// mylib cpp
#include "Arduino.h"
#include "mylib.h"
led::led( unsigned char p )
{
pin = p;
state = 0;
};
void led::start( )
{
digitalWrite( pin, LOW );
pinMode( pin, OUTPUT );
};
void led::turnON( )
{
state = 1;
digitalWrite( pin, state );
};
void led::turnOFF( )
{
state = 0;
digitalWrite( pin, state );
};
void led::toggle( )
{
state = state ^ 1; // XOR 1 reverses 0 and 1
digitalWrite( pin, state );
}
void led::setLed( unsigned char s )
{
state = s;
digitalWrite( pin, state );
};